Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
seamile committed Jun 13, 2023
1 parent 827316d commit 4e6a264
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
19 changes: 12 additions & 7 deletions jsonfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def format_to_text(py_obj: Any, fmt: str, *,
if fmt == 'json':
if compact:
return json.dumps(py_obj, ensure_ascii=escape, sort_keys=sort_keys,
separators=(',', ':'))
separators=(',', ':')) + '\n'
else:
return json.dumps(py_obj, ensure_ascii=escape, sort_keys=sort_keys,
indent=indent)
indent=indent) + '\n'

elif fmt == 'toml':
if not isinstance(py_obj, dict):
Expand Down Expand Up @@ -205,9 +205,12 @@ def output(output_fp: IO, text: str, fmt: str, cp2clip: bool):
else:
output_fp.write(colored_text)
else:
output_fp.seek(0)
output_fp.truncate()
if output_fp.fileno() > 2:
output_fp.seek(0)
output_fp.truncate()

output_fp.write(text)

if output_fp.fileno() > 2:
print_inf(f'result written to {output_fp.name}')

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

files = args.files or [stdin]
files_cnt = len(files)

for file in files:
for num, file in enumerate(files, start=1):
try:
# read from file
input_fp = open(file, 'r+') if isinstance(file, str) else file
Expand All @@ -321,8 +325,9 @@ def main():
sort_keys=args.sort_keys,
sets=sets,
pops=pops)
# output a blank line to separate multiple results
print()
# output a line to separate multiple results
if num < files_cnt:
print('----------------', file=stdout)
except (FormatError, JMESPathError, JSONPathError) as err:
print_err(err)
except FileNotFoundError:
Expand Down
10 changes: 6 additions & 4 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ def test_format_to_text(self):
j_compacted = jsonfmt.format_to_text(py_obj, 'json',
compact=True, escape=True,
indent=4, sort_keys=False)
self.assertEqual(j_compacted, '{"name":"\\u7ea6\\u7ff0","age":30}')
self.assertEqual(j_compacted.strip(), '{"name":"\\u7ea6\\u7ff0","age":30}')

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

# format to toml
toml_text = jsonfmt.format_to_text(self.py_obj, 'toml',
Expand Down Expand Up @@ -342,10 +342,12 @@ def test_parse_cmdline_args(self):
############################################################################

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

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

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

0 comments on commit 4e6a264

Please sign in to comment.