From c17011ef21c78022accb9cd2b03447e3ebe2ff37 Mon Sep 17 00:00:00 2001 From: "SU, Yanyu" <238259854+TohruSu@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:15:37 +0900 Subject: [PATCH] Treat an empty -c string as the input instead of reading stdin --- json5/tool.py | 2 +- tests/tool_test.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/json5/tool.py b/json5/tool.py index 05b27b2..f4b6bdb 100644 --- a/json5/tool.py +++ b/json5/tool.py @@ -45,7 +45,7 @@ def main(argv=None, host=None): host.print(__version__) return 0 - if args.cmd: + if args.cmd is not None: inp = args.cmd elif args.file == '-': inp = host.stdin.read() diff --git a/tests/tool_test.py b/tests/tool_test.py index 81934f6..647b368 100644 --- a/tests/tool_test.py +++ b/tests/tool_test.py @@ -200,6 +200,18 @@ def test_keys_are_quoted_by_default(self): def test_read_command(self): self.check(['-c', '"foo"'], out='"foo"\n') + def test_read_empty_command(self): + # An empty -c string is still the input; stdin and FILE are not read. + err = 'Empty strings are not legal JSON5\n' + self.check(['-c', ''], stdin='"foo"', returncode=1, out='', err=err) + self.check( + ['-c', '', 'foo.json5'], + files={'foo.json5': '"foo"\n'}, + returncode=1, + out='', + err=err, + ) + def test_read_from_stdin(self): self.check([], stdin='"foo"\n', out='"foo"\n')