From 34c29fee135803154c6bb494d908b98067f028e2 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Tue, 24 Apr 2012 01:43:03 +0200 Subject: Added -o parameter for output to the specified file. --- NEWS.txt | 3 +++ json_diff.py | 15 ++++++++++++--- test/test_json_diff.py | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index 2bb1003..f786673 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,3 +1,6 @@ +1.3.0 2012-04-24 + * Added -o parameter for output to the specified file. + 1.2.9 2012-02-13 * Give up on non-UTF-8 encoding for output. diff --git a/json_diff.py b/json_diff.py index 2f0d0de..0d191d1 100755 --- a/json_diff.py +++ b/json_diff.py @@ -23,6 +23,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from __future__ import print_function try: import json except ImportError: @@ -32,7 +33,7 @@ import logging from optparse import OptionParser __author__ = "Matěj Cepl" -__version__ = "1.2.9" +__version__ = "1.3.0" logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level=logging.INFO) @@ -337,6 +338,9 @@ def main(sys_args): parser.add_option("-i", "--include", action="append", dest="include", metavar="ATTR", default=[], help="attributes which should be exclusively used when comparing") + parser.add_option("-o", "--output", + action="append", dest="output", metavar="FILE", default=[], + help="name of the output file (default is stdout)") parser.add_option("-a", "--ignore-append", action="store_true", dest="ignore_append", metavar="BOOL", default=False, help="ignore appended keys") @@ -345,6 +349,11 @@ def main(sys_args): help="program should output to HTML report") (options, args) = parser.parse_args(sys_args[1:]) + if options.output: + outf = open(options.output[0], "w") + else: + outf = sys.stdout + if len(args) != 2: parser.error("Script requires two positional arguments, " + \ "names for old and new JSON file.") @@ -353,10 +362,10 @@ def main(sys_args): if options.HTMLoutput: # we want to hardcode UTF-8 here, because that's what's # in element of the generated HTML - print(unicode(HTMLFormatter(diff_res)).encode("utf-8")) + print(unicode(HTMLFormatter(diff_res)).encode("utf-8"), file=outf) else: outs = json.dumps(diff_res, indent=4, ensure_ascii=False) - print(outs.encode("utf-8")) + print(outs.encode("utf-8"), file=outf) if len(diff_res) > 0: return 1 diff --git a/test/test_json_diff.py b/test/test_json_diff.py index cb2e878..d7c16e3 100644 --- a/test/test_json_diff.py +++ b/test/test_json_diff.py @@ -2,8 +2,10 @@ """ PyUnit unit tests """ +from __future__ import print_function import unittest import sys +import tempfile import locale try: import json @@ -215,6 +217,27 @@ class TestMainArgsMgmt(unittest.TestCase): "\n\nexpected = %d\n\nobserved = %d" % (1, res)) + def test_args_run_output(self): + save_stdout = tempfile.NamedTemporaryFile(prefix="json_diff_") + cur_loc = locale.getlocale() + locale.setlocale(locale.LC_ALL, "cs_CZ.utf8") + + res = json_diff.main(["./test_json_diff.py", + "-o", save_stdout.name, + "test/old.json", "test/new.json"]) + + expected_file = open("test/diff.json") + expected = expected_file.read() + + save_stdout.seek(0) + observed = save_stdout.read() + save_stdout.close() + + locale.setlocale(locale.LC_ALL, cur_loc) + self.assertEqual(expected, observed, "non-stdout output file" + + "\n\nexpected = %s\n\nobserved = %s" % + (expected, observed)) + add_tests_from_class = unittest.TestLoader().loadTestsFromTestCase suite = unittest.TestSuite() -- cgit