8000 bpo-33927: Add support for same infile and outfile to json.tool by remilapeyre · Pull Request #7865 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-33927: Add support for same infile and outfile to json.tool #7865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Prev Previous commit
Next Next commit
Clean json_tools tests
  • Loading branch information
Rémi Lapeyre committed Nov 3, 2018
commit 48fd5619d866a30b65f4a1cfd5234cd464c38eb4
2 changes: 1 addition & 1 deletion Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def main():
try:
outfile = sys.stdout if options.outfile == '-' else open(options.outfile, 'w')
except IOError as e:
parser.error("can't open '{}': {}".format(options.outfile, str(e)))
parser.error(f"can't open '{options.outfile}': {str(e)}")
with outfile:
json.dump(obj, outfile, sort_keys=sort_keys, indent=4)
outfile.write('\n')
Expand Down
57 changes: 20 additions & 37 deletions Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def _create_infile(self):
def test_infile_stdout(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
self.assertEqual(err, b'')

Expand All @@ -91,14 +90,12 @@ def test_infile_outfile(self):
self.addCleanup(os.remove, outfile)
with open(outfile, "r") as fp:
self.assertEqual(fp.read(), self.expect)
self.assertEqual(rc, 0)
self.assertEqual(out, b'')
self.assertEqual(err, b'')

def test_infile_same_outfile(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', 'json.tool', infile, infile)
self.assertEqual(rc, 0)
self.assertEqual(out, b'')
self.assertEqual(err, b'')

Expand All @@ -107,40 +104,23 @@ def test_unavailable_outfile(self):
rc, out, err = assert_python_failure('-m', 'json.tool', infile, '/bla/outfile')
self.assertEqual(rc, 2)
self.assertEqual(out, b'')
err = err.decode().splitlines()
self.assertEqual(err[0], 'usage: python -m json.tool [-h] [--sort-keys] [infile] [outfile]')
self.assertEqual(err[1], "python -m json.tool: error: can't open '/bla/outfile': [Errno 2] No such file or directory: '/bla/outfile'")
self.assertIn(b"error: can't open '/bla/outfile': [Errno 2]", err)

def test_help_flag(self):
rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
self.assertEqual(rc, 0)
self.assertTrue(out.startswith(b'usage: '))
self.assertEqual(err, b'')

def test_sort_keys_flag(self):
infile = self._create_infile()
rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(),
self.expect_without_sort_keys.encode().splitlines())
self.assertEqual(err, b'')

def test_no_fd_leak_infile_outfile(self):
closed = []
opened = []
io_open = io.open

def open(*args, **kwargs):
fd = io_open(*args, **kwargs)
opened.append(fd)
fd_close = fd.close
def close(self):
closed.append(self)
fd_close()
fd.close = types.MethodType(close, fd)
return fd

infile = self._create_infile()
closed, opened, open = mock_open()
with mock.patch('builtins.open', side_effect=open):
with mock.patch.object(sys, 'argv', ['tool.py', infile, infile + '.out']):
import json.tool
Expand All @@ -151,28 +131,31 @@ def close(self):
self.assertEqual(len(opened), len(closed))

def test_no_fd_leak_same_infile_outfile(self):
closed = []
opened = []
io_open = io.open

def open(*args, **kwargs):
fd = io_open(*args, **kwargs)
opened.append(fd)
fd_close = fd.close
def close(self):
closed.append(self)
fd_close()
fd.close = types.MethodType(close, fd)
return fd

infile = self._create_infile()
closed, opened, open = mock_open()
with mock.patch('builtins.open', side_effect=open):
with mock.patch.object(sys, 'argv', ['tool.py', infile, infile]):
try:
import json.tool
json.tool.main()
except SystemExit: # We expect SystemExit to happen on c9d43c
except SystemExit:
pass

self.assertEqual(opened, closed)
self.assertEqual(len(opened), len(closed))

def mock_open():
closed = []
opened = []
io_open = io.open

def _open(*args, **kwargs):
fd = io_open(*args, **kwargs)
opened.append(fd)
fd_close = fd.close
def close(self):
closed.append(self)
fd_close()
fd.close = types.MethodType(close, fd)
return fd
return closed, opened, _open
0