Skip to content

Commit

Permalink
Merge pull request #99 from manu-chroma/master
Browse files Browse the repository at this point in the history
schema_salad/main.py: --version now correctly prints version
  • Loading branch information
mr-c committed Mar 21, 2017
2 parents 61c2203 + a63f75f commit ca9218e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
15 changes: 10 additions & 5 deletions schema_salad/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def main(argsl=None): # type: (List[str]) -> int
"--print-index", action="store_true", help="Print node index")
exgroup.add_argument("--print-metadata",
action="store_true", help="Print document metadata")
exgroup.add_argument("--version", action="store_true",
help="Print version")

exgroup = parser.add_mutually_exclusive_group()
exgroup.add_argument("--strict", action="store_true", help="Strict validation (unrecognized or out of place fields are error)",
Expand All @@ -79,11 +77,18 @@ def main(argsl=None): # type: (List[str]) -> int
exgroup.add_argument("--debug", action="store_true",
help="Print even more logging")

parser.add_argument("schema", type=str)
parser.add_argument("schema", type=str, nargs="?", default=None)
parser.add_argument("document", type=str, nargs="?", default=None)
parser.add_argument("--version", "-v", action="store_true",
help="Print version", default=None)


args = parser.parse_args(argsl)

if args.version is None and args.schema is None:
print('%s: error: too few arguments' % sys.argv[0])
return 1

if args.quiet:
_logger.setLevel(logging.WARN)
if args.debug:
Expand All @@ -92,10 +97,10 @@ def main(argsl=None): # type: (List[str]) -> int
pkg = pkg_resources.require("schema_salad")
if pkg:
if args.version:
print("%s %s" % (sys.argv[0], pkg[0].version))
print("%s Current version: %s" % (sys.argv[0], pkg[0].version))
return 0
else:
_logger.info("%s %s", sys.argv[0], pkg[0].version)
_logger.info("%s Current version: %s", sys.argv[0], pkg[0].version)

# Get the metaschema to validate the schema
metaschema_names, metaschema_doc, metaschema_loader = schema.get_metaschema()
Expand Down
40 changes: 40 additions & 0 deletions schema_salad/tests/test_cli_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
import sys

import schema_salad.main as cli_parser

""" for capturing print() output """
from contextlib import contextmanager
from StringIO import StringIO

@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err


""" test different sets of command line arguments"""
class ParseCliArgs(unittest.TestCase):

def test_version(self):
args = [["--version"], ["-v"]]
for arg in args:
with captured_output() as (out, err):
cli_parser.main(arg)

response = out.getvalue().strip() # capture output and strip newline
self.assertTrue("Current version" in response)

def test_empty_input(self):
# running schema_salad tool wihtout any args
args = []
with captured_output() as (out, err):
cli_parser.main(args)

response = out.getvalue().strip()
self.assertTrue("error: too few arguments" in response)

0 comments on commit ca9218e

Please sign in to comment.