aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2011-11-30 16:59:01 +0100
committerMatěj Cepl <mcepl@redhat.com>2011-11-30 22:15:04 +0100
commitfa8858e6b70dfabf026ef3018d702260c2405137 (patch)
treedfbc6add4ad4bc4a8cb3f86391e9bdc0793c9379
parent5bb8295a9193266a9f2a960e1825643c48c4bd1c (diff)
downloadjson_diff-fa8858e6b70dfabf026ef3018d702260c2405137.tar.gz
Set exit status of json_diff command.1.2.0
0 means no difference 1 there is a difference.
-rwxr-xr-xjson_diff.py17
-rw-r--r--test_json_diff.py22
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()