From ef5439ee80e6ef889853fb984bc007d214143be3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 10 Jul 2024 10:08:09 +0200 Subject: [PATCH] refs #12918 - added test showing `-D` being lost when specified before cppcheck GUI project on command-line (#6578) --- test/cli/more-projects_test.py | 92 +++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index c2a6b9edc1f..2830d577abb 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -752,4 +752,94 @@ def test_json_file_ignore_2(tmpdir): 'cppcheck: all paths were ignored' ] - assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines) \ No newline at end of file + assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines) + + +@pytest.mark.xfail(strict=True) +def test_project_D(tmpdir): + test_file = os.path.join(tmpdir, 'test.cpp') + with open(test_file, 'wt') as f: + f.write(""" +#ifndef __GNUC__ +#error "requirement not met" +#endif + """) + + project_file = os.path.join(tmpdir, 'test.cppcheck') + with open(project_file, 'wt') as f: + f.write( + """ + + + + + + + """.format(test_file)) + + args = [ + '--project=' + project_file, + '--template=simple', + ] + arg_D = ['-D__GNUC__'] + + out_expected = [ + 'Checking {} ...'.format(test_file), + 'Checking {}: __GNUC__=1...'.format(test_file) + ] + + args1 = args + arg_D + ret, stdout, stderr = cppcheck(args1) + assert stdout.splitlines() == out_expected + assert stderr.splitlines() == [] + assert ret == 0, stdout + + # TODO: -D__GNUC__ is lost + args2 = arg_D + args + ret, stdout, stderr = cppcheck(args2) + assert stdout.splitlines() == out_expected + assert stderr.splitlines() == [] + assert ret == 0, stdout + + +def test_compdb_D(tmpdir): + test_file = os.path.join(tmpdir, 'test.cpp') + with open(test_file, 'wt') as f: + f.write(""" +#ifndef __GNUC__ +#error "requirement not met" +#endif + """) + + compile_commands = os.path.join(tmpdir, 'compile_commands.json') + compilation_db = [ + {"directory": str(tmpdir), + "command": "c++ -o test.o -c test.cpp", + "file": "test.cpp", + "output": "test.o"} + ] + with open(compile_commands, 'wt') as f: + f.write(json.dumps(compilation_db)) + + args = [ + '--project=' + compile_commands, + '--template=simple', + ] + arg_D = ['-D__GNUC__'] + + out_expected = [ + 'Checking {} ...'.format(test_file), + 'Checking {}: __GNUC__=1;...'.format(test_file) # TODO: get rid of extra ; + ] + + args1 = args + arg_D + ret, stdout, stderr = cppcheck(args1) + assert stdout.splitlines() == out_expected + assert stderr.splitlines() == [] + assert ret == 0, stdout + + args2 = arg_D + args + ret, stdout, stderr = cppcheck(args2) + assert stdout.splitlines() == out_expected + assert stderr.splitlines() == [] + assert ret == 0, stdout