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

feat: support multiple paths in yamale #251

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 9 additions & 8 deletions yamale/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ def _validate_dir(root, schema_name, cpus, parser, strict):
raise ValueError("\n----\n".join(set(error_messages)))


def _router(root, schema_name, cpus, parser, strict=True):
root = os.path.abspath(root)
if os.path.isfile(root):
_validate_single(root, schema_name, parser, strict)
else:
_validate_dir(root, schema_name, cpus, parser, strict)
def _router(paths, schema_name, cpus, parser, strict=True):
for path in paths:
path = os.path.abspath(path)
if os.path.isfile(path):
_validate_single(path, schema_name, parser, strict)
else:
_validate_dir(path, schema_name, cpus, parser, strict)


def main():
parser = argparse.ArgumentParser(description="Validate yaml files.")
parser.add_argument(
"path", metavar="PATH", default="./", nargs="?", help="folder to validate. Default is current directory."
"paths", metavar="PATHS", default=["./"], nargs="*", help="Paths to validate, either directories or files. Default is the current directory."
)
parser.add_argument("-V", "--version", action="version", version=__version__)
parser.add_argument("-s", "--schema", default="schema.yaml", help="filename of schema. Default is schema.yaml.")
Expand All @@ -125,7 +126,7 @@ def main():
)
args = parser.parse_args()
try:
_router(args.path, args.schema, args.cpu_num, args.parser, not args.no_strict)
_router(args.paths, args.schema, args.cpu_num, args.parser, not args.no_strict)
except (SyntaxError, NameError, TypeError, ValueError) as e:
print("Validation failed!\n%s" % str(e))
exit(1)
Expand Down
3 changes: 3 additions & 0 deletions yamale/tests/command_line_fixtures/yamls/good2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
map:
good: "world"
yes: 4
39 changes: 28 additions & 11 deletions yamale/tests/test_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def scoped_change_dir(new_dir):
def test_bad_yaml(parser):
with pytest.raises(ValueError) as e:
command_line._router(
"yamale/tests/command_line_fixtures/yamls/bad.yaml",
["yamale/tests/command_line_fixtures/yamls/bad.yaml"],
"schema.yaml",
1,
parser,
Expand All @@ -37,7 +37,7 @@ def test_bad_yaml(parser):
def test_required_keys_yaml(parser):
with pytest.raises(ValueError) as e:
command_line._router(
"yamale/tests/command_line_fixtures/yamls/required_keys_bad.yaml",
["yamale/tests/command_line_fixtures/yamls/required_keys_bad.yaml"],
"required_keys_schema.yaml",
1,
parser,
Expand All @@ -47,13 +47,30 @@ def test_required_keys_yaml(parser):

@pytest.mark.parametrize("parser", parsers)
def test_good_yaml(parser):
command_line._router("yamale/tests/command_line_fixtures/yamls/good.yaml", "schema.yaml", 1, parser)
command_line._router(["yamale/tests/command_line_fixtures/yamls/good.yaml"], "schema.yaml", 1, parser)


def test_multiple_paths_good_yaml():
command_line._router([
"yamale/tests/command_line_fixtures/yamls/good.yaml",
"yamale/tests/command_line_fixtures/yamls/good2.yaml",
], "schema.yaml", 1, "PyYAML")


def test_multiple_paths_bad_yaml():
with pytest.raises(ValueError) as e:
command_line._router([
"yamale/tests/command_line_fixtures/yamls/bad.yaml",
"yamale/tests/command_line_fixtures/yamls/required_keys_bad.yaml",
], "schema.yaml", 1, "PyYAML")
assert "map.bad: '12.5' is not a str." in e.value.message
assert "map.key: Required field missing" in e.value.message


@pytest.mark.parametrize("parser", parsers)
def test_good_relative_yaml(parser):
command_line._router(
"yamale/tests/command_line_fixtures/yamls/good.yaml",
["yamale/tests/command_line_fixtures/yamls/good.yaml"],
"../schema_dir/external.yaml",
1,
parser,
Expand All @@ -63,13 +80,13 @@ def test_good_relative_yaml(parser):
@pytest.mark.parametrize("parser", parsers)
def test_good_relative_schema_in_subfolder(parser):
with scoped_change_dir("yamale/tests/command_line_fixtures/schema_dir"):
command_line._router("../yamls/good.yaml", "external.yaml", 1, parser)
command_line._router(["../yamls/good.yaml"], "external.yaml", 1, parser)


@pytest.mark.parametrize("parser", parsers)
def test_external_glob_schema(parser):
command_line._router(
"yamale/tests/command_line_fixtures/yamls/good.yaml",
["yamale/tests/command_line_fixtures/yamls/good.yaml"],
os.path.join(dir_path, "command_line_fixtures/schema_dir/ex*.yaml"),
1,
parser,
Expand All @@ -79,7 +96,7 @@ def test_external_glob_schema(parser):
def test_empty_schema_file():
with pytest.raises(ValueError, match="is an empty file!"):
command_line._router(
"yamale/tests/command_line_fixtures/empty_schema/data.yaml",
["yamale/tests/command_line_fixtures/empty_schema/data.yaml"],
"empty_schema.yaml",
1,
"PyYAML",
Expand All @@ -88,7 +105,7 @@ def test_empty_schema_file():

def test_external_schema():
command_line._router(
"yamale/tests/command_line_fixtures/yamls/good.yaml",
["yamale/tests/command_line_fixtures/yamls/good.yaml"],
os.path.join(dir_path, "command_line_fixtures/schema_dir/external.yaml"),
1,
"PyYAML",
Expand All @@ -103,7 +120,7 @@ def test_bad_dir():
def test_bad_strict():
with pytest.raises(ValueError) as e:
command_line._router(
"yamale/tests/command_line_fixtures/yamls/required_keys_extra_element.yaml",
["yamale/tests/command_line_fixtures/yamls/required_keys_extra_element.yaml"],
"required_keys_schema.yaml",
4,
"PyYAML",
Expand All @@ -115,7 +132,7 @@ def test_bad_strict():
def test_bad_issue_54():
with pytest.raises(yamale_error.YamaleError) as e:
command_line._router(
"yamale/tests/fixtures/nested_issue_54.yaml",
["yamale/tests/fixtures/nested_issue_54.yaml"],
"nested.yaml",
4,
"PyYAML",
Expand All @@ -132,4 +149,4 @@ def test_bad_issue_54():


def test_nested_schema_issue_69():
command_line._router("yamale/tests/command_line_fixtures/nestedYaml", "schema.yaml", 1, "PyYAML")
command_line._router(["yamale/tests/command_line_fixtures/nestedYaml"], "schema.yaml", 1, "PyYAML")
Loading