Skip to content

Commit ec646c5

Browse files
authored
Merge pull request #251 from thiagowfx/thiagowfx/multiple-paths
feat: support multiple paths in yamale
2 parents a729a86 + 4200699 commit ec646c5

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

Diff for: yamale/command_line.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,19 @@ def _validate_dir(root, schema_name, cpus, parser, strict):
9898
raise ValueError("\n----\n".join(set(error_messages)))
9999

100100

101-
def _router(root, schema_name, cpus, parser, strict=True):
102-
root = os.path.abspath(root)
103-
if os.path.isfile(root):
104-
_validate_single(root, schema_name, parser, strict)
105-
else:
106-
_validate_dir(root, schema_name, cpus, parser, strict)
101+
def _router(paths, schema_name, cpus, parser, strict=True):
102+
for path in paths:
103+
path = os.path.abspath(path)
104+
if os.path.isdir(path):
105+
_validate_dir(path, schema_name, cpus, parser, strict)
106+
else:
107+
_validate_single(path, schema_name, parser, strict)
107108

108109

109110
def main():
110111
parser = argparse.ArgumentParser(description="Validate yaml files.")
111112
parser.add_argument(
112-
"path", metavar="PATH", default="./", nargs="?", help="folder to validate. Default is current directory."
113+
"paths", metavar="PATHS", default=["./"], nargs="*", help="Paths to validate, either directories or files. Default is the current directory."
113114
)
114115
parser.add_argument("-V", "--version", action="version", version=__version__)
115116
parser.add_argument("-s", "--schema", default="schema.yaml", help="filename of schema. Default is schema.yaml.")
@@ -125,7 +126,7 @@ def main():
125126
)
126127
args = parser.parse_args()
127128
try:
128-
_router(args.path, args.schema, args.cpu_num, args.parser, not args.no_strict)
129+
_router(args.paths, args.schema, args.cpu_num, args.parser, not args.no_strict)
129130
except (SyntaxError, NameError, TypeError, ValueError) as e:
130131
print("Validation failed!\n%s" % str(e))
131132
exit(1)

Diff for: yamale/tests/command_line_fixtures/yamls/good2.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
map:
2+
good: "world"
3+
yes: 4

