Skip to content

Commit 9097f2c

Browse files
committed
generate test files
1 parent 58231bb commit 9097f2c

3 files changed

Lines changed: 34 additions & 22 deletions

File tree

test/cli/metrics_test.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
from testutils import cppcheck
55

66
__script_dir = os.path.dirname(os.path.abspath(__file__))
7-
__addon_path = os.path.join(__script_dir, 'metrics_test', 'dummy_addon.py')
8-
__source_path = os.path.join(__script_dir, 'metrics_test', 'dummy_file.c')
7+
__addon_output = """
8+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISCall","lineNumber":6,"value":2}}
9+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISGoto","lineNumber":6,"value":0}}
10+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISLevel","lineNumber":6,"value":2}}
11+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISParam","lineNumber":6,"value":2}}
12+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISPath","lineNumber":6,"value":3}}
13+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISReturn","lineNumber":6,"value":0}}
14+
{"metric":{"fileName":"1.cpp","function":"write","id":"HISStmt","lineNumber":6,"value":15}}
15+
{"metric":{"fileName":"1.cpp","function":"write","id":"cyclomaticComplexity","lineNumber":6,"value":3}}
16+
"""
17+
__addon_source = f'print("""{__addon_output}""")'
918
__expected_xml = [
1019
'<metric fileName="1.cpp" function="write" id="HISCall" lineNumber="6" value="2"/>',
1120
'<metric fileName="1.cpp" function="write" id="HISGoto" lineNumber="6" value="0"/>',
@@ -17,36 +26,52 @@
1726
'<metric fileName="1.cpp" function="write" id="cyclomaticComplexity" lineNumber="6" value="3"/>'
1827
]
1928

29+
def __create_addon(tmpdir):
30+
path = os.path.join(tmpdir, 'addon.py')
31+
with open(path, 'w') as file:
32+
file.write(__addon_source)
33+
return path
34+
35+
def __create_source_file(tmpdir):
36+
path = os.path.join(tmpdir, 'test.c')
37+
with open(path, 'w') as _:
38+
pass
39+
return path
40+
2041
def test_dummy_metrics_xml_report(tmpdir):
2142
output_file = os.path.join(tmpdir, "results.xml")
43+
source_path = __create_source_file(tmpdir)
44+
addon_path = __create_addon(tmpdir)
2245
args = [
2346
f'--output-file={output_file}',
24-
f'--addon={__addon_path}',
47+
f'--addon={addon_path}',
2548
'--xml-version=3',
26-
__source_path
49+
source_path
2750
]
2851

2952
ret, stdout, stderr = cppcheck(args)
3053
assert ret == 0
3154
assert stderr == ''
32-
assert stdout == f'Checking {__source_path} ...\n'
55+
assert stdout == f'Checking {source_path} ...\n'
3356

3457
with open(output_file, 'r') as file:
3558
xml = file.read()
3659

3760
for expected in __expected_xml:
3861
assert xml.find(expected) >= 0
3962

40-
def test_dummy_metrics_stdout():
63+
def test_dummy_metrics_stdout(tmpdir):
64+
source_path = __create_source_file(tmpdir)
65+
addon_path = __create_addon(tmpdir)
4166
args = [
42-
f'--addon={__addon_path}',
67+
f'--addon={addon_path}',
4368
'--xml-version=3',
44-
__source_path
69+
source_path
4570
]
4671

4772
ret, stdout, stderr = cppcheck(args)
4873
assert ret == 0
49-
assert stdout == f'Checking {__source_path} ...\n'
74+
assert stdout == f'Checking {source_path} ...\n'
5075

5176
for expected in __expected_xml:
5277
assert stderr.find(expected) >= 0

test/cli/metrics_test/dummy_addon.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/cli/metrics_test/dummy_file.c

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)