Skip to content

Commit 4e6a264

Browse files
committed
fix bug
1 parent 827316d commit 4e6a264

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

jsonfmt.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ def format_to_text(py_obj: Any, fmt: str, *,
167167
if fmt == 'json':
168168
if compact:
169169
return json.dumps(py_obj, ensure_ascii=escape, sort_keys=sort_keys,
170-
separators=(',', ':'))
170+
separators=(',', ':')) + '\n'
171171
else:
172172
return json.dumps(py_obj, ensure_ascii=escape, sort_keys=sort_keys,
173-
indent=indent)
173+
indent=indent) + '\n'
174174

175175
elif fmt == 'toml':
176176
if not isinstance(py_obj, dict):
@@ -205,9 +205,12 @@ def output(output_fp: IO, text: str, fmt: str, cp2clip: bool):
205205
else:
206206
output_fp.write(colored_text)
207207
else:
208-
output_fp.seek(0)
209-
output_fp.truncate()
208+
if output_fp.fileno() > 2:
209+
output_fp.seek(0)
210+
output_fp.truncate()
211+
210212
output_fp.write(text)
213+
211214
if output_fp.fileno() > 2:
212215
print_inf(f'result written to {output_fp.name}')
213216

@@ -304,8 +307,9 @@ def main():
304307
pops = [k.strip() for k in args.pop.split(';')] if args.pop else []
305308

306309
files = args.files or [stdin]
310+
files_cnt = len(files)
307311

308-
for file in files:
312+
for num, file in enumerate(files, start=1):
309313
try:
310314
# read from file
311315
input_fp = open(file, 'r+') if isinstance(file, str) else file
@@ -321,8 +325,9 @@ def main():
321325
sort_keys=args.sort_keys,
322326
sets=sets,
323327
pops=pops)
324-
# output a blank line to separate multiple results
325-
print()
328+
# output a line to separate multiple results
329+
if num < files_cnt:
330+
print('----------------', file=stdout)
326331
except (FormatError, JMESPathError, JSONPathError) as err:
327332
print_err(err)
328333
except FileNotFoundError:

test/test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ def test_format_to_text(self):
228228
j_compacted = jsonfmt.format_to_text(py_obj, 'json',
229229
compact=True, escape=True,
230230
indent=4, sort_keys=False)
231-
self.assertEqual(j_compacted, '{"name":"\\u7ea6\\u7ff0","age":30}')
231+
self.assertEqual(j_compacted.strip(), '{"name":"\\u7ea6\\u7ff0","age":30}')
232232

233233
# format to json (indentation)
234234
j_indented = jsonfmt.format_to_text(py_obj, 'json',
235235
compact=False, escape=False,
236236
indent=4, sort_keys=True)
237-
self.assertEqual(j_indented, '{\n "age": 30,\n "name": "约翰"\n}')
237+
self.assertEqual(j_indented.strip(), '{\n "age": 30,\n "name": "约翰"\n}')
238238

239239
# format to toml
240240
toml_text = jsonfmt.format_to_text(self.py_obj, 'toml',
@@ -342,10 +342,12 @@ def test_parse_cmdline_args(self):
342342
############################################################################
343343

344344
@patch.multiple(sys, argv=['jsonfmt', '-i', 't', '-p', 'actions[*].name',
345-
JSON_FILE])
345+
JSON_FILE, YAML_FILE])
346346
@patch.multiple(jsonfmt, stdout=FakeStdOut())
347347
def test_main_with_file(self):
348348
expected_output = color('[\n\t"eat",\n\t"sport"\n]', 'json')
349+
expected_output += '----------------\n'
350+
expected_output += color('- eat\n- sport', 'yaml')
349351
jsonfmt.main()
350352
self.assertEqual(jsonfmt.stdout.read(), expected_output)
351353

@@ -403,7 +405,7 @@ def test_main_convert(self):
403405
@patch.multiple(jsonfmt, stdin=FakeStdIn('{"a": "asfd", "b": [1, 2, 3]}'), stdout=FakeStdOut(tty=False))
404406
def test_main_overview(self):
405407
jsonfmt.main()
406-
self.assertEqual(jsonfmt.stdout.read(), '{"a":"...","b":[]}')
408+
self.assertEqual(jsonfmt.stdout.read().strip(), '{"a":"...","b":[]}')
407409

408410
@patch('sys.argv', ['jsonfmt', '-Ocsf', 'json', TOML_FILE])
409411
def test_main_overwrite_to_original_file(self):

0 commit comments

Comments
 (0)