aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xjson_diff.py34
-rw-r--r--test_json_diff.py41
2 files changed, 48 insertions, 27 deletions
diff --git a/json_diff.py b/json_diff.py
index 8322fd9..5d469fd 100755
--- a/json_diff.py
+++ b/json_diff.py
@@ -27,6 +27,7 @@ try:
import json
except ImportError:
import simplejson as json
+import sys
import logging
from optparse import OptionParser
@@ -162,19 +163,13 @@ class Comparator(object):
raise BadJSONError("Cannot decode object from JSON\n%s" %
unicode(exc))
- if opts and ("excluded_attrs" in opts):
- self.excluded_attributes = opts['excluded_attrs']
- else:
- self.excluded_attributes = ()
- if opts and ("included_attrs" in opts):
- self.included_attributes = opts['included_attrs']
- else:
- self.included_attributes = ()
-
- if opts and ('ignore_append' in opts):
- self.ignore_appended = opts['ignore_append']
- else:
- self.ignore_appended = False
+ self.excluded_attributes = []
+ self.included_attributes = []
+ self.ignore_appended = False
+ if opts:
+ self.excluded_attributes = opts.exclude or []
+ self.included_attributes = opts.include or []
+ self.ignore_appended = opts.ignore_append or False
def _is_incex_key(self, key, value):
"""Is this key excluded or not among included ones? If yes, it should
@@ -333,10 +328,9 @@ class Comparator(object):
return self._filter_results(result)
-
-if __name__ == "__main__":
+def main(sys_args):
+ """Main function, to process command line arguments etc."""
usage = "usage: %prog [options] old.json new.json"
- description = "Generates diff between two JSON files."
parser = OptionParser(usage=usage)
parser.add_option("-x", "--exclude",
action="append", dest="exclude", metavar="ATTR", default=[],
@@ -350,12 +344,13 @@ if __name__ == "__main__":
parser.add_option("-H", "--HTML",
action="store_true", dest="HTMLoutput", metavar="BOOL", default=False,
help="program should output to HTML report")
- (options, args) = parser.parse_args()
+ (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)
if options.HTMLoutput:
diff_res = diff.compare_dicts()
@@ -365,3 +360,6 @@ if __name__ == "__main__":
else:
outs = json.dumps(diff.compare_dicts(), indent=4, ensure_ascii=False)
print(outs.encode(locale.getpreferredencoding()))
+
+if __name__ == "__main__":
+ main(sys.argv)
diff --git a/test_json_diff.py b/test_json_diff.py
index 2c97974..5ebfb89 100644
--- a/test_json_diff.py
+++ b/test_json_diff.py
@@ -3,6 +3,7 @@
PyUnit unit tests
"""
import unittest
+import sys
try:
import json
except ImportError:
@@ -17,6 +18,11 @@ 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):
@@ -115,15 +121,15 @@ class TestHappyPath(OurTestCase):
def test_nested_excluded(self):
self._run_test_strings(NESTED_OLD, NESTED_NEW, NESTED_DIFF_EXCL,
"Nested objects diff with exclusion.",
- {'excluded_attrs': ("nome",)})
+ OptionsClass(exc=["nome"]))
def test_nested_included(self):
self._run_test_strings(NESTED_OLD, NESTED_NEW, NESTED_DIFF_INCL,
- "Nested objects diff.", {'included_attrs': ("nome",)})
+ "Nested objects diff.", OptionsClass(inc=["nome"]))
def test_nested_ignoring_append(self):
self._run_test_strings(NESTED_OLD, NESTED_NEW, NESTED_DIFF_IGNORING,
- "Nested objects diff.", {'ignore_append': True})
+ "Nested objects diff.", OptionsClass(ign=True))
class TestadPath(OurTestCase):
@@ -145,17 +151,34 @@ class TestadPath(OurTestCase):
class TestPiglitData(OurTestCase):
-# def test_piglit_results(self):
-# self._run_test(open("test/old-testing-data.json"),
-# open("test/new-testing-data.json"),
-# open("test/diff-testing-data.json"), "Large piglit results diff.")
-
def test_piglit_result_only(self):
self._run_test(open("test/old-testing-data.json"),
open("test/new-testing-data.json"),
open("test/diff-result-only-testing-data.json"),
"Large piglit reports diff (just resume field).",
- {'included_attrs': ("result",)})
+ OptionsClass(inc=["result"]))
+
+# def test_piglit_results(self):
+# self._run_test(open("test/old-testing-data.json"),
+# open("test/new-testing-data.json"),
+# open("test/diff-testing-data.json"), "Large piglit results diff.")
+
+
+class TestMainArgsMgmt(unittest.TestCase):
+ def test_args_help(self):
+ save_stdout = StringIO()
+ sys.stdout = save_stdout
+
+ try:
+ json_diff.main(["./test_json_diff.py", "-h"])
+ except SystemExit:
+ save_stdout.seek(0)
+ sys.stdout = sys.__stdout__
+ expected = "Usage:"
+ observed = save_stdout.read()
+
+ self.assertEquals(observed[:len(expected)], expected,
+ "testing -h usage message")
if __name__ == "__main__":
unittest.main()