Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling source: docs/*.md pattern #67

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions techdocs/script/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def __init__(self, from_path, filesystem):
self.filesystem = filesystem

def validate(self, config):
if nodes.looks_wildcardish(config["source"]):
# This validator is not applicable to wildcardish paths
return
if nodes.looks_fileish(config["source"]) and not self.filesystem.is_file(
path.join(self.from_path, config["source"])
):
Expand All @@ -184,15 +187,17 @@ class IfSourceIsDirectoryThenDestinationMustAlsoBeDirectory(ConfigValidator):
def validate(self, config):
if nodes.looks_dirish(config["source"]) and not nodes.looks_dirish(config["destination"]):
raise ConfigError(
f"Source is a directory but destination is not. Did you forget to add a trailing slash to desination? Offending config: {config}"
f"Source is a directory but destination is not. Did you forget to add a trailing slash to desination? "
f"Offending config: {config}"
)


class WildardsInTheMiddleAreApplicableOnlyToDirectories(ConfigValidator):
def validate(self, config):
if "*" in config["source"][:-1] and not nodes.looks_dirish(config["destination"]):
raise ConfigError(
f"Putting wildcards in the middle of the pattern is only supported if the destination is a directory. Offending config: {config}"
f"Putting wildcards in the middle of the pattern is only supported if the destination is a directory. "
f"Offending config: {config}"
)


Expand Down
4 changes: 4 additions & 0 deletions techdocs/script/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ def looks_fileish(fspath):

def looks_dirish(fspath):
return fspath.endswith("/") or fspath.endswith("*") or fspath == "."


def looks_wildcardish(fspath):
return "*" in fspath
24 changes: 24 additions & 0 deletions techdocs/script/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@
},
False, # Absolute destination path
),
(
{
"documents": [
{
"project": "promil",
"source": "*.md",
"destination": "docs/promil/",
}
],
},
True, # Wildcard for file extension
),
(
{
"documents": [
{
"project": "promil",
"source": "*.md",
"destination": "docs/promil/test.md",
}
],
},
False, # Wildcard for file extension pointing single file
),
(
{
"documents": [
Expand Down
22 changes: 21 additions & 1 deletion techdocs/script/test_copier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@ def filesystem():
"project": "promil",
"source": "docs/*",
"destination": "somedir/",
"exclude": ["docs/internal/*"],
"exclude": [
"docs/internal/*",
"docs/*.txt",
],
},
{
"project": "promil",
"source": "other/*.txt",
"destination": "somedir/",
},
],
}
),
"/tmp/foo/README.md": "blabla",
"/tmp/foo/docs/one.md": "blabla",
"/tmp/foo/docs/first.txt": "blabla",
"/tmp/foo/docs/second.txt": "blabla",
"/tmp/foo/docs/inner/other-dir/foo.md": "blabla",
"/tmp/foo/docs/two.md": "blabla",
"/tmp/foo/docs/internal/int.md": "blabla",
"/tmp/foo/other/uno.txt": "blabla",
"/tmp/foo/other/due.txt": "blabla",
"/tmp/foo/other/non-text.md": "blabla",
"/tmp/foo/other/level/due.txt": "blabla",
"/tmp/bar/projects.json": json.dumps({"promil": {"path": "docs/promil"}}),
}
)
Expand All @@ -56,3 +70,9 @@ def test_copier(filesystem):
assert filesystem.is_file("/tmp/bar/docs/promil/somedir/one.md")
assert filesystem.is_file("/tmp/bar/docs/promil/somedir/two.md")
assert filesystem.is_file("/tmp/bar/docs/promil/somedir/inner/other-dir/foo.md")
assert not filesystem.is_file("/tmp/bar/docs/promil/somedir/first.txt")
assert not filesystem.is_file("/tmp/bar/docs/promil/somedir/second.txt")
assert filesystem.is_file("/tmp/bar/docs/promil/somedir/uno.txt")
assert filesystem.is_file("/tmp/bar/docs/promil/somedir/due.txt")
assert not filesystem.is_file("/tmp/bar/docs/promil/somedir/non-text.md")
assert not filesystem.is_file("/tmp/bar/docs/promil/somedir/level/due.txt")