From db53d9a525ea7fec93a00fc17a1e094e821961ad Mon Sep 17 00:00:00 2001 From: Bob Ippolito Date: Thu, 21 Feb 2013 15:13:39 -0800 Subject: simplejson.tool tests and bugfix for Python 3.x (#60) --- simplejson/tests/__init__.py | 1 + simplejson/tests/test_tool.py | 80 +++++++++++++++++++++++++++++++++++++++++++ simplejson/tool.py | 24 +++++++------ 3 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 simplejson/tests/test_tool.py (limited to 'simplejson') diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py index da21365..61540d5 100644 --- a/simplejson/tests/__init__.py +++ b/simplejson/tests/__init__.py @@ -59,6 +59,7 @@ def all_tests_suite(): 'simplejson.tests.test_decimal', 'simplejson.tests.test_tuple', 'simplejson.tests.test_namedtuple', + 'simplejson.tests.test_tool', ]) suite = additional_tests(suite) return OptionalExtensionTestSuite([suite]) diff --git a/simplejson/tests/test_tool.py b/simplejson/tests/test_tool.py new file mode 100644 index 0000000..f39f0ee --- /dev/null +++ b/simplejson/tests/test_tool.py @@ -0,0 +1,80 @@ +import os +import sys +import textwrap +import unittest +import subprocess +import tempfile + +class TestTool(unittest.TestCase): + data = """ + + [["blorpie"],[ "whoops" ] , [ + ],\t"d-shtaeou",\r"d-nthiouh", + "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" + :"yes"} ] + """ + + expect = textwrap.dedent("""\ + [ + [ + "blorpie" + ], + [ + "whoops" + ], + [], + "d-shtaeou", + "d-nthiouh", + "i-vhbjkhnth", + { + "nifty": 87 + }, + { + "field": "yes", + "morefield": false + } + ] + """) + + def runTool(self, args=None, data=None): + argv = [sys.executable, '-m', 'simplejson.tool'] + if args: + argv.extend(args) + proc = subprocess.Popen(argv, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE) + out, err = proc.communicate(data) + self.assertEqual(len(err), 0) + self.assertEqual(proc.returncode, 0) + return out + + def test_stdin_stdout(self): + self.assertEqual( + self.runTool(data=self.data.encode()), + self.expect.encode()) + + def test_infile_stdout(self): + with tempfile.NamedTemporaryFile() as infile: + infile.write(self.data.encode()) + infile.flush() + self.assertEqual( + self.runTool(args=[infile.name]), + self.expect.encode()) + + def test_infile_outfile(self): + with tempfile.NamedTemporaryFile() as infile: + infile.write(self.data.encode()) + infile.flush() + # outfile will get overwritten by tool, so the delete + # may not work on some platforms. Do it manually. + outfile = tempfile.NamedTemporaryFile(delete=0) + try: + self.assertEqual( + self.runTool(args=[infile.name, outfile.name]), + ''.encode()) + with open(outfile.name, 'rb') as f: + self.assertEqual(f.read(), self.expect.encode()) + finally: + outfile.close() + os.unlink(outfile.name) diff --git a/simplejson/tool.py b/simplejson/tool.py index f00e592..35627db 100644 --- a/simplejson/tool.py +++ b/simplejson/tool.py @@ -18,21 +18,23 @@ def main(): infile = sys.stdin outfile = sys.stdout elif len(sys.argv) == 2: - infile = open(sys.argv[1], 'rb') + infile = open(sys.argv[1], 'r') outfile = sys.stdout elif len(sys.argv) == 3: - infile = open(sys.argv[1], 'rb') - outfile = open(sys.argv[2], 'wb') + infile = open(sys.argv[1], 'r') + outfile = open(sys.argv[2], 'w') else: raise SystemExit(sys.argv[0] + " [infile [outfile]]") - try: - obj = json.load(infile, - object_pairs_hook=json.OrderedDict, - use_decimal=True) - except ValueError: - raise SystemExit(sys.exc_info()[1]) - json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True) - outfile.write('\n') + with infile: + try: + obj = json.load(infile, + object_pairs_hook=json.OrderedDict, + use_decimal=True) + except ValueError: + raise SystemExit(sys.exc_info()[1]) + with outfile: + json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True) + outfile.write('\n') if __name__ == '__main__': -- cgit v1.2.1