From de9c999d79f497e9e3dfba6c9b36ad48fcb1233b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Jun 2019 19:20:15 +0200 Subject: [PATCH] Better handling of spaces in paths --- .travis.yml | 5 ++-- lib/cppcheck.cpp | 2 +- test/cli/test-helloworld.py | 38 +++++++++++++-------------- test/cli/test-inline-suppress.py | 4 +-- test/cli/test-proj2.py | 20 +++++++------- test/cli/test-suppress-syntaxError.py | 4 +-- test/cli/testutils.py | 13 ++++----- 7 files changed, 44 insertions(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index ce9ce3a32..9d691c51e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -245,9 +245,10 @@ script: - make -s -j2 - cd ../../ # Testing cli - - cd test/cli + - cp -R . ../cppcheck\ 2 + - cd ../cppcheck\ 2/test/cli # path with space - python -m pytest test-*.py - - cd ../.. + - cd - # Testing addons - python -m pytest addons/test/test-*.py diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 67d2fbb86..8c3c636e6 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -129,7 +129,7 @@ namespace { static std::string executeAddon(const AddonInfo &addonInfo, const std::string &dumpFile) { - const std::string cmd = "python " + addonInfo.scriptFile + " --cli" + addonInfo.args + " " + dumpFile; + const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\""; #ifdef _WIN32 std::unique_ptr pipe(_popen(cmd.c_str(), "r"), _pclose); diff --git a/test/cli/test-helloworld.py b/test/cli/test-helloworld.py index 524112e6a..c6d84ffa6 100644 --- a/test/cli/test-helloworld.py +++ b/test/cli/test-helloworld.py @@ -35,7 +35,7 @@ def getVsConfigs(stdout, filename): return ' '.join(ret) def test_relative_path(): - ret, stdout, stderr = cppcheck('helloworld') + ret, stdout, stderr = cppcheck(['helloworld']) filename = os.path.join('helloworld', 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) @@ -43,21 +43,21 @@ def test_relative_path(): def test_local_path(): - ret, stdout, stderr = cppcheck_local('.') + ret, stdout, stderr = cppcheck_local(['.']) assert ret == 0 assert stdout == 'Checking main.c ...\n' assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_absolute_path(): prjpath = getAbsoluteProjectPath() - ret, stdout, stderr = cppcheck(prjpath) + ret, stdout, stderr = cppcheck([prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename) def test_addon_local_path(): - ret, stdout, stderr = cppcheck_local('--addon=misra .') + ret, stdout, stderr = cppcheck_local(['--addon=misra', '.']) assert ret == 0 assert stdout == 'Checking main.c ...\n' assert stderr == ('[main.c:5]: (error) Division by zero.\n' @@ -65,7 +65,7 @@ def test_addon_local_path(): def test_addon_absolute_path(): prjpath = getAbsoluteProjectPath() - ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath)) + ret, stdout, stderr = cppcheck(['--addon=misra', prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) @@ -74,7 +74,7 @@ def test_addon_absolute_path(): def test_addon_relative_path(): prjpath = getRelativeProjectPath() - ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath)) + ret, stdout, stderr = cppcheck(['--addon=misra', prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) @@ -84,7 +84,7 @@ def test_addon_relative_path(): def test_addon_relative_path(): project_file = 'helloworld/test.cppcheck' create_gui_project_file(project_file, paths=['.'], addon='misra') - ret, stdout, stderr = cppcheck('--project=%s' % (project_file)) + ret, stdout, stderr = cppcheck(['--project=' + project_file]) filename = os.path.join('helloworld', 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) @@ -93,7 +93,7 @@ def test_addon_relative_path(): def test_basepath_relative_path(): prjpath = getRelativeProjectPath() - ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath)) + ret, stdout, stderr = cppcheck([prjpath, '-rp=' + prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) @@ -101,21 +101,21 @@ def test_basepath_relative_path(): def test_basepath_absolute_path(): prjpath = getAbsoluteProjectPath() - ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath)) + ret, stdout, stderr = cppcheck([prjpath, '-rp=' + prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert stdout == 'Checking %s ...\n' % (filename) assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_vs_project_local_path(): - ret, stdout, stderr = cppcheck_local('--project=helloworld.vcxproj') + ret, stdout, stderr = cppcheck_local(['--project=helloworld.vcxproj']) assert ret == 0 assert getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64' assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_vs_project_relative_path(): prjpath = getRelativeProjectPath() - ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj'))) + ret, stdout, stderr = cppcheck(['--project=' + os.path.join(prjpath, 'helloworld.vcxproj')]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64' @@ -123,21 +123,21 @@ def test_vs_project_relative_path(): def test_vs_project_absolute_path(): prjpath = getAbsoluteProjectPath() - ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj'))) + ret, stdout, stderr = cppcheck(['--project=' + os.path.join(prjpath, 'helloworld.vcxproj')]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64' assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename) def test_cppcheck_project_local_path(): - ret, stdout, stderr = cppcheck_local('--platform=win64 --project=helloworld.cppcheck') + ret, stdout, stderr = cppcheck_local(['--platform=win64', '--project=helloworld.cppcheck']) assert ret == 0 assert getVsConfigs(stdout, 'main.c') == 'Debug|x64' assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_cppcheck_project_relative_path(): prjpath = getRelativeProjectPath() - ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck'))) + ret, stdout, stderr = cppcheck(['--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert getVsConfigs(stdout, filename) == 'Debug|x64' @@ -145,7 +145,7 @@ def test_cppcheck_project_relative_path(): def test_cppcheck_project_absolute_path(): prjpath = getAbsoluteProjectPath() - ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck'))) + ret, stdout, stderr = cppcheck(['--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 assert getVsConfigs(stdout, filename) == 'Debug|x64' @@ -153,12 +153,12 @@ def test_cppcheck_project_absolute_path(): def test_suppress_command_line(): prjpath = getRelativeProjectPath() - ret, stdout, stderr = cppcheck('--suppress=zerodiv:%s %s' % (os.path.join(prjpath, 'main.c'), prjpath)) + ret, stdout, stderr = cppcheck(['--suppress=zerodiv:' + os.path.join(prjpath, 'main.c'), prjpath]) assert ret == 0 assert stderr == '' prjpath = getAbsoluteProjectPath() - ret, stdout, stderr = cppcheck('--suppress=zerodiv:%s %s' % (os.path.join(prjpath, 'main.c'), prjpath)) + ret, stdout, stderr = cppcheck(['--suppress=zerodiv:' + os.path.join(prjpath, 'main.c'), prjpath]) assert ret == 0 assert stderr == '' @@ -169,12 +169,12 @@ def test_suppress_project(): suppressions=[{'fileName':'main.c', 'id':'zerodiv'}]) # Relative path - ret, stdout, stderr = cppcheck('--project=%s' % (project_file)) + ret, stdout, stderr = cppcheck(['--project=' + project_file]) assert ret == 0 assert stderr == '' # Absolute path - ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(os.getcwd(), 'helloworld', 'test.cppcheck'))) + ret, stdout, stderr = cppcheck(['--project=' + os.path.join(os.getcwd(), 'helloworld', 'test.cppcheck')]) assert ret == 0 assert stderr == '' diff --git a/test/cli/test-inline-suppress.py b/test/cli/test-inline-suppress.py index 9a70ee469..31dad1ab1 100644 --- a/test/cli/test-inline-suppress.py +++ b/test/cli/test-inline-suppress.py @@ -6,12 +6,12 @@ import re from testutils import cppcheck def test1(): - ret, stdout, stderr = cppcheck('--inline-suppr proj-inline-suppress') + ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress']) assert ret == 0 # TODO assert len(stderr) == 0 def test2(): - ret, stdout, stderr = cppcheck('proj-inline-suppress') + ret, stdout, stderr = cppcheck(['proj-inline-suppress']) assert ret == 0 assert len(stderr) > 0 diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py index cfe1df623..60e1d60e5 100644 --- a/test/cli/test-proj2.py +++ b/test/cli/test-proj2.py @@ -25,7 +25,7 @@ def cppcheck_local(args): def test_local_path(): create_compile_commands() - ret, stdout, stderr = cppcheck_local('--project=compile_commands.json') + ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json']) cwd = os.getcwd() file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') @@ -35,7 +35,7 @@ def test_local_path(): def test_relative_path(): create_compile_commands() - ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON) + ret, stdout, stderr = cppcheck(['--project=' + COMPILE_COMMANDS_JSON]) cwd = os.getcwd() file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') @@ -46,7 +46,7 @@ def test_relative_path(): def test_absolute_path(): create_compile_commands() cwd = os.getcwd() - ret, stdout, stderr = cppcheck('--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON)) + ret, stdout, stderr = cppcheck(['--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON)]) file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') assert ret == 0 @@ -55,7 +55,7 @@ def test_absolute_path(): def test_gui_project_loads_compile_commands_1(): create_compile_commands() - ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck') + ret, stdout, stderr = cppcheck(['--project=proj2/proj2.cppcheck']) cwd = os.getcwd() file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') @@ -69,7 +69,7 @@ def test_gui_project_loads_compile_commands_2(): create_gui_project_file('proj2/test.cppcheck', import_project='compile_commands.json', exclude_paths=[exclude_path_1]) - ret, stdout, stderr = cppcheck('--project=proj2/test.cppcheck') + ret, stdout, stderr = cppcheck(['--project=proj2/test.cppcheck']) cwd = os.getcwd() file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') # Excluded by test.cppcheck @@ -80,7 +80,7 @@ def test_gui_project_loads_compile_commands_2(): def test_gui_project_loads_relative_vs_solution(): create_gui_project_file('test.cppcheck', import_project='proj2/proj2.sln') - ret, stdout, stderr = cppcheck('--project=test.cppcheck') + ret, stdout, stderr = cppcheck(['--project=test.cppcheck']) file1 = os.path.join('proj2', 'a', 'a.c') file2 = os.path.join('proj2', 'b', 'b.c') assert ret == 0 @@ -95,7 +95,7 @@ def test_gui_project_loads_relative_vs_solution(): def test_gui_project_loads_absolute_vs_solution(): create_gui_project_file('test.cppcheck', import_project=os.path.join(os.getcwd(),'proj2', 'proj2.sln').replace('\\', '/')) - ret, stdout, stderr = cppcheck('--project=test.cppcheck') + ret, stdout, stderr = cppcheck(['--project=test.cppcheck']) file1 = os.path.join(os.getcwd(), 'proj2', 'a', 'a.c') file2 = os.path.join(os.getcwd(), 'proj2', 'b', 'b.c') print(stdout) @@ -111,20 +111,20 @@ def test_gui_project_loads_absolute_vs_solution(): def test_gui_project_loads_relative_vs_solution(): create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln') - ret, stdout, stderr = cppcheck('--project=test.cppcheck') + ret, stdout, stderr = cppcheck(['--project=test.cppcheck']) assert stderr == ('[a/a.c:1]: (error) Division by zero.\n' '[b/b.c:1]: (error) Division by zero.\n') def test_gui_project_loads_relative_vs_solution(): create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b']) - ret, stdout, stderr = cppcheck('--project=test.cppcheck') + ret, stdout, stderr = cppcheck(['--project=test.cppcheck']) assert stderr == '[a/a.c:1]: (error) Division by zero.\n' def test_gui_project_loads_absolute_vs_solution(): create_gui_project_file('test.cppcheck', root_path=os.path.join(os.getcwd(), 'proj2').replace('\\', '/'), import_project=os.path.join(os.getcwd(), 'proj2', 'proj2.sln').replace('\\', '/')) - ret, stdout, stderr = cppcheck('--project=test.cppcheck') + ret, stdout, stderr = cppcheck(['--project=test.cppcheck']) assert stderr == ('[a/a.c:1]: (error) Division by zero.\n' '[b/b.c:1]: (error) Division by zero.\n') diff --git a/test/cli/test-suppress-syntaxError.py b/test/cli/test-suppress-syntaxError.py index c5d6a68d6..0276b1eaa 100644 --- a/test/cli/test-suppress-syntaxError.py +++ b/test/cli/test-suppress-syntaxError.py @@ -6,12 +6,12 @@ import re from testutils import cppcheck def test_j2(): - ret, stdout, stderr = cppcheck('--error-exitcode=1 -j2 -q proj-suppress-syntaxError') + ret, stdout, stderr = cppcheck(['--error-exitcode=1', '-j2', '-q', 'proj-suppress-syntaxError']) assert ret == 1 assert len(stderr) > 0 def test_j2_suppress(): - ret, stdout, stderr = cppcheck('--error-exitcode=1 --suppress=*:proj-suppress-syntaxError/* -j2 -q proj-suppress-syntaxError') + ret, stdout, stderr = cppcheck(['--error-exitcode=1', '--suppress=*:proj-suppress-syntaxError/*', '-j2', '-q', 'proj-suppress-syntaxError']) assert ret == 0 assert len(stderr) == 0 diff --git a/test/cli/testutils.py b/test/cli/testutils.py index d9ca352d3..84f791c3e 100644 --- a/test/cli/testutils.py +++ b/test/cli/testutils.py @@ -42,16 +42,17 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p # Run Cppcheck with args def cppcheck(args): + exe = None if os.path.isfile('../../bin/debug/cppcheck.exe'): - cmd = '../../bin/debug/cppcheck.exe ' + args + exe = '../../bin/debug/cppcheck.exe' elif os.path.isfile('../../../bin/debug/cppcheck.exe'): - cmd = '../../../bin/debug/cppcheck.exe ' + args + exe = '../../../bin/debug/cppcheck.exe' elif os.path.isfile('../../cppcheck'): - cmd = '../../cppcheck ' + args + exe = '../../cppcheck' else: - cmd = '../../../cppcheck ' + args - logging.info(cmd) - p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + exe = '../../../cppcheck' + logging.info(exe + ' ' + ' '.join(args)) + p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) comm = p.communicate() stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')