diff options
-rwxr-xr-x | json_diff.py | 17 | ||||
-rw-r--r-- | test_json_diff.py | 22 |
2 files changed, 33 insertions, 6 deletions
diff --git a/json_diff.py b/json_diff.py index 5d469fd..cc8bfa7 100755 --- a/json_diff.py +++ b/json_diff.py @@ -32,7 +32,7 @@ import logging from optparse import OptionParser __author__ = "Matěj Cepl" -__version__ = "1.1.0" +__version__ = "1.2.0" import locale @@ -328,6 +328,7 @@ class Comparator(object): return self._filter_results(result) + def main(sys_args): """Main function, to process command line arguments etc.""" usage = "usage: %prog [options] old.json new.json" @@ -345,21 +346,25 @@ def main(sys_args): action="store_true", dest="HTMLoutput", metavar="BOOL", default=False, help="program should output to HTML report") (options, args) = parser.parse_args(sys_args[1:]) - print >> sys.stderr, "options = %s" % options - print >> sys.stderr, "args = %s" % args if len(args) != 2: parser.error("Script requires two positional arguments, " + \ "names for old and new JSON file.") diff = Comparator(open(args[0]), open(args[1]), options) + diff_res = diff.compare_dicts() if options.HTMLoutput: - diff_res = diff.compare_dicts() # we want to hardcode UTF-8 here, because that's what's # in <meta> element of the generated HTML print(unicode(HTMLFormatter(diff_res)).encode("utf-8")) else: - outs = json.dumps(diff.compare_dicts(), indent=4, ensure_ascii=False) + outs = json.dumps(diff_res, indent=4, ensure_ascii=False) print(outs.encode(locale.getpreferredencoding())) + if len(diff_res) > 0: + return 1 + + return 0 + if __name__ == "__main__": - main(sys.argv) + main_res = main(sys.argv) + sys.exit(main_res) diff --git a/test_json_diff.py b/test_json_diff.py index 5ebfb89..95296e0 100644 --- a/test_json_diff.py +++ b/test_json_diff.py @@ -18,12 +18,14 @@ from test_strings import ARRAY_DIFF, ARRAY_NEW, ARRAY_OLD, \ NESTED_DIFF_IGNORING, \ SIMPLE_ARRAY_OLD, SIMPLE_DIFF, SIMPLE_DIFF_HTML, SIMPLE_NEW, SIMPLE_OLD + class OptionsClass(object): def __init__(self, inc=None, exc=None, ign=None): self.exclude = exc self.include = inc self.ignore_append = ign + class OurTestCase(unittest.TestCase): def _run_test(self, oldf, newf, difff, msg="", opts=None): diffator = json_diff.Comparator(oldf, newf, opts) @@ -180,5 +182,25 @@ class TestMainArgsMgmt(unittest.TestCase): self.assertEquals(observed[:len(expected)], expected, "testing -h usage message") + def test_args_run_same(self): + save_stdout = StringIO() + sys.stdout = save_stdout + + res = json_diff.main(["./test_json_diff.py", + "test/old.json", "test/old.json"]) + + sys.stdout = sys.__stdout__ + self.assertEquals(res, 0, "testing -h usage message") + + def test_args_run_different(self): + save_stdout = StringIO() + sys.stdout = save_stdout + + res = json_diff.main(["./test_json_diff.py", + "test/old.json", "test/new.json"]) + + sys.stdout = sys.__stdout__ + self.assertEquals(res, 1, "testing -h usage message") + if __name__ == "__main__": unittest.main() |