Skip to content

Commit 65d18b3

Browse files
committed
test/cli: refactorings
1 parent 1a8a40e commit 65d18b3

3 files changed

Lines changed: 46 additions & 37 deletions

File tree

‎.travis.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ script:
244244
- cd ../../
245245
# Testing cli
246246
- cd test/cli
247-
- python -m pytest test-helloworld.py
247+
- python -m pytest test-*.py
248248
- cd ../..
249249

250250
notifications:

‎test/cli/test-helloworld.py‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ def cppcheck(args):
2323
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
2424
return p.returncode, stdout, stderr
2525

26+
# Run Cppcheck from project path
27+
def cppcheck_local(args):
28+
cwd = os.getcwd()
29+
os.chdir('1-helloworld')
30+
ret, stdout, stderr = cppcheck(args)
31+
os.chdir(cwd)
32+
return (ret, stdout, stderr)
33+
34+
def getRelativeProjectPath():
35+
return '1-helloworld'
36+
37+
def getAbsoluteProjectPath():
38+
return os.path.join(os.getcwd(), '1-helloworld')
39+
2640
# Get Visual Studio configurations checking a file
2741
# Checking {file} {config}...
2842
def getVsConfigs(stdout, filename):
@@ -48,34 +62,28 @@ def test_relative_path():
4862

4963

5064
def test_local_path():
51-
cwd = os.getcwd()
52-
os.chdir('1-helloworld')
53-
ret, stdout, stderr = cppcheck('.')
54-
os.chdir(cwd)
65+
ret, stdout, stderr = cppcheck_local('.')
5566
assert ret == 0
5667
assert stdout == 'Checking main.c ...\n'
5768
assert stderr == '[main.c:5]: (error) Division by zero.\n'
5869

5970
def test_absolute_path():
60-
prjpath = os.path.join(os.getcwd(), '1-helloworld')
71+
prjpath = getAbsoluteProjectPath()
6172
ret, stdout, stderr = cppcheck(prjpath)
6273
filename = os.path.join(prjpath, 'main.c')
6374
assert ret == 0
6475
assert stdout == 'Checking %s ...\n' % (filename)
6576
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
6677

6778
def test_addon_local_path():
68-
cwd = os.getcwd()
69-
os.chdir('1-helloworld')
70-
ret, stdout, stderr = cppcheck('--addon=misra .')
71-
os.chdir(cwd)
79+
ret, stdout, stderr = cppcheck_local('--addon=misra .')
7280
assert ret == 0
7381
assert stdout == 'Checking main.c ...\n'
7482
assert stderr == ('[main.c:5]: (error) Division by zero.\n'
7583
'[main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n')
7684

7785
def test_addon_absolute_path():
78-
prjpath = os.path.join(os.getcwd(), '1-helloworld')
86+
prjpath = getAbsoluteProjectPath()
7987
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
8088
filename = os.path.join(prjpath, 'main.c')
8189
assert ret == 0
@@ -84,7 +92,7 @@ def test_addon_absolute_path():
8492
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
8593

8694
def test_addon_relative_path():
87-
prjpath = '1-helloworld'
95+
prjpath = getRelativeProjectPath()
8896
ret, stdout, stderr = cppcheck('--addon=misra %s' % (prjpath))
8997
filename = os.path.join(prjpath, 'main.c')
9098
assert ret == 0
@@ -93,69 +101,62 @@ def test_addon_relative_path():
93101
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
94102

95103
def test_basepath_relative_path():
96-
prjpath = '1-helloworld'
104+
prjpath = getRelativeProjectPath()
97105
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
98106
filename = os.path.join(prjpath, 'main.c')
99107
assert ret == 0
100108
assert stdout == 'Checking %s ...\n' % (filename)
101109
assert stderr == '[main.c:5]: (error) Division by zero.\n'
102110

103111
def test_basepath_absolute_path():
104-
prjpath = os.path.join(os.getcwd(), '1-helloworld')
112+
prjpath = getAbsoluteProjectPath()
105113
ret, stdout, stderr = cppcheck('%s -rp=%s' % (prjpath, prjpath))
106114
filename = os.path.join(prjpath, 'main.c')
107115
assert ret == 0
108116
assert stdout == 'Checking %s ...\n' % (filename)
109117
assert stderr == '[main.c:5]: (error) Division by zero.\n'
110118

111119
def test_vs_project_local_path():
112-
cwd = os.getcwd()
113-
os.chdir('1-helloworld')
114-
ret, stdout, stderr = cppcheck('--project=helloworld.vcxproj')
115-
os.chdir(cwd)
120+
ret, stdout, stderr = cppcheck_local('--project=helloworld.vcxproj')
116121
assert ret == 0
117122
assert getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
118123
assert stderr == '[main.c:5]: (error) Division by zero.\n'
119124

120125
def test_vs_project_relative_path():
121-
prjpath = '1-helloworld'
126+
prjpath = getRelativeProjectPath()
122127
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
123128
filename = os.path.join(prjpath, 'main.c')
124129
assert ret == 0
125130
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
126131
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
127132

128133
def test_vs_project_absolute_path():
129-
prjpath = os.path.join(os.getcwd(), '1-helloworld')
134+
prjpath = getAbsoluteProjectPath()
130135
ret, stdout, stderr = cppcheck('--project=%s' % (os.path.join(prjpath, 'helloworld.vcxproj')))
131136
filename = os.path.join(prjpath, 'main.c')
132137
assert ret == 0
133138
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
134139
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
135140

136141
def test_cppcheck_project_local_path():
137-
cwd = os.getcwd()
138-
os.chdir('1-helloworld')
139-
ret, stdout, stderr = cppcheck('--platform=win64 --project=helloworld.cppcheck')
140-
os.chdir(cwd)
142+
ret, stdout, stderr = cppcheck_local('--platform=win64 --project=helloworld.cppcheck')
141143
assert ret == 0
142144
assert getVsConfigs(stdout, 'main.c') == 'Debug|x64'
143145
assert stderr == '[main.c:5]: (error) Division by zero.\n'
144146

145147
def test_cppcheck_project_relative_path():
146-
prjpath = '1-helloworld'
148+
prjpath = getRelativeProjectPath()
147149
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
148150
filename = os.path.join(prjpath, 'main.c')
149151
assert ret == 0
150152
assert getVsConfigs(stdout, filename) == 'Debug|x64'
151153
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
152154

153155
def test_cppcheck_project_absolute_path():
154-
prjpath = os.path.join(os.getcwd(), '1-helloworld')
156+
prjpath = getAbsoluteProjectPath()
155157
ret, stdout, stderr = cppcheck('--platform=win64 --project=%s' % (os.path.join(prjpath, 'helloworld.cppcheck')))
156158
filename = os.path.join(prjpath, 'main.c')
157159
assert ret == 0
158160
assert getVsConfigs(stdout, filename) == 'Debug|x64'
159161
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
160162

161-

‎test/cli/test-proj2.py‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
COMPILE_COMMANDS_JSON = os.path.join('proj2', 'compile_commands.json')
1010

11-
def create_compile_commands(path):
12-
j = [{'directory': path+'/a', 'command': 'gcc -c a.c', 'file': 'a.c'},
13-
{'directory': path+'/b', 'command': 'gcc -c b.c', 'file': 'b.c'}]
11+
def create_compile_commands():
12+
prjpath = os.path.join(os.getcwd(), 'proj2')
13+
j = [{'directory': os.path.join(prjpath,'a'), 'command': 'gcc -c a.c', 'file': 'a.c'},
14+
{'directory': os.path.join(prjpath,'b'), 'command': 'gcc -c b.c', 'file': 'b.c'}]
1415
f = open(COMPILE_COMMANDS_JSON, 'wt')
1516
f.write(json.dumps(j))
1617

@@ -31,20 +32,27 @@ def cppcheck(args):
3132
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
3233
return p.returncode, stdout, stderr
3334

34-
def test_local_path():
35-
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
35+
# Run Cppcheck from project path
36+
def cppcheck_local(args):
3637
cwd = os.getcwd()
3738
os.chdir('proj2')
38-
ret, stdout, stderr = cppcheck('--project=compile_commands.json')
39+
ret, stdout, stderr = cppcheck(args)
3940
os.chdir(cwd)
41+
return (ret, stdout, stderr)
42+
43+
44+
def test_local_path():
45+
create_compile_commands()
46+
ret, stdout, stderr = cppcheck_local('--project=compile_commands.json')
47+
cwd = os.getcwd()
4048
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
4149
file2 = os.path.join(cwd, 'proj2', 'b', 'b.c')
4250
assert ret == 0
4351
assert stdout.find('Checking %s ...' % (file1)) >= 0
4452
assert stdout.find('Checking %s ...' % (file2)) >= 0
4553

4654
def test_relative_path():
47-
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
55+
create_compile_commands()
4856
ret, stdout, stderr = cppcheck('--project=' + COMPILE_COMMANDS_JSON)
4957
cwd = os.getcwd()
5058
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
@@ -54,7 +62,7 @@ def test_relative_path():
5462
assert stdout.find('Checking %s ...' % (file2)) >= 0
5563

5664
def test_absolute_path():
57-
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
65+
create_compile_commands()
5866
cwd = os.getcwd()
5967
ret, stdout, stderr = cppcheck('--project=' + os.path.join(cwd,COMPILE_COMMANDS_JSON))
6068
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
@@ -64,7 +72,7 @@ def test_absolute_path():
6472
assert stdout.find('Checking %s ...' % (file2)) >= 0
6573

6674
def test_gui_project_loads_compile_commands_1():
67-
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
75+
create_compile_commands()
6876
ret, stdout, stderr = cppcheck('--project=proj2/proj2.cppcheck')
6977
cwd = os.getcwd()
7078
file1 = os.path.join(cwd, 'proj2', 'a', 'a.c')
@@ -74,7 +82,7 @@ def test_gui_project_loads_compile_commands_1():
7482
assert stdout.find('Checking %s ...' % (file2)) >= 0
7583

7684
def test_gui_project_loads_compile_commands_2():
77-
create_compile_commands(os.path.join(os.getcwd(), 'proj2'))
85+
create_compile_commands()
7886

7987
# create cppcheck gui project file
8088
cppcheck_xml = ('<?xml version="1.0" encoding="UTF-8"?>'

0 commit comments

Comments
 (0)