Skip to content

Commit 98f11b5

Browse files
committed
Use base_uri for validate in cli; add tests
1 parent 4605755 commit 98f11b5

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

openapi_spec_validator/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def main(args: Optional[Sequence[str]] = None) -> None:
8484

8585
# read source
8686
try:
87-
spec, spec_url = reader(filename)
87+
spec, base_uri = reader(filename)
8888
except Exception as exc:
8989
print(exc)
9090
sys.exit(1)
@@ -103,7 +103,7 @@ def main(args: Optional[Sequence[str]] = None) -> None:
103103

104104
# validate
105105
try:
106-
validator.validate(spec, spec_url=spec_url)
106+
validator.validate(spec, base_uri=base_uri)
107107
except ValidationError as exc:
108108
print_validationerror(filename, exc, args_parsed.errors)
109109
sys.exit(1)

tests/integration/test_main.py

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,74 @@
66
from openapi_spec_validator.__main__ import main
77

88

9-
def test_schema_v2_detect():
9+
def test_schema_v2_detect(capsys):
1010
"""Test schema v2 is detected"""
1111
testargs = ["./tests/integration/data/v2.0/petstore.yaml"]
1212
main(testargs)
13+
out, err = capsys.readouterr()
14+
assert not err
15+
assert "./tests/integration/data/v2.0/petstore.yaml: OK\n" in out
1316

1417

15-
def test_schema_v31_detect():
18+
def test_schema_v31_detect(capsys):
1619
"""Test schema v3.1 is detected"""
1720
testargs = ["./tests/integration/data/v3.1/petstore.yaml"]
1821
main(testargs)
22+
out, err = capsys.readouterr()
23+
assert not err
24+
assert "./tests/integration/data/v3.1/petstore.yaml: OK\n" in out
1925

2026

21-
def test_schema_v31():
27+
def test_schema_v31(capsys):
2228
"""No errors when calling proper v3.1 file."""
2329
testargs = [
2430
"--schema",
2531
"3.1.0",
2632
"./tests/integration/data/v3.1/petstore.yaml",
2733
]
2834
main(testargs)
35+
out, err = capsys.readouterr()
36+
assert not err
37+
assert "./tests/integration/data/v3.1/petstore.yaml: OK\n" in out
2938

3039

31-
def test_schema_v30():
40+
def test_schema_v30(capsys):
3241
"""No errors when calling proper v3.0 file."""
3342
testargs = [
3443
"--schema",
3544
"3.0.0",
3645
"./tests/integration/data/v3.0/petstore.yaml",
3746
]
3847
main(testargs)
48+
out, err = capsys.readouterr()
49+
assert not err
50+
assert "./tests/integration/data/v3.0/petstore.yaml: OK\n" in out
3951

4052

41-
def test_schema_v2():
53+
def test_schema_v2(capsys):
4254
"""No errors when calling with proper v2 file."""
4355
testargs = [
4456
"--schema",
4557
"2.0",
4658
"./tests/integration/data/v2.0/petstore.yaml",
4759
]
4860
main(testargs)
61+
out, err = capsys.readouterr()
62+
assert not err
63+
assert "./tests/integration/data/v2.0/petstore.yaml: OK\n" in out
64+
65+
66+
def test_many(capsys):
67+
"""No errors when calling with proper v2 and v3 files."""
68+
testargs = [
69+
"./tests/integration/data/v2.0/petstore.yaml",
70+
"./tests/integration/data/v3.0/petstore.yaml",
71+
]
72+
main(testargs)
73+
out, err = capsys.readouterr()
74+
assert not err
75+
assert "./tests/integration/data/v2.0/petstore.yaml: OK\n" in out
76+
assert "./tests/integration/data/v3.0/petstore.yaml: OK\n" in out
4977

5078

5179
def test_errors_on_missing_description_best(capsys):
@@ -57,6 +85,11 @@ def test_errors_on_missing_description_best(capsys):
5785
with pytest.raises(SystemExit):
5886
main(testargs)
5987
out, err = capsys.readouterr()
88+
assert not err
89+
assert (
90+
"./tests/integration/data/v3.0/missing-description.yaml: Validation Error:"
91+
in out
92+
)
6093
assert "Failed validating" in out
6194
assert "'description' is a required property" in out
6295
assert "'$ref' is a required property" not in out
@@ -73,13 +106,18 @@ def test_errors_on_missing_description_full(capsys):
73106
with pytest.raises(SystemExit):
74107
main(testargs)
75108
out, err = capsys.readouterr()
109+
assert not err
110+
assert (
111+
"./tests/integration/data/v3.0/missing-description.yaml: Validation Error:"
112+
in out
113+
)
76114
assert "Failed validating" in out
77115
assert "'description' is a required property" in out
78116
assert "'$ref' is a required property" in out
79117
assert "1 more subschema error" not in out
80118

81119

82-
def test_schema_unknown():
120+
def test_schema_unknown(capsys):
83121
"""Errors on running with unknown schema."""
84122
testargs = [
85123
"--schema",
@@ -88,9 +126,12 @@ def test_schema_unknown():
88126
]
89127
with pytest.raises(SystemExit):
90128
main(testargs)
129+
out, err = capsys.readouterr()
130+
assert "error: argument --schema" in err
131+
assert not out
91132

92133

93-
def test_validation_error():
134+
def test_validation_error(capsys):
94135
"""SystemExit on running with ValidationError."""
95136
testargs = [
96137
"--schema",
@@ -99,13 +140,20 @@ def test_validation_error():
99140
]
100141
with pytest.raises(SystemExit):
101142
main(testargs)
143+
out, err = capsys.readouterr()
144+
assert not err
145+
assert (
146+
"./tests/integration/data/v2.0/petstore.yaml: Validation Error:" in out
147+
)
148+
assert "Failed validating" in out
149+
assert "'openapi' is a required property" in out
102150

103151

104152
@mock.patch(
105153
"openapi_spec_validator.__main__.openapi_v30_spec_validator.validate",
106154
side_effect=Exception,
107155
)
108-
def test_unknown_error(m_validate):
156+
def test_unknown_error(m_validate, capsys):
109157
"""SystemExit on running with unknown error."""
110158
testargs = [
111159
"--schema",
@@ -114,16 +162,22 @@ def test_unknown_error(m_validate):
114162
]
115163
with pytest.raises(SystemExit):
116164
main(testargs)
165+
out, err = capsys.readouterr()
166+
assert not err
167+
assert "./tests/integration/data/v2.0/petstore.yaml: Error:" in out
117168

118169

119-
def test_nonexisting_file():
170+
def test_nonexisting_file(capsys):
120171
"""Calling with non-existing file should sys.exit."""
121172
testargs = ["i_dont_exist.yaml"]
122173
with pytest.raises(SystemExit):
123174
main(testargs)
175+
out, err = capsys.readouterr()
176+
assert not err
177+
assert "No such file: i_dont_exist.yaml\n" in out
124178

125179

126-
def test_schema_stdin():
180+
def test_schema_stdin(capsys):
127181
"""Test schema from STDIN"""
128182
spes_path = "./tests/integration/data/v3.0/petstore.yaml"
129183
with open(spes_path) as spec_file:
@@ -133,3 +187,6 @@ def test_schema_stdin():
133187
testargs = ["--schema", "3.0.0", "-"]
134188
with mock.patch("openapi_spec_validator.__main__.sys.stdin", spec_io):
135189
main(testargs)
190+
out, err = capsys.readouterr()
191+
assert not err
192+
assert "stdin: OK\n" in out

0 commit comments

Comments
 (0)