Skip to content

Commit 6549aed

Browse files
committed
test/cli: Added testutils.py
1 parent a03455c commit 6549aed

3 files changed

Lines changed: 57 additions & 105 deletions

File tree

test/cli/test-helloworld.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11

22
# python -m pytest test-helloworld.py
33

4-
import logging
54
import os
65
import re
7-
import subprocess
8-
9-
# Run Cppcheck with args
10-
def cppcheck(args):
11-
if os.path.isfile('../../bin/debug/cppcheck.exe'):
12-
cmd = '../../bin/debug/cppcheck.exe ' + args
13-
elif os.path.isfile('../../../bin/debug/cppcheck.exe'):
14-
cmd = '../../../bin/debug/cppcheck.exe ' + args
15-
elif os.path.isfile('../../cppcheck'):
16-
cmd = '../../cppcheck ' + args
17-
else:
18-
cmd = '../../../cppcheck ' + args
19-
logging.info(cmd)
20-
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21-
comm = p.communicate()
22-
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
23-
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
24-
return p.returncode, stdout, stderr
6+
from testutils import create_gui_project_file, cppcheck
257

268
# Run Cppcheck from project path
279
def cppcheck_local(args):
@@ -52,39 +34,6 @@ def getVsConfigs(stdout, filename):
5234
ret.sort()
5335
return ' '.join(ret)
5436

55-
# Create Cppcheck project file
56-
def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None):
57-
cppcheck_xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
58-
'<project version="1">\n')
59-
if root_path:
60-
cppcheck_xml += ' <root name="' + root_path + '"/>\n'
61-
if import_project:
62-
cppcheck_xml += ' <importproject>' + import_project + '</importproject>\n'
63-
if paths:
64-
cppcheck_xml += ' <paths>\n'
65-
for path in paths:
66-
cppcheck_xml += ' <dir name="' + path + '"/>\n'
67-
cppcheck_xml += ' </paths>\n'
68-
if exclude_paths:
69-
cppcheck_xml += ' <exclude>\n'
70-
for path in exclude_paths:
71-
cppcheck_xml += ' <path name="' + path + '"/>\n'
72-
cppcheck_xml += ' </exclude>\n'
73-
if suppressions:
74-
cppcheck_xml += ' <suppressions>\n'
75-
for suppression in suppressions:
76-
cppcheck_xml += ' <suppression'
77-
if 'fileName' in suppression:
78-
cppcheck_xml += ' fileName="' + suppression['fileName'] + '"'
79-
cppcheck_xml += '>' + suppression['id'] + '</suppression>\n'
80-
cppcheck_xml += ' </suppressions>\n'
81-
cppcheck_xml += '</project>\n'
82-
83-
f = open(project_file, 'wt')
84-
f.write(cppcheck_xml)
85-
f.close()
86-
87-
8837
def test_relative_path():
8938
ret, stdout, stderr = cppcheck('1-helloworld')
9039
filename = os.path.join('1-helloworld', 'main.c')

test/cli/test-proj2.py

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
# python -m pytest test-helloworld.py
33

44
import json
5-
import logging
65
import os
7-
import subprocess
6+
from testutils import create_gui_project_file, cppcheck
87

98
COMPILE_COMMANDS_JSON = os.path.join('proj2', 'compile_commands.json')
109

@@ -15,57 +14,6 @@ def create_compile_commands():
1514
f = open(COMPILE_COMMANDS_JSON, 'wt')
1615
f.write(json.dumps(j))
1716

