Better handling of spaces in paths

This commit is contained in:
Daniel Marjamäki 2019-06-22 19:20:15 +02:00
parent 7ceb51a952
commit de9c999d79
7 changed files with 44 additions and 42 deletions

View File

@ -245,9 +245,10 @@ script:
- make -s -j2 - make -s -j2
- cd ../../ - cd ../../
# Testing cli # Testing cli
- cd test/cli - cp -R . ../cppcheck\ 2
- cd ../cppcheck\ 2/test/cli # path with space
- python -m pytest test-*.py - python -m pytest test-*.py
- cd ../.. - cd -
# Testing addons # Testing addons
- python -m pytest addons/test/test-*.py - python -m pytest addons/test/test-*.py

View File

@ -129,7 +129,7 @@ namespace {
static std::string executeAddon(const AddonInfo &addonInfo, const std::string &dumpFile) 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 #ifdef _WIN32
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose); std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);

View File

@ -35,7 +35,7 @@ def getVsConfigs(stdout, filename):
return ' '.join(ret) return ' '.join(ret)
def test_relative_path(): def test_relative_path():
ret, stdout, stderr = cppcheck('helloworld') ret, stdout, stderr = cppcheck(['helloworld'])
filename = os.path.join('helloworld', 'main.c') filename = os.path.join('helloworld', 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
@ -43,21 +43,21 @@ def test_relative_path():
def test_local_path(): def test_local_path():
ret, stdout, stderr = cppcheck_local('.') ret, stdout, stderr = cppcheck_local(['.'])
assert ret == 0 assert ret == 0
assert stdout == 'Checking main.c ...\n' assert stdout == 'Checking main.c ...\n'
assert stderr == '[main.c:5]: (error) Division by zero.\n' assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_absolute_path(): def test_absolute_path():
prjpath = getAbsoluteProjectPath() prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck(prjpath) ret, stdout, stderr = cppcheck([prjpath])
filename = os.path.join(prjpath, 'main.c') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename) assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
def test_addon_local_path(): def test_addon_local_path():
ret, stdout, stderr = cppcheck_local('--addon=misra .') ret, stdout, stderr = cppcheck_local(['--addon=misra', '.'])
assert ret == 0 assert ret == 0
assert stdout == 'Checking main.c ...\n' assert stdout == 'Checking main.c ...\n'
assert stderr == ('[main.c:5]: (error) Division by zero.\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(): def test_addon_absolute_path():
prjpath = getAbsoluteProjectPath() prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath)) ret, stdout, stderr = cppcheck(['--addon=misra', prjpath])
filename = os.path.join(prjpath, 'main.c') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
@ -74,7 +74,7 @@ def test_addon_absolute_path():
def test_addon_relative_path(): def test_addon_relative_path():
prjpath = getRelativeProjectPath() prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath)) ret, stdout, stderr = cppcheck(['--addon=misra', prjpath])
filename = os.path.join(prjpath, 'main.c') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
@ -84,7 +84,7 @@ def test_addon_relative_path():
def test_addon_relative_path(): def test_addon_relative_path():
project_file = 'helloworld/test.cppcheck' project_file = 'helloworld/test.cppcheck'
create_gui_project_file(project_file, paths=['.'], addon='misra') 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') filename = os.path.join('helloworld', 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
@ -93,7 +93,7 @@ def test_addon_relative_path():
def test_basepath_relative_path(): def test_basepath_relative_path():
prjpath = getRelativeProjectPath() 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') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
@ -101,21 +101,21 @@ def test_basepath_relative_path():
def test_basepath_absolute_path(): def test_basepath_absolute_path():
prjpath = getAbsoluteProjectPath() 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') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename) assert stdout == 'Checking %s ...\n' % (filename)
assert stderr == '[main.c:5]: (error) Division by zero.\n' assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_vs_project_local_path(): 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 ret == 0
assert getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64' assert getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[main.c:5]: (error) Division by zero.\n' assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_vs_project_relative_path(): def test_vs_project_relative_path():
prjpath = getRelativeProjectPath() 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') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64' 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(): def test_vs_project_absolute_path():
prjpath = getAbsoluteProjectPath() 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') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64' assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename) assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
def test_cppcheck_project_local_path(): 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 ret == 0
assert getVsConfigs(stdout, 'main.c') == 'Debug|x64' assert getVsConfigs(stdout, 'main.c') == 'Debug|x64'
assert stderr == '[main.c:5]: (error) Division by zero.\n' assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_cppcheck_project_relative_path(): def test_cppcheck_project_relative_path():
prjpath = getRelativeProjectPath() 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') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|x64' assert getVsConfigs(stdout, filename) == 'Debug|x64'
@ -145,7 +145,7 @@ def test_cppcheck_project_relative_path():
def test_cppcheck_project_absolute_path(): def test_cppcheck_project_absolute_path():
prjpath = getAbsoluteProjectPath() 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') filename = os.path.join(prjpath, 'main.c')
assert ret == 0 assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|x64' assert getVsConfigs(stdout, filename) == 'Debug|x64'
@ -153,12 +153,12 @@ def test_cppcheck_project_absolute_path():
def test_suppress_command_line(): def test_suppress_command_line():
prjpath = getRelativeProjectPath() 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 ret == 0
assert stderr == '' assert stderr == ''
prjpath = getAbsoluteProjectPath() 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 ret == 0
assert stderr == '' assert stderr == ''
@ -169,12 +169,12 @@ def test_suppress_project():
suppressions=[{'fileName':'main.c', 'id':'zerodiv'}]) suppressions=[{'fileName':'main.c', 'id':'zerodiv'}])
# Relative path # Relative path
ret, stdout, stderr = cppcheck('--project=%s' % (project_file)) ret, stdout, stderr = cppcheck(['--project=' + project_file])
assert ret == 0 assert ret == 0
assert stderr == '' assert stderr == ''
# Absolute path # 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 ret == 0
assert stderr == '' assert stderr == ''

