|
4 | 4 | from testutils import cppcheck |
5 | 5 |
|
6 | 6 | __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}""")' |
9 | 18 | __expected_xml = [ |
10 | 19 | '<metric fileName="1.cpp" function="write" id="HISCall" lineNumber="6" value="2"/>', |
11 | 20 | '<metric fileName="1.cpp" function="write" id="HISGoto" lineNumber="6" value="0"/>', |
|
17 | 26 | '<metric fileName="1.cpp" function="write" id="cyclomaticComplexity" lineNumber="6" value="3"/>' |
18 | 27 | ] |
19 | 28 |
|
| 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 | + |
20 | 41 | def test_dummy_metrics_xml_report(tmpdir): |
21 | 42 | output_file = os.path.join(tmpdir, "results.xml") |
| 43 | + source_path = __create_source_file(tmpdir) |
| 44 | + addon_path = __create_addon(tmpdir) |
22 | 45 | args = [ |
23 | 46 | f'--output-file={output_file}', |
24 | | - f'--addon={__addon_path}', |
| 47 | + f'--addon={addon_path}', |
25 | 48 | '--xml-version=3', |
26 | | - __source_path |
| 49 | + source_path |
27 | 50 | ] |
28 | 51 |
|
29 | 52 | ret, stdout, stderr = cppcheck(args) |
30 | 53 | assert ret == 0 |
31 | 54 | assert stderr == '' |
32 | | - assert stdout == f'Checking {__source_path} ...\n' |
| 55 | + assert stdout == f'Checking {source_path} ...\n' |
33 | 56 |
|
34 | 57 | with open(output_file, 'r') as file: |
35 | 58 | xml = file.read() |
36 | 59 |
|
37 | 60 | for expected in __expected_xml: |
38 | 61 | assert xml.find(expected) >= 0 |
39 | 62 |
|
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) |
41 | 66 | args = [ |
42 | | - f'--addon={__addon_path}', |
| 67 | + f'--addon={addon_path}', |
43 | 68 | '--xml-version=3', |
44 | | - __source_path |
| 69 | + source_path |
45 | 70 | ] |
46 | 71 |
|
47 | 72 | ret, stdout, stderr = cppcheck(args) |
48 | 73 | assert ret == 0 |
49 | | - assert stdout == f'Checking {__source_path} ...\n' |
| 74 | + assert stdout == f'Checking {source_path} ...\n' |
50 | 75 |
|
51 | 76 | for expected in __expected_xml: |
52 | 77 | assert stderr.find(expected) >= 0 |
|
0 commit comments