| 1 |
import errno |
| 2 |
import pathlib |
| 3 |
import os |
| 4 |
import sys |
| 5 |
import textwrap |
| 6 |
import unittest |
| 7 |
import subprocess |
| 8 |
|
| 9 |
from test import support |
| 10 |
from test.support import force_not_colorized, os_helper |
| 11 |
from test.support.script_helper import assert_python_ok |
| 12 |
|
| 13 |
|
| 14 |
@support.requires_subprocess() |
| 15 |
class TestTool(unittest.TestCase): |
| 16 |
data = """ |
| 17 |
|
| 18 |
[["blorpie"],[ "whoops" ] , [ |
| 19 |
],\t"d-shtaeou",\r"d-nthiouh", |
| 20 |
"i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" |
| 21 |
:"yes"} ] |
| 22 |
""" |
| 23 |
|
| 24 |
expect_without_sort_keys = textwrap.dedent("""\ |
| 25 |
[ |
| 26 |
[ |
| 27 |
"blorpie" |
| 28 |
], |
| 29 |
[ |
| 30 |
"whoops" |
| 31 |
], |
| 32 |
[], |
| 33 |
"d-shtaeou", |
| 34 |
"d-nthiouh", |
| 35 |
"i-vhbjkhnth", |
| 36 |
{ |
| 37 |
"nifty": 87 |
| 38 |
}, |
| 39 |
{ |
| 40 |
"field": "yes", |
| 41 |
"morefield": false |
| 42 |
} |
| 43 |
] |
| 44 |
""") |
| 45 |
|
| 46 |
expect = textwrap.dedent("""\ |
| 47 |
[ |
| 48 |
[ |
| 49 |
"blorpie" |
| 50 |
], |
| 51 |
[ |
| 52 |
"whoops" |
| 53 |
], |
| 54 |
[], |
| 55 |
"d-shtaeou", |
| 56 |
"d-nthiouh", |
| 57 |
"i-vhbjkhnth", |
| 58 |
{ |
| 59 |
"nifty": 87 |
| 60 |
}, |
| 61 |
{ |
| 62 |
"morefield": false, |
| 63 |
"field": "yes" |
| 64 |
} |
| 65 |
] |
| 66 |
""") |
| 67 |
|
| 68 |
jsonlines_raw = textwrap.dedent("""\ |
| 69 |
{"ingredients":["frog", "water", "chocolate", "glucose"]} |
| 70 |
{"ingredients":["chocolate","steel bolts"]} |
| 71 |
""") |
| 72 |
|
| 73 |
jsonlines_expect = textwrap.dedent("""\ |
| 74 |
{ |
| 75 |
"ingredients": [ |
| 76 |
"frog", |
| 77 |
"water", |
| 78 |
"chocolate", |
| 79 |
"glucose" |
| 80 |
] |
| 81 |
} |
| 82 |
{ |
| 83 |
"ingredients": [ |
| 84 |
"chocolate", |
| 85 |
"steel bolts" |
| 86 |
] |
| 87 |
} |
| 88 |
""") |
| 89 |
|
| 90 |
def test_stdin_stdout(self): |
| 91 |
args = sys.executable, '-m', 'json.tool' |
| 92 |
process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True) |
| 93 |
self.assertEqual(process.stdout, self.expect) |
| 94 |
self.assertEqual(process.stderr, '') |
| 95 |
|
| 96 |
def _create_infile(self, data=None): |
| 97 |
infile = os_helper.TESTFN |
| 98 |
with open(infile, "w", encoding="utf-8") as fp: |
| 99 |
self.addCleanup(os.remove, infile) |
| 100 |
fp.write(data or self.data) |
| 101 |
return infile |
| 102 |
|
| 103 |
def test_infile_stdout(self): |
| 104 |
infile = self._create_infile() |
| 105 |
rc, out, err = assert_python_ok('-m', 'json.tool', infile) |
| 106 |
self.assertEqual(rc, 0) |
| 107 |
self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) |
| 108 |
self.assertEqual(err, b'') |
| 109 |
|
| 110 |
def test_non_ascii_infile(self): |
| 111 |
data = '{"msg": "\u3053\u3093\u306b\u3061\u306f"}' |
| 112 |
expect = textwrap.dedent('''\ |
| 113 |
{ |
| 114 |
"msg": "\\u3053\\u3093\\u306b\\u3061\\u306f" |
| 115 |
} |
| 116 |
''').encode() |
| 117 |
|
| 118 |
infile = self._create_infile(data) |
| 119 |
rc, out, err = assert_python_ok('-m', 'json.tool', infile) |
| 120 |
|
| 121 |
self.assertEqual(rc, 0) |
| 122 |
self.assertEqual(out.splitlines(), expect.splitlines()) |
| 123 |
self.assertEqual(err, b'') |
| 124 |
|
| 125 |
def test_infile_outfile(self): |
| 126 |
infile = self._create_infile() |
| 127 |
outfile = os_helper.TESTFN + '.out' |
| 128 |
rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile) |
| 129 |
self.addCleanup(os.remove, outfile) |
| 130 |
with open(outfile, "r", encoding="utf-8") as fp: |
| 131 |
self.assertEqual(fp.read(), self.expect) |
| 132 |
self.assertEqual(rc, 0) |
| 133 |
self.assertEqual(out, b'') |
| 134 |
self.assertEqual(err, b'') |
| 135 |
|
| 136 |
def test_writing_in_place(self): |
| 137 |
infile = self._create_infile() |
| 138 |
rc, out, err = assert_python_ok('-m', 'json.tool', infile, infile) |
| 139 |
with open(infile, "r", encoding="utf-8") as fp: |
| 140 |
self.assertEqual(fp.read(), self.expect) |
| 141 |
self.assertEqual(rc, 0) |
| 142 |
self.assertEqual(out, b'') |
| 143 |
self.assertEqual(err, b'') |
| 144 |
|
| 145 |
def test_jsonlines(self): |
| 146 |
args = sys.executable, '-m', 'json.tool', '--json-lines' |
| 147 |
process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True) |
| 148 |
self.assertEqual(process.stdout, self.jsonlines_expect) |
| 149 |
self.assertEqual(process.stderr, '') |
| 150 |
|
| 151 |
@force_not_colorized |
| 152 |
def test_jsonlines_from_file(self): |
| 153 |
jsonl = pathlib.Path(__file__).parent / 'json_lines.jsonl' |
| 154 |
args = sys.executable, '-m', 'json.tool', '--json-lines', jsonl |
| 155 |
process = subprocess.run(args, capture_output=True, text=True, check=True) |
| 156 |
self.assertEqual(process.stdout, self.jsonlines_expect) |
| 157 |
self.assertEqual(process.stderr, '') |
| 158 |
|
| 159 |
def test_help_flag(self): |
| 160 |
rc, out, err = assert_python_ok('-m', 'json.tool', '-h') |
| 161 |
self.assertEqual(rc, 0) |
| 162 |
self.assertTrue(out.startswith(b'usage: ')) |
| 163 |
self.assertEqual(err, b'') |
| 164 |
|
| 165 |
def test_sort_keys_flag(self): |
| 166 |
infile = self._create_infile() |
| 167 |
rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile) |
| 168 |
self.assertEqual(rc, 0) |
| 169 |
self.assertEqual(out.splitlines(), |
| 170 |
self.expect_without_sort_keys.encode().splitlines()) |
| 171 |
self.assertEqual(err, b'') |
| 172 |
|
| 173 |
def test_indent(self): |
| 174 |
input_ = '[1, 2]' |
| 175 |
expect = textwrap.dedent('''\ |
| 176 |
[ |
| 177 |
1, |
| 178 |
2 |
| 179 |
] |
| 180 |
''') |
| 181 |
args = sys.executable, '-m', 'json.tool', '--indent', '2' |
| 182 |
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True) |
| 183 |
self.assertEqual(process.stdout, expect) |
| 184 |
self.assertEqual(process.stderr, '') |
| 185 |
|
| 186 |
def test_no_indent(self): |
| 187 |
input_ = '[1,\n2]' |
| 188 |
expect = '[1, 2]\n' |
| 189 |
args = sys.executable, '-m', 'json.tool', '--no-indent' |
| 190 |
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True) |
| 191 |
self.assertEqual(process.stdout, expect) |
| 192 |
self.assertEqual(process.stderr, '') |
| 193 |
|
| 194 |
def test_tab(self): |
| 195 |
input_ = '[1, 2]' |
| 196 |
expect = '[\n\t1,\n\t2\n]\n' |
| 197 |
args = sys.executable, '-m', 'json.tool', '--tab' |
| 198 |
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True) |
| 199 |
self.assertEqual(process.stdout, expect) |
| 200 |
self.assertEqual(process.stderr, '') |
| 201 |
|
| 202 |
def test_compact(self): |
| 203 |
input_ = '[ 1 ,\n 2]' |
| 204 |
expect = '[1,2]\n' |
| 205 |
args = sys.executable, '-m', 'json.tool', '--compact' |
| 206 |
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True) |
| 207 |
self.assertEqual(process.stdout, expect) |
| 208 |
self.assertEqual(process.stderr, '') |
| 209 |
|
| 210 |
def test_no_ensure_ascii_flag(self): |
| 211 |
infile = self._create_infile('{"key":"💩"}') |
| 212 |
outfile = os_helper.TESTFN + '.out' |
| 213 |
self.addCleanup(os.remove, outfile) |
| 214 |
assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile, outfile) |
| 215 |
with open(outfile, "rb") as f: |
| 216 |
lines = f.read().splitlines() |
| 217 |
# asserting utf-8 encoded output file |
| 218 |
expected = [b'{', b' "key": "\xf0\x9f\x92\xa9"', b"}"] |
| 219 |
self.assertEqual(lines, expected) |
| 220 |
|
| 221 |
def test_ensure_ascii_default(self): |
| 222 |
infile = self._create_infile('{"key":"💩"}') |
| 223 |
outfile = os_helper.TESTFN + '.out' |
| 224 |
self.addCleanup(os.remove, outfile) |
| 225 |
assert_python_ok('-m', 'json.tool', infile, outfile) |
| 226 |
with open(outfile, "rb") as f: |
| 227 |
lines = f.read().splitlines() |
| 228 |
# asserting an ascii encoded output file |
| 229 |
expected = [b'{', rb' "key": "\ud83d\udca9"', b"}"] |
| 230 |
self.assertEqual(lines, expected) |
| 231 |
|
| 232 |
@unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows") |
| 233 |
def test_broken_pipe_error(self): |
| 234 |
cmd = [sys.executable, '-m', 'json.tool'] |
| 235 |
proc = subprocess.Popen(cmd, |
| 236 |
stdout=subprocess.PIPE, |
| 237 |
stdin=subprocess.PIPE) |
| 238 |
# bpo-39828: Closing before json.tool attempts to write into stdout. |
| 239 |
proc.stdout.close() |
| 240 |
proc.communicate(b'"{}"') |
| 241 |
self.assertEqual(proc.returncode, errno.EPIPE) |