File tree Expand file tree Collapse file tree 2 files changed +36
-4
lines changed
Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change 33import os
44import sys
55
6- from openapi_spec_validator import validate_spec_url
6+ from openapi_spec_validator import validate_spec_url , validate_v2_spec_url
77from openapi_spec_validator .exceptions import ValidationError
88
99logger = logging .getLogger (__name__ )
1616def main (args = None ):
1717 parser = argparse .ArgumentParser ()
1818 parser .add_argument ('filename' , help = "Absolute or relative path to file" )
19+ parser .add_argument (
20+ '--schema' ,
21+ help = "OpenAPI schema (default: 3.0.0)" ,
22+ type = str ,
23+ choices = ['2.0' , '3.0.0' ],
24+ default = '3.0.0'
25+ )
1926 args = parser .parse_args (args )
2027 filename = args .filename
2128 filename = os .path .abspath (filename )
29+ # choose the validator
30+ if args .schema == '2.0' :
31+ validate_url = validate_v2_spec_url
32+ elif args .schema == '3.0.0' :
33+ validate_url = validate_spec_url
34+ # validate
2235 try :
23- validate_spec_url ('file://' + filename )
36+ validate_url ('file://' + filename )
2437 except ValidationError as e :
2538 print (e )
2639 sys .exit (1 )
Original file line number Diff line number Diff line change 33from openapi_spec_validator .__main__ import main
44
55
6- def test_happy_path ():
7- """No errors when calling with proper file. """
6+ def test_schema_default ():
7+ """Test default schema is 3.0.0 """
88 testargs = ['./tests/integration/data/v3.0/petstore.yaml' ]
99 main (testargs )
1010
1111
12+ def test_schema_v3 ():
13+ """No errors when calling proper v3 file."""
14+ testargs = ['--schema' , '3.0.0' , './tests/integration/data/v3.0/petstore.yaml' ]
15+ main (testargs )
16+
17+
18+ def test_schema_v2 ():
19+ """No errors when calling with proper v2 file."""
20+ testargs = ['--schema' , '2.0' , './tests/integration/data/v2.0/petstore.yaml' ]
21+ main (testargs )
22+
23+
24+ def test_schema_unknown ():
25+ """Errors on running with unknown schema."""
26+ testargs = ['--schema' , 'x.x' , './tests/integration/data/v2.0/petstore.yaml' ]
27+ with pytest .raises (SystemExit ):
28+ main (testargs )
29+
30+
1231def test_nonexisting_file ():
1332 """Calling with non-existing file should sys.exit."""
1433 testargs = ['i_dont_exist.yaml' ]
You can’t perform that action at this time.
0 commit comments