diff --git a/openapi_spec_validator/__main__.py b/openapi_spec_validator/__main__.py index 0e0c4340..6b1fc4b8 100644 --- a/openapi_spec_validator/__main__.py +++ b/openapi_spec_validator/__main__.py @@ -152,6 +152,7 @@ def main(args: Sequence[str] | None = None) -> None: schemas.get_validator_backend() != "jsonschema-rs" ) + read_failed = False for filename in args_parsed.file: # choose source reader = read_from_filename @@ -163,8 +164,9 @@ def main(args: Sequence[str] | None = None) -> None: try: spec, base_uri = reader(filename) except Exception as exc: - print(exc) - sys.exit(1) + print_error(filename, exc) + read_failed = True + continue # choose the validator validators: dict[str, type[SpecValidator] | None] = { @@ -218,6 +220,9 @@ def main(args: Sequence[str] | None = None) -> None: else: print_ok(filename) + if read_failed: + sys.exit(1) + if __name__ == "__main__": main() diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 55c68140..84f14ac7 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -221,6 +221,20 @@ def test_nonexisting_file(capsys): assert "No such file: i_dont_exist.yaml\n" in out +def test_read_error_names_file_and_continues(capsys): + """A file that cannot be read is named and does not stop the rest.""" + testargs = [ + "i_dont_exist.yaml", + "./tests/integration/data/v3.1/petstore.yaml", + ] + with pytest.raises(SystemExit): + main(testargs) + out, err = capsys.readouterr() + assert not err + assert "i_dont_exist.yaml: Error: No such file: i_dont_exist.yaml\n" in out + assert "./tests/integration/data/v3.1/petstore.yaml: OK\n" in out + + def test_schema_stdin(capsys): """Test schema from STDIN""" spes_path = "./tests/integration/data/v3.0/petstore.yaml"