mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
gh-122873: Allow "python -m json" to work (#122884)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
This commit is contained in:
parent
db6f5e1933
commit
906b796af8
8 changed files with 77 additions and 45 deletions
|
@ -11,7 +11,7 @@ from test.support.script_helper import assert_python_ok
|
|||
|
||||
|
||||
@support.requires_subprocess()
|
||||
class TestTool(unittest.TestCase):
|
||||
class TestMain(unittest.TestCase):
|
||||
data = """
|
||||
|
||||
[["blorpie"],[ "whoops" ] , [
|
||||
|
@ -19,6 +19,7 @@ class TestTool(unittest.TestCase):
|
|||
"i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
|
||||
:"yes"} ]
|
||||
"""
|
||||
module = 'json'
|
||||
|
||||
expect_without_sort_keys = textwrap.dedent("""\
|
||||
[
|
||||
|
@ -87,7 +88,7 @@ class TestTool(unittest.TestCase):
|
|||
""")
|
||||
|
||||
def test_stdin_stdout(self):
|
||||
args = sys.executable, '-m', 'json.tool'
|
||||
args = sys.executable, '-m', self.module
|
||||
process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(process.stdout, self.expect)
|
||||
self.assertEqual(process.stderr, '')
|
||||
|
@ -101,7 +102,7 @@ class TestTool(unittest.TestCase):
|
|||
|
||||
def test_infile_stdout(self):
|
||||
infile = self._create_infile()
|
||||
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
|
||||
rc, out, err = assert_python_ok('-m', self.module, infile)
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
|
||||
self.assertEqual(err, b'')
|
||||
|
@ -115,7 +116,7 @@ class TestTool(unittest.TestCase):
|
|||
''').encode()
|
||||
|
||||
infile = self._create_infile(data)
|
||||
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
|
||||
rc, out, err = assert_python_ok('-m', self.module, infile)
|
||||
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out.splitlines(), expect.splitlines())
|
||||
|
@ -124,7 +125,7 @@ class TestTool(unittest.TestCase):
|
|||
def test_infile_outfile(self):
|
||||
infile = self._create_infile()
|
||||
outfile = os_helper.TESTFN + '.out'
|
||||
rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile)
|
||||
rc, out, err = assert_python_ok('-m', self.module, infile, outfile)
|
||||
self.addCleanup(os.remove, outfile)
|
||||
with open(outfile, "r", encoding="utf-8") as fp:
|
||||
self.assertEqual(fp.read(), self.expect)
|
||||
|
@ -134,7 +135,7 @@ class TestTool(unittest.TestCase):
|
|||
|
||||
def test_writing_in_place(self):
|
||||
infile = self._create_infile()
|
||||
rc, out, err = assert_python_ok('-m', 'json.tool', infile, infile)
|
||||
rc, out, err = assert_python_ok('-m', self.module, infile, infile)
|
||||
with open(infile, "r", encoding="utf-8") as fp:
|
||||
self.assertEqual(fp.read(), self.expect)
|
||||
self.assertEqual(rc, 0)
|
||||
|
@ -142,20 +143,20 @@ class TestTool(unittest.TestCase):
|
|||
self.assertEqual(err, b'')
|
||||
|
||||
def test_jsonlines(self):
|
||||
args = sys.executable, '-m', 'json.tool', '--json-lines'
|
||||
args = sys.executable, '-m', self.module, '--json-lines'
|
||||
process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(process.stdout, self.jsonlines_expect)
|
||||
self.assertEqual(process.stderr, '')
|
||||
|
||||
def test_help_flag(self):
|
||||
rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
|
||||
rc, out, err = assert_python_ok('-m', self.module, '-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)
|
||||
rc, out, err = assert_python_ok('-m', self.module, '--sort-keys', infile)
|
||||
self.assertEqual(rc, 0)
|
||||
self.assertEqual(out.splitlines(),
|
||||
self.expect_without_sort_keys.encode().splitlines())
|
||||
|
@ -169,7 +170,7 @@ class TestTool(unittest.TestCase):
|
|||
2
|
||||
]
|
||||
''')
|
||||
args = sys.executable, '-m', 'json.tool', '--indent', '2'
|
||||
args = sys.executable, '-m', self.module, '--indent', '2'
|
||||
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(process.stdout, expect)
|
||||
self.assertEqual(process.stderr, '')
|
||||
|
@ -177,7 +178,7 @@ class TestTool(unittest.TestCase):
|
|||
def test_no_indent(self):
|
||||
input_ = '[1,\n2]'
|
||||
expect = '[1, 2]\n'
|
||||
args = sys.executable, '-m', 'json.tool', '--no-indent'
|
||||
args = sys.executable, '-m', self.module, '--no-indent'
|
||||
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(process.stdout, expect)
|
||||
self.assertEqual(process.stderr, '')
|
||||
|
@ -185,7 +186,7 @@ class TestTool(unittest.TestCase):
|
|||
def test_tab(self):
|
||||
input_ = '[1, 2]'
|
||||
expect = '[\n\t1,\n\t2\n]\n'
|
||||
args = sys.executable, '-m', 'json.tool', '--tab'
|
||||
args = sys.executable, '-m', self.module, '--tab'
|
||||
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(process.stdout, expect)
|
||||
self.assertEqual(process.stderr, '')
|
||||
|
@ -193,7 +194,7 @@ class TestTool(unittest.TestCase):
|
|||
def test_compact(self):
|
||||
input_ = '[ 1 ,\n 2]'
|
||||
expect = '[1,2]\n'
|
||||
args = sys.executable, '-m', 'json.tool', '--compact'
|
||||
args = sys.executable, '-m', self.module, '--compact'
|
||||
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
|
||||
self.assertEqual(process.stdout, expect)
|
||||
self.assertEqual(process.stderr, '')
|
||||
|
@ -202,7 +203,7 @@ class TestTool(unittest.TestCase):
|
|||
infile = self._create_infile('{"key":"💩"}')
|
||||
outfile = os_helper.TESTFN + '.out'
|
||||
self.addCleanup(os.remove, outfile)
|
||||
assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile, outfile)
|
||||
assert_python_ok('-m', self.module, '--no-ensure-ascii', infile, outfile)
|
||||
with open(outfile, "rb") as f:
|
||||
lines = f.read().splitlines()
|
||||
# asserting utf-8 encoded output file
|
||||
|
@ -213,7 +214,7 @@ class TestTool(unittest.TestCase):
|
|||
infile = self._create_infile('{"key":"💩"}')
|
||||
outfile = os_helper.TESTFN + '.out'
|
||||
self.addCleanup(os.remove, outfile)
|
||||
assert_python_ok('-m', 'json.tool', infile, outfile)
|
||||
assert_python_ok('-m', self.module, infile, outfile)
|
||||
with open(outfile, "rb") as f:
|
||||
lines = f.read().splitlines()
|
||||
# asserting an ascii encoded output file
|
||||
|
@ -222,11 +223,16 @@ class TestTool(unittest.TestCase):
|
|||
|
||||
@unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
|
||||
def test_broken_pipe_error(self):
|
||||
cmd = [sys.executable, '-m', 'json.tool']
|
||||
cmd = [sys.executable, '-m', self.module]
|
||||
proc = subprocess.Popen(cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stdin=subprocess.PIPE)
|
||||
# bpo-39828: Closing before json.tool attempts to write into stdout.
|
||||
# bpo-39828: Closing before json attempts to write into stdout.
|
||||
proc.stdout.close()
|
||||
proc.communicate(b'"{}"')
|
||||
self.assertEqual(proc.returncode, errno.EPIPE)
|
||||
|
||||
|
||||
@support.requires_subprocess()
|
||||
class TestTool(TestMain):
|
||||
module = 'json.tool'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue