Skip to content

Commit de9c999

Browse files
committed
Better handling of spaces in paths
1 parent 7ceb51a commit de9c999

7 files changed

Lines changed: 44 additions & 42 deletions

File tree

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ script:
245245
- make -s -j2
246246
- cd ../../
247247
# Testing cli
248-
- cd test/cli
248+
- cp -R . ../cppcheck\ 2
249+
- cd ../cppcheck\ 2/test/cli # path with space
249250
- python -m pytest test-*.py
250-
- cd ../..
251+
- cd -
251252
# Testing addons
252253
- python -m pytest addons/test/test-*.py
253254

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ namespace {
129129

130130
static std::string executeAddon(const AddonInfo &addonInfo, const std::string &dumpFile)
131131
{
132-
const std::string cmd = "python " + addonInfo.scriptFile + " --cli" + addonInfo.args + " " + dumpFile;
132+
const std::string cmd = "python \"" + addonInfo.scriptFile + "\" --cli" + addonInfo.args + " \"" + dumpFile + "\"";
133133

134134
#ifdef _WIN32
135135
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd.c_str(), "r"), _pclose);

test/cli/test-helloworld.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,37 @@ def getVsConfigs(stdout, filename):
3535
return ' '.join(ret)
3636

3737
def test_relative_path():
38-
ret, stdout, stderr = cppcheck('helloworld')
38+
ret, stdout, stderr = cppcheck(['helloworld'])
3939
filename = os.path.join('helloworld', 'main.c')
4040
assert ret == 0
4141
assert stdout == 'Checking %s ...\n' % (filename)
4242
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
4343

4444

4545
def test_local_path():
46-
ret, stdout, stderr = cppcheck_local('.')
46+
ret, stdout, stderr = cppcheck_local(['.'])
4747
assert ret == 0
4848
assert stdout == 'Checking main.c ...\n'
4949
assert stderr == '[main.c:5]: (error) Division by zero.\n'
5050

5151
def test_absolute_path():
5252
prjpath = getAbsoluteProjectPath()
53-
ret, stdout, stderr = cppcheck(prjpath)
53+
ret, stdout, stderr = cppcheck([prjpath])
5454
filename = os.path.join(prjpath, 'main.c')
5555
assert ret == 0
5656
assert stdout == 'Checking %s ...\n' % (filename)
5757
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
5858

5959
def test_addon_local_path():
60-
ret, stdout, stderr = cppcheck_local('--addon=misra .')
60+
ret, stdout, stderr = cppcheck_local(['--addon=misra', '.'])
6161
assert ret == 0
6262
assert stdout == 'Checking main.c ...\n'
6363
assert stderr == ('[main.c:5]: (error) Division by zero.\n'
6464
'[main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n')
6565

6666
def test_addon_absolute_path():
6767
prjpath = getAbsoluteProjectPath()
68-
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
68+
ret, stdout, stderr = cppcheck(['--addon=misra', prjpath])
6969
filename = os.path.join(prjpath, 'main.c')
7070
assert ret == 0
7171
assert stdout == 'Checking %s ...\n' % (filename)
@@ -74,7 +74,7 @@ def test_addon_absolute_path():
7474

7575
def test_addon_relative_path():
7676
prjpath = getRelativeProjectPath()
77-
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
77+
ret, stdout, stderr = cppcheck(['--addon=misra', prjpath])
7878
filename = os.path.join(prjpath, 'main.c')
7979
assert ret == 0
8080
assert stdout == 'Checking %s ...\n' % (filename)
@@ -84,7 +84,7 @@ def test_addon_relative_path():
8484
def test_addon_relative_path():
8585
project_file = 'helloworld/test.cppcheck'
8686
create_gui_project_file(project_file, paths=['.'], addon='misra')
87-
ret, stdout, stderr = cppcheck('--project=%s' % (project_file))
87+
ret, stdout, stderr = cppcheck(['--project=' + project_file])
8888
filename = os.path.join('helloworld', 'main.c')
8989
assert ret == 0
9090
assert stdout == 'Checking %s ...\n' % (filename)
@@ -93,72 +93,72 @@ def test_addon_relative_path():
9393