Diff for: yamale/tests/test_command_line.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def scoped_change_dir(new_dir):
2525
def test_bad_yaml(parser):
2626
with pytest.raises(ValueError) as e:
2727
command_line._router(
28-
"yamale/tests/command_line_fixtures/yamls/bad.yaml",
28+
["yamale/tests/command_line_fixtures/yamls/bad.yaml"],
2929
"schema.yaml",
3030
1,
3131
parser,
@@ -37,7 +37,7 @@ def test_bad_yaml(parser):
3737
def test_required_keys_yaml(parser):
3838
with pytest.raises(ValueError) as e:
3939
command_line._router(
40-
"yamale/tests/command_line_fixtures/yamls/required_keys_bad.yaml",
40+
["yamale/tests/command_line_fixtures/yamls/required_keys_bad.yaml"],
4141
"required_keys_schema.yaml",
4242
1,
4343
parser,
@@ -47,13 +47,29 @@ def test_required_keys_yaml(parser):
4747

4848
@pytest.mark.parametrize("parser", parsers)
4949
def test_good_yaml(parser):
50-
command_line._router("yamale/tests/command_line_fixtures/yamls/good.yaml", "schema.yaml", 1, parser)
50+
command_line._router(["yamale/tests/command_line_fixtures/yamls/good.yaml"], "schema.yaml", 1, parser)
51+
52+
53+
def test_multiple_paths_good_yaml():
54+
command_line._router([
55+
"yamale/tests/command_line_fixtures/yamls/good.yaml",
56+
"yamale/tests/command_line_fixtures/yamls/good2.yaml",
57+
], "schema.yaml", 1, "PyYAML")
58+
59+
60+
def test_multiple_paths_bad_yaml():
61+
with pytest.raises(ValueError) as e:
62+
command_line._router([
63+
"yamale/tests/command_line_fixtures/yamls/good.yaml",
64+
"yamale/tests/command_line_fixtures/yamls/bad.yaml",
65+
], "schema.yaml", 1, "PyYAML")
66+
assert "map.bad: '12.5' is not a int." in e.value.message
5167

5268

5369
@pytest.mark.parametrize("parser", parsers)
5470
def test_good_relative_yaml(parser):
5571
command_line._router(
56-
"yamale/tests/command_line_fixtures/yamls/good.yaml",
72+
["yamale/tests/command_line_fixtures/yamls/good.yaml"],
5773
"../schema_dir/external.yaml",
5874
1,
5975
parser,
@@ -63,13 +79,13 @@ def test_good_relative_yaml(parser):
6379
@pytest.mark.parametrize("parser", parsers)
6480
def test_good_relative_schema_in_subfolder(parser):
6581
with scoped_change_dir("yamale/tests/command_line_fixtures/schema_dir"):
66-
command_line._router("../yamls/good.yaml", "external.yaml", 1, parser)
82+
command_line._router(["../yamls/good.yaml"], "external.yaml", 1, parser)
6783

6884

6985
@pytest.mark.parametrize("parser", parsers)
7086
def test_external_glob_schema(parser):
7187
command_line._router(
72-
"yamale/tests/command_line_fixtures/yamls/good.yaml",
88+
["yamale/tests/command_line_fixtures/yamls/good.yaml"],
7389
os.path.join(dir_path, "command_line_fixtures/schema_dir/ex*.yaml"),
7490
1,
7591
parser,
@@ -79,7 +95,7 @@ def test_external_glob_schema(parser):
7995
def test_empty_schema_file():
8096
with pytest.raises(ValueError, match="is an empty file!"):
8197
command_line._router(
82-
"yamale/tests/command_line_fixtures/empty_schema/data.yaml",
98+
["yamale/tests/command_line_fixtures/empty_schema/data.yaml"],
8399
"empty_schema.yaml",
84100
1,
85101
"PyYAML",
@@ -88,7 +104,7 @@ def test_empty_schema_file():
88104

89105
def test_external_schema():
90106
command_line._router(
91-
"yamale/tests/command_line_fixtures/yamls/good.yaml",
107+
["yamale/tests/command_line_fixtures/yamls/good.yaml"],
92108
os.path.join(dir_path, "command_line_fixtures/schema_dir/external.yaml"),
93109
1,
94110
"PyYAML",
@@ -103,7 +119,7 @@ def test_bad_dir():
103119
def test_bad_strict():
104120
with pytest.raises(ValueError) as e:
105121
command_line._router(
106-
"yamale/tests/command_line_fixtures/yamls/required_keys_extra_element.yaml",
122+
["yamale/tests/command_line_fixtures/yamls/required_keys_extra_element.yaml"],
107123
"required_keys_schema.yaml",
108124
4,
109125
"PyYAML",
@@ -115,7 +131,7 @@ def test_bad_strict():
115131
def test_bad_issue_54():
116132
with pytest.raises(yamale_error.YamaleError) as e:
117133
command_line._router(
118-
"yamale/tests/fixtures/nested_issue_54.yaml",
134+
["yamale/tests/fixtures/nested_issue_54.yaml"],
119135
"nested.yaml",
120136
4,
121137
"PyYAML",
@@ -132,4 +148,4 @@ def test_bad_issue_54():
132148

133149

134150
def test_nested_schema_issue_69():
135-
command_line._router("yamale/tests/command_line_fixtures/nestedYaml", "schema.yaml", 1, "PyYAML")
151+
command_line._router(["yamale/tests/command_line_fixtures/nestedYaml"], "schema.yaml", 1, "PyYAML")

0 commit comments

Comments
 (0)