View File

@ -6,12 +6,12 @@ import re
from testutils import cppcheck from testutils import cppcheck
def test1(): def test1():
ret, stdout, stderr = cppcheck('--inline-suppr proj-inline-suppress') ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress'])
assert ret == 0 assert ret == 0
# TODO assert len(stderr) == 0 # TODO assert len(stderr) == 0
def test2(): def test2():
ret, stdout, stderr = cppcheck('proj-inline-suppress') ret, stdout, stderr = cppcheck(['proj-inline-suppress'])
assert ret == 0 assert ret == 0
assert len(stderr) > 0 assert len(stderr) > 0

View File

@ -25,7 +25,7 @@ def cppcheck_local(args):
def test_local_path(): def test_local_path():
create_compile_commands() create_compile_commands()
ret, stdout, stderr = cppcheck_local('--project=compile_commands.json') ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json'])
cwd = os.getcwd() cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
@ -35,7 +35,7 @@ def test_local_path():
def test_relative_path(): def test_relative_path():
create_compile_commands() create_compile_commands()
ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON) ret, stdout, stderr = cppcheck(['--project=' + COMPILE_COMMANDS_JSON])
cwd = os.getcwd() cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
@ -46,7 +46,7 @@ def test_relative_path():
def test_absolute_path(): def test_absolute_path():
create_compile_commands() create_compile_commands()
cwd = os.getcwd() 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') file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
assert ret == 0 assert ret == 0
@ -55,7 +55,7 @@ def test_absolute_path():
def test_gui_project_loads_compile_commands_1(): def test_gui_project_loads_compile_commands_1():
create_compile_commands() create_compile_commands()
ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck') ret, stdout, stderr = cppcheck(['--project=proj2/proj2.cppcheck'])
cwd = os.getcwd() cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
file2 = os.path.join(cwd, 'proj2', 'b', 'b.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', create_gui_project_file('proj2/test.cppcheck',
import_project='compile_commands.json', import_project='compile_commands.json',
exclude_paths=[exclude_path_1]) exclude_paths=[exclude_path_1])
ret, stdout, stderr = cppcheck('--project=proj2/test.cppcheck') ret, stdout, stderr = cppcheck(['--project=proj2/test.cppcheck'])
cwd = os.getcwd() cwd = os.getcwd()
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') # Excluded by test.cppcheck 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(): def test_gui_project_loads_relative_vs_solution():
create_gui_project_file('test.cppcheck', import_project='proj2/proj2.sln') 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') file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c') file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0 assert ret == 0
@ -95,7 +95,7 @@ def test_gui_project_loads_relative_vs_solution():
def test_gui_project_loads_absolute_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('\\', '/')) 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') file1 = os.path.join(os.getcwd(), 'proj2', 'a', 'a.c')
file2 = os.path.join(os.getcwd(), 'proj2', 'b', 'b.c') file2 = os.path.join(os.getcwd(), 'proj2', 'b', 'b.c')
print(stdout) print(stdout)
@ -111,20 +111,20 @@ def test_gui_project_loads_absolute_vs_solution():
def test_gui_project_loads_relative_vs_solution(): def test_gui_project_loads_relative_vs_solution():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln') 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' assert stderr == ('[a/a.c:1]: (error) Division by zero.\n'
'[b/b.c:1]: (error) Division by zero.\n') '[b/b.c:1]: (error) Division by zero.\n')
def test_gui_project_loads_relative_vs_solution(): 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']) 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' assert stderr == '[a/a.c:1]: (error) Division by zero.\n'
def test_gui_project_loads_absolute_vs_solution(): def test_gui_project_loads_absolute_vs_solution():
create_gui_project_file('test.cppcheck', create_gui_project_file('test.cppcheck',
root_path=os.path.join(os.getcwd(), 'proj2').replace('\\', '/'), root_path=os.path.join(os.getcwd(), 'proj2').replace('\\', '/'),
import_project=os.path.join(os.getcwd(), 'proj2', 'proj2.sln').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' assert stderr == ('[a/a.c:1]: (error) Division by zero.\n'
'[b/b.c:1]: (error) Division by zero.\n') '[b/b.c:1]: (error) Division by zero.\n')

View File

@ -6,12 +6,12 @@ import re
from testutils import cppcheck from testutils import cppcheck
def test_j2(): 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 ret == 1
assert len(stderr) > 0 assert len(stderr) > 0
def test_j2_suppress(): 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 ret == 0
assert len(stderr) == 0 assert len(stderr) == 0

View File

@ -42,16 +42,17 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
# Run Cppcheck with args # Run Cppcheck with args
def cppcheck(args): def cppcheck(args):
exe = None
if os.path.isfile('../../bin/debug/cppcheck.exe'): 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'): elif os.path.isfile('../../../bin/debug/cppcheck.exe'):
cmd = '../../../bin/debug/cppcheck.exe ' + args exe = '../../../bin/debug/cppcheck.exe'
elif os.path.isfile('../../cppcheck'): elif os.path.isfile('../../cppcheck'):
cmd = '../../cppcheck ' + args exe = '../../cppcheck'
else: else:
cmd = '../../../cppcheck ' + args exe = '../../../cppcheck'
logging.info(cmd) logging.info(exe + ' ' + ' '.join(args))
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
comm = p.communicate() comm = p.communicate()
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') 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') stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')