-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nick Timkovich
committed
Aug 9, 2018
1 parent
7e27cad
commit 8457f1b
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Swagger spec validator. | ||
""" | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import argparse | ||
import sys | ||
|
||
from swagger_spec_validator.common import SwaggerValidationError | ||
from swagger_spec_validator.util import validate_spec_url | ||
|
||
|
||
def _extract_error_message(exc): | ||
""" | ||
The validators return nested errors coming from common.wrap_exception, but | ||
other errors like invalid URLs aren't nested. Try to | ||
""" | ||
try: | ||
return exc.args[1].args[0] | ||
except IndexError: | ||
pass | ||
|
||
return exc.args[0] | ||
|
||
|
||
def main(argv=None): | ||
if argv is None: | ||
argv = sys.argv[1:] | ||
|
||
parser = argparse.ArgumentParser(description=__doc__) | ||
|
||
parser.add_argument("url", type=str, help="URL of Swagger spec to validate") | ||
parser.add_argument( | ||
"-q", | ||
"--quiet", | ||
action="store_true", | ||
help="No output if validation is successful", | ||
) | ||
|
||
args = parser.parse_args(argv) | ||
|
||
try: | ||
validate_spec_url(args.url) | ||
except SwaggerValidationError as exc: | ||
error_message = _extract_error_message(exc) | ||
print(error_message, file=sys.stderr) | ||
return 1 | ||
|
||
if not args.quiet: | ||
print("Validation successful!", file=sys.stderr) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from swagger_spec_validator.cli import main | ||
from swagger_spec_validator.common import get_uri_from_file_path | ||
|
||
|
||
def test_cli_main_goodfile(): | ||
good_file = "./tests/data/v2.0/petstore.json" | ||
file_uri = get_uri_from_file_path(good_file) | ||
|
||
exit_code = main([file_uri]) | ||
|
||
assert exit_code == 0 | ||
|
||
|
||
def test_cli_main_badfile(): | ||
bad_file = "./tests/data/v2.0/test_fails_on_invalid_external_ref_in_list/petstore.json" | ||
file_uri = get_uri_from_file_path(bad_file) | ||
|
||
exit_code = main([file_uri]) | ||
|
||
assert exit_code == 1 |