9494
def test_basepath_relative_path():
9595
prjpath = getRelativeProjectPath()
96-
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
96+
ret, stdout, stderr = cppcheck([prjpath, '-rp=' + prjpath])
9797
filename = os.path.join(prjpath, 'main.c')
9898
assert ret == 0
9999
assert stdout == 'Checking %s ...\n' % (filename)
100100
assert stderr == '[main.c:5]: (error) Division by zero.\n'
101101

102102
def test_basepath_absolute_path():
103103
prjpath = getAbsoluteProjectPath()
104-
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
104+
ret, stdout, stderr = cppcheck([prjpath, '-rp=' + prjpath])
105105
filename = os.path.join(prjpath, 'main.c')
106106
assert ret == 0
107107
assert stdout == 'Checking %s ...\n' % (filename)
108108
assert stderr == '[main.c:5]: (error) Division by zero.\n'
109109

110110
def test_vs_project_local_path():
111-
ret, stdout, stderr = cppcheck_local('--project=helloworld.vcxproj')
111+
ret, stdout, stderr = cppcheck_local(['--project=helloworld.vcxproj'])
112112
assert ret == 0
113113
assert getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
114114
assert stderr == '[main.c:5]: (error) Division by zero.\n'
115115

116116
def test_vs_project_relative_path():
117117
prjpath = getRelativeProjectPath()
118-
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
118+
ret, stdout, stderr = cppcheck(['--project=' + os.path.join(prjpath, 'helloworld.vcxproj')])
119119
filename = os.path.join(prjpath, 'main.c')
120120
assert ret == 0
121121
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
122122
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
123123

124124
def test_vs_project_absolute_path():
125125
prjpath = getAbsoluteProjectPath()
126-
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
126+
ret, stdout, stderr = cppcheck(['--project=' + os.path.join(prjpath, 'helloworld.vcxproj')])
127127
filename = os.path.join(prjpath, 'main.c')
128128
assert ret == 0
129129
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
130130
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
131131

132132
def test_cppcheck_project_local_path():
133-
ret, stdout, stderr = cppcheck_local('--platform=win64 --project=helloworld.cppcheck')
133+
ret, stdout, stderr = cppcheck_local(['--platform=win64', '--project=helloworld.cppcheck'])
134134
assert ret == 0
135135
assert getVsConfigs(stdout, 'main.c') == 'Debug|x64'
136136
assert stderr == '[main.c:5]: (error) Division by zero.\n'
137137