18-
19-
# Create Cppcheck project file
20-
def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None):
21-
cppcheck_xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
22-
'<project version="1">\n')
23-
if root_path:
24-
cppcheck_xml += ' <root name="' + root_path + '"/>\n'
25-
if import_project:
26-
cppcheck_xml += ' <importproject>' + import_project + '</importproject>\n'
27-
if paths:
28-
cppcheck_xml += ' <paths>\n'
29-
for path in paths:
30-
cppcheck_xml += ' <dir name="' + path + '"/>\n'
31-
cppcheck_xml += ' </paths>\n'
32-
if exclude_paths:
33-
cppcheck_xml += ' <exclude>\n'
34-
for path in exclude_paths:
35-
cppcheck_xml += ' <path name="' + path + '"/>\n'
36-
cppcheck_xml += ' </exclude>\n'
37-
if suppressions:
38-
cppcheck_xml += ' <suppressions>\n'
39-
for suppression in suppressions:
40-
cppcheck_xml += ' <suppression'
41-
if 'fileName' in suppression:
42-
cppcheck_xml += ' fileName="' + suppression['fileName'] + '"'
43-
cppcheck_xml += '>' + suppression['id'] + '</suppression>\n'
44-
cppcheck_xml += ' </suppressions>\n'
45-
cppcheck_xml += '</project>\n'
46-
47-
f = open(project_file, 'wt')
48-
f.write(cppcheck_xml)
49-
f.close()
50-
51-
52-
# Run Cppcheck with args
53-
def cppcheck(args):
54-
if os.path.isfile('../../bin/debug/cppcheck.exe'):
55-
cmd = '../../bin/debug/cppcheck.exe ' + args
56-
elif os.path.isfile('../../../bin/debug/cppcheck.exe'):
57-
cmd = '../../../bin/debug/cppcheck.exe ' + args
58-
elif os.path.isfile('../../cppcheck'):
59-
cmd = '../../cppcheck ' + args
60-
else:
61-
cmd = '../../../cppcheck ' + args
62-
logging.info(cmd)
63-
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64-
comm = p.communicate()
65-
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
66-
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
67-
return p.returncode, stdout, stderr
68-
6917
# Run Cppcheck from project path
7018
def cppcheck_local(args):
7119
cwd = os.getcwd()

test/cli/testutils.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
import logging
3+
import os
4+
import subprocess
5+
6+
# Create Cppcheck project file
7+
def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None):
8+
cppcheck_xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
9+
'<project version="1">\n')
10+
if root_path:
11+
cppcheck_xml += ' <root name="' + root_path + '"/>\n'
12+
if import_project:
13+
cppcheck_xml += ' <importproject>' + import_project + '</importproject>\n'
14+
if paths:
15+
cppcheck_xml += ' <paths>\n'
16+
for path in paths:
17+
cppcheck_xml += ' <dir name="' + path + '"/>\n'
18+
cppcheck_xml += ' </paths>\n'
19+
if exclude_paths:
20+
cppcheck_xml += ' <exclude>\n'
21+
for path in exclude_paths:
22+
cppcheck_xml += ' <path name="' + path + '"/>\n'
23+
cppcheck_xml += ' </exclude>\n'
24+
if suppressions:
25+
cppcheck_xml += ' <suppressions>\n'
26+
for suppression in suppressions:
27+
cppcheck_xml += ' <suppression'
28+
if 'fileName' in suppression:
29+
cppcheck_xml += ' fileName="' + suppression['fileName'] + '"'
30+
cppcheck_xml += '>' + suppression['id'] + '</suppression>\n'
31+
cppcheck_xml += ' </suppressions>\n'
32+
cppcheck_xml += '</project>\n'
33+
34+
f = open(project_file, 'wt')
35+
f.write(cppcheck_xml)
36+
f.close()
37+
38+
39+
# Run Cppcheck with args
40+
def cppcheck(args):
41+
if os.path.isfile('../../bin/debug/cppcheck.exe'):
42+
cmd = '../../bin/debug/cppcheck.exe ' + args
43+
elif os.path.isfile('../../../bin/debug/cppcheck.exe'):
44+
cmd = '../../../bin/debug/cppcheck.exe ' + args
45+
elif os.path.isfile('../../cppcheck'):
46+
cmd = '../../cppcheck ' + args
47+
else:
48+
cmd = '../../../cppcheck ' + args
49+
logging.info(cmd)
50+
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
51+
comm = p.communicate()
52+
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
53+
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
54+
return p.returncode, stdout, stderr
55+

0 commit comments

Comments
 (0)