From 35264d4dbef86656cfb0ba7e300a0eb5c9318371 Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Sat, 7 Nov 2026 07:31:32 +0900 Subject: [PATCH 1/4] test: run the jsontestrunner command without a shell --- test/runjsontests.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/test/runjsontests.py b/test/runjsontests.py index 14275ec22..df1f231e5 100644 --- a/test/runjsontests.py +++ b/test/runjsontests.py @@ -11,26 +11,23 @@ import os import os.path import optparse +import subprocess VALGRIND_CMD = 'valgrind --tool=memcheck --leak-check=yes --undef-value-errors=yes ' def getStatusOutput(cmd): """ - Return int, unicode (for both Python 2 and 3). - Note: os.popen().close() would return None for 0. + Return exit status and text output. Zero means success. """ print(cmd, file=sys.stderr) - pipe = os.popen(cmd) - process_output = pipe.read() try: - # We have been using os.popen(). When we read() the result - # we get 'str' (bytes) in py2, and 'str' (unicode) in py3. - # Ugh! There must be a better way to handle this. - process_output = process_output.decode('utf-8') - except AttributeError: - pass # python3 - status = pipe.close() - return status, process_output + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, + universal_newlines=True) + except OSError as error: + print(error, file=sys.stderr) + return 1, '' + process_output = process.communicate()[0] + return process.returncode, process_output def compareOutputs(expected, actual, message): expected = expected.strip().replace('\r','').split('\n') actual = actual.strip().replace('\r','').split('\n') @@ -107,18 +104,19 @@ def runAllTests(jsontest_executable_path, input_path = None, ] failed_tests = [] - valgrind_path = use_valgrind and VALGRIND_CMD or '' + valgrind_args = VALGRIND_CMD.split() if use_valgrind else [] for input_path in tests: expect_failure = os.path.basename(input_path).startswith('fail') is_json_checker_test = os.path.basename(os.path.dirname(input_path)) == "jsonchecker" is_parse_only = is_json_checker_test or expect_failure is_strict_test = ('_strict_' in os.path.basename(input_path)) or is_json_checker_test print('TESTING:', input_path, end=' ') - options = is_parse_only and '--parse-only' or '' - options += is_strict_test and ' --strict' or '' - options += ' --json-writer %s'%writerClass - cmd = '%s%s %s "%s"' % ( valgrind_path, jsontest_executable_path, options, - input_path) + cmd = valgrind_args + [jsontest_executable_path] + if is_parse_only: + cmd.append('--parse-only') + if is_strict_test: + cmd.append('--strict') + cmd.extend(['--json-writer', writerClass, input_path]) status, process_output = getStatusOutput(cmd) if is_parse_only: if expect_failure: From 73a722dcefc2a66f4271960aa96017e7b58baedf Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Sat, 7 Nov 2026 07:31:53 +0900 Subject: [PATCH 2/4] test: register the runner argument tests with CTest --- src/jsontestrunner/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/jsontestrunner/CMakeLists.txt b/src/jsontestrunner/CMakeLists.txt index 7f536d318..28be28d71 100644 --- a/src/jsontestrunner/CMakeLists.txt +++ b/src/jsontestrunner/CMakeLists.txt @@ -49,6 +49,10 @@ if(PYTHONINTERP_FOUND) WORKING_DIRECTORY "${TEST_DIR}/data" ) + add_test(NAME jsoncpp_runner_arguments + COMMAND "${PYTHON_EXECUTABLE}" -B "${TEST_DIR}/test_runjsontests.py" + ) + # Both tests write .actual/.actual-rewrite along with test data, need to prevent collision when running tests via ctest -j set_tests_properties(jsoncpp_readerwriter jsoncpp_readerwriter_json_checker PROPERTIES RESOURCE_LOCK "test_data_files" From ec8fb69d9553eebb4d2a58c5e18151e82615a26e Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Sat, 7 Nov 2026 07:32:12 +0900 Subject: [PATCH 3/4] test: register the runner argument tests with Meson --- meson.build | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/meson.build b/meson.build index 858675d9e..855b597d4 100644 --- a/meson.build +++ b/meson.build @@ -155,3 +155,9 @@ test( join_paths(meson.current_source_dir(), 'test/data')], workdir : join_paths(meson.current_source_dir(), 'test/data'), ) + +test( + 'runner_arguments', + python, + args : ['-B', join_paths(meson.current_source_dir(), 'test/test_runjsontests.py')], + ) From 2ab9fef3b8ce5a315c38ac6e4f6e785046ba6a9a Mon Sep 17 00:00:00 2001 From: Lee Sang Gyu Date: Sat, 7 Nov 2026 07:32:41 +0900 Subject: [PATCH 4/4] test: add unit tests for the jsontestrunner command builder --- test/test_runjsontests.py | 101 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 test/test_runjsontests.py diff --git a/test/test_runjsontests.py b/test/test_runjsontests.py new file mode 100644 index 000000000..bd0fafe6d --- /dev/null +++ b/test/test_runjsontests.py @@ -0,0 +1,101 @@ +# Copyright 2026 The JsonCpp Authors +# Distributed under MIT license, or public domain if desired and recognized. +# See file LICENSE for details. + +import contextlib +import io +import os +import sys +import tempfile +import unittest +from unittest import mock + +import runjsontests + + +class RunnerTest(unittest.TestCase): + def test_arguments_are_not_split(self): + with contextlib.redirect_stderr(io.StringIO()): + status, output = runjsontests.getStatusOutput([ + sys.executable, '-c', + 'import sys; print(repr(sys.argv[1:]))', + 'argument with spaces', 'quote"value', 'single\'quote']) + self.assertEqual(status, 0) + self.assertEqual(output.strip(), + repr(['argument with spaces', 'quote"value', + 'single\'quote'])) + + def test_exit_status_and_output(self): + with contextlib.redirect_stderr(io.StringIO()): + status, output = runjsontests.getStatusOutput([ + sys.executable, '-c', + 'import sys; print("output"); sys.exit(7)']) + self.assertEqual(status, 7) + self.assertEqual(output, 'output\n') + + def test_missing_executable_returns_failure(self): + with tempfile.TemporaryDirectory() as directory: + missing = os.path.join(directory, 'missing executable') + with contextlib.redirect_stderr(io.StringIO()): + status, output = runjsontests.getStatusOutput([missing]) + self.assertTrue(status) + self.assertEqual(output, '') + + @unittest.skipIf(os.name == 'nt', 'POSIX executable permission test') + def test_non_executable_file_returns_failure(self): + with tempfile.TemporaryDirectory() as directory: + path = os.path.join(directory, 'not executable') + with open(path, 'w') as executable: + executable.write('not executable') + os.chmod(path, 0o600) + with contextlib.redirect_stderr(io.StringIO()): + status, output = runjsontests.getStatusOutput([path]) + self.assertTrue(status) + self.assertEqual(output, '') + + def test_missing_executable_does_not_stop_remaining_fixtures(self): + with tempfile.TemporaryDirectory() as directory: + missing = os.path.join(directory, 'missing executable') + for name in ['first.json', 'second.json']: + with open(os.path.join(directory, name), 'w') as fixture: + fixture.write('{}') + output = io.StringIO() + with contextlib.redirect_stdout(output): + with contextlib.redirect_stderr(io.StringIO()): + with self.assertRaises(runjsontests.FailError): + runjsontests.runAllTests(missing, directory) + self.assertIn('first.json', output.getvalue()) + self.assertIn('second.json', output.getvalue()) + self.assertIn('Test results: 0 passed, 2 failed.', output.getvalue()) + + def check_command(self, input_path, expected_options, use_valgrind=False): + executable = os.path.join('build space', 'runner executable') + with mock.patch.object(runjsontests, 'getStatusOutput', + return_value=(1, 'expected parse failure')) as run: + with contextlib.redirect_stdout(io.StringIO()): + runjsontests.runAllTests(executable, input_path, + use_valgrind=use_valgrind) + expected = runjsontests.VALGRIND_CMD.split() if use_valgrind else [] + expected += [executable] + expected_options + expected += ['--json-writer', 'StyledWriter', input_path] + run.assert_called_once_with(expected) + + def test_parse_only_keeps_spaced_paths(self): + self.check_command(os.path.join('fixture space', 'fail_example.json'), + ['--parse-only']) + + def test_strict_parse_only_options(self): + self.check_command(os.path.join('fixture space', 'fail_strict_case.json'), + ['--parse-only', '--strict']) + + def test_json_checker_options(self): + self.check_command(os.path.join('jsonchecker', 'fail1.json'), + ['--parse-only', '--strict']) + + def test_valgrind_prefix(self): + self.check_command(os.path.join('fixture space', 'fail_example.json'), + ['--parse-only'], use_valgrind=True) + + +if __name__ == '__main__': + unittest.main()