138138
def test_cppcheck_project_relative_path():
139139
prjpath = getRelativeProjectPath()
140-
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
140+
ret, stdout, stderr = cppcheck(['--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
141141
filename = os.path.join(prjpath, 'main.c')
142142
assert ret == 0
143143
assert getVsConfigs(stdout, filename) == 'Debug|x64'
144144
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
145145

146146
def test_cppcheck_project_absolute_path():
147147
prjpath = getAbsoluteProjectPath()
148-
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
148+
ret, stdout, stderr = cppcheck(['--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
149149
filename = os.path.join(prjpath, 'main.c')
150150
assert ret == 0
151151
assert getVsConfigs(stdout, filename) == 'Debug|x64'
152152
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
153153

154154
def test_suppress_command_line():
155155
prjpath = getRelativeProjectPath()
156-
ret, stdout, stderr = cppcheck('--suppress=zerodiv:%s %s' % (os.path.join(prjpath, 'main.c'), prjpath))
156+
ret, stdout, stderr = cppcheck(['--suppress=zerodiv:' + os.path.join(prjpath, 'main.c'), prjpath])
157157
assert ret == 0
158158
assert stderr == ''
159159

160160
prjpath = getAbsoluteProjectPath()
161-
ret, stdout, stderr = cppcheck('--suppress=zerodiv:%s %s' % (os.path.join(prjpath, 'main.c'), prjpath))
161+
ret, stdout, stderr = cppcheck(['--suppress=zerodiv:' + os.path.join(prjpath, 'main.c'), prjpath])
162162
assert ret == 0
163163
assert stderr == ''
164164

@@ -169,12 +169,12 @@ def test_suppress_project():
169169
suppressions=[{'fileName':'main.c', 'id':'zerodiv'}])
170170

171171
# Relative path
172-
ret, stdout, stderr = cppcheck('--project=%s' % (project_file))
172+
ret, stdout, stderr = cppcheck(['--project=' + project_file])
173173
assert ret == 0
174174
assert stderr == ''
175175

176176
# Absolute path
177-
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(os.getcwd(), 'helloworld', 'test.cppcheck')))
177+
ret, stdout, stderr = cppcheck(['--project=' + os.path.join(os.getcwd(), 'helloworld', 'test.cppcheck')])
178178
assert ret == 0
179179
assert stderr == ''
180180

test/cli/test-inline-suppress.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from testutils import cppcheck
77

88
def test1():
9-
ret, stdout, stderr = cppcheck('--inline-suppr proj-inline-suppress')
9+
ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress'])
1010
assert ret == 0
1111
# TODO assert len(stderr) == 0
1212

1313
def test2():
14-
ret, stdout, stderr = cppcheck('proj-inline-suppress')
14+
ret, stdout, stderr = cppcheck(['proj-inline-suppress'])
1515
assert ret == 0
1616
assert len(stderr) > 0
1717

test/cli/test-proj2.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def cppcheck_local(args):
2525

2626
def test_local_path():
2727
create_compile_commands()
28-
ret, stdout, stderr = cppcheck_local('--project=compile_commands.json')
28+
ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json'])
2929
cwd = os.getcwd()
3030
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
3131
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
@@ -35,7 +35,7 @@ def test_local_path():
3535

3636
def test_relative_path():
3737
create_compile_commands()
38-
ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON)
38+
ret, stdout, stderr = cppcheck(['--project=' + COMPILE_COMMANDS_JSON])
3939
cwd = os.getcwd()
4040
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
4141
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
@@ -46,7 +46,7 @@ def test_relative_path():
4646
def test_absolute_path():
4747
create_compile_commands()
4848
cwd = os.getcwd()
49-
ret, stdout, stderr = cppcheck('--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON))
49+
ret, stdout, stderr = cppcheck(['--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON)])
5050
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
5151
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
5252
assert ret == 0
@@ -55,7 +55,7 @@ def test_absolute_path():
5555

5656
def test_gui_project_loads_compile_commands_1():
5757
create_compile_commands()
58-
ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck')
58+
ret, stdout, stderr = cppcheck(['--project=proj2/proj2.cppcheck'])
5959
cwd = os.getcwd()
6060
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
6161
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
@@ -69,7 +69,7 @@ def test_gui_project_loads_compile_commands_2():
6969
create_gui_project_file('proj2/test.cppcheck',
7070
import_project='compile_commands.json',
7171
exclude_paths=[exclude_path_1])
72-
ret, stdout, stderr = cppcheck('--project=proj2/test.cppcheck')
72+
ret, stdout, stderr = cppcheck(['--project=proj2/test.cppcheck'])
7373
cwd = os.getcwd()
7474
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
7575
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():
8080

8181
def test_gui_project_loads_relative_vs_solution():
8282
create_gui_project_file('test.cppcheck', import_project='proj2/proj2.sln')
83-
ret, stdout, stderr = cppcheck('--project=test.cppcheck')
83+
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
8484
file1 = os.path.join('proj2', 'a', 'a.c')
8585
file2 = os.path.join('proj2', 'b', 'b.c')
8686
assert ret == 0
@@ -95,7 +95,7 @@ def test_gui_project_loads_relative_vs_solution():
9595

9696
def test_gui_project_loads_absolute_vs_solution():
9797
create_gui_project_file('test.cppcheck', import_project=os.path.join(os.getcwd(),'proj2', 'proj2.sln').replace('\\', '/'))
98-
ret, stdout, stderr = cppcheck('--project=test.cppcheck')
98+
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
9999
file1 = os.path.join(os.getcwd(), 'proj2', 'a', 'a.c')
100100
file2 = os.path.join(os.getcwd(), 'proj2', 'b', 'b.c')
101101
print(stdout)
@@ -111,20 +111,20 @@ def test_gui_project_loads_absolute_vs_solution():
111111

112112
def test_gui_project_loads_relative_vs_solution():
113113
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln')
114-
ret, stdout, stderr = cppcheck('--project=test.cppcheck')
114+
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
115115
assert stderr == ('[a/a.c:1]: (error) Division by zero.\n'
116116
'[b/b.c:1]: (error) Division by zero.\n')
117117

118118
def test_gui_project_loads_relative_vs_solution():
119119
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
120-
ret, stdout, stderr = cppcheck('--project=test.cppcheck')
120+
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
121121
assert stderr == '[a/a.c:1]: (error) Division by zero.\n'
122122

123123
def test_gui_project_loads_absolute_vs_solution():
124124
create_gui_project_file('test.cppcheck',
125125
root_path=os.path.join(os.getcwd(), 'proj2').replace('\\', '/'),
126126
import_project=os.path.join(os.getcwd(), 'proj2', 'proj2.sln').replace('\\', '/'))
127-
ret, stdout, stderr = cppcheck('--project=test.cppcheck')
127+
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
128128
assert stderr == ('[a/a.c:1]: (error) Division by zero.\n'
129129
'[b/b.c:1]: (error) Division by zero.\n')
130130

test/cli/test-suppress-syntaxError.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from testutils import cppcheck
77

88
def test_j2():
9-
ret, stdout, stderr = cppcheck('--error-exitcode=1 -j2 -q proj-suppress-syntaxError')
9+
ret, stdout, stderr = cppcheck(['--error-exitcode=1', '-j2', '-q', 'proj-suppress-syntaxError'])
1010
assert ret == 1
1111
assert len(stderr) > 0
1212

1313
def test_j2_suppress():
14-
ret, stdout, stderr = cppcheck('--error-exitcode=1 --suppress=*:proj-suppress-syntaxError/* -j2 -q proj-suppress-syntaxError')
14+
ret, stdout, stderr = cppcheck(['--error-exitcode=1', '--suppress=*:proj-suppress-syntaxError/*', '-j2', '-q', 'proj-suppress-syntaxError'])
1515
assert ret == 0
1616
assert len(stderr) == 0
1717

test/cli/testutils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
4242

4343
# Run Cppcheck with args
4444
def cppcheck(args):
45+
exe = None
4546
if os.path.isfile('../../bin/debug/cppcheck.exe'):
46-
cmd = '../../bin/debug/cppcheck.exe ' + args
47+
exe = '../../bin/debug/cppcheck.exe'
4748
elif os.path.isfile('../../../bin/debug/cppcheck.exe'):
48-
cmd = '../../../bin/debug/cppcheck.exe ' + args
49+
exe = '../../../bin/debug/cppcheck.exe'
4950
elif os.path.isfile('../../cppcheck'):
50-
cmd = '../../cppcheck ' + args
51+
exe = '../../cppcheck'
5152
else:
52-
cmd = '../../../cppcheck ' + args
53-
logging.info(cmd)
54-
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
53+
exe = '../../../cppcheck'
54+
logging.info(exe + ' ' + ' '.join(args))
55+
p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
5556
comm = p.communicate()
5657
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
5758
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')

0 commit comments

Comments
 (0)