File: metrics_test.py

package info (click to toggle)
cppcheck 2.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,132 kB
  • sloc: cpp: 268,935; python: 20,890; ansic: 8,090; sh: 1,045; makefile: 1,008; xml: 1,005; cs: 291
file content (78 lines) | stat: -rw-r--r-- 3,030 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# python -m pytest metrics_test.py

import os
from testutils import cppcheck

__script_dir = os.path.dirname(os.path.abspath(__file__))
__addon_output = """
{"metric":{"fileName":"1.cpp","function":"write","id":"HISCall","lineNumber":6,"value":2}}
{"metric":{"fileName":"1.cpp","function":"write","id":"HISGoto","lineNumber":6,"value":0}}
{"metric":{"fileName":"1.cpp","function":"write","id":"HISLevel","lineNumber":6,"value":2}}
{"metric":{"fileName":"1.cpp","function":"write","id":"HISParam","lineNumber":6,"value":2}}
{"metric":{"fileName":"1.cpp","function":"write","id":"HISPath","lineNumber":6,"value":3}}
{"metric":{"fileName":"1.cpp","function":"write","id":"HISReturn","lineNumber":6,"value":0}}
{"metric":{"fileName":"1.cpp","function":"write","id":"HISStmt","lineNumber":6,"value":15}}
{"metric":{"fileName":"1.cpp","function":"write","id":"cyclomaticComplexity","lineNumber":6,"value":3}}
"""
__addon_source = f'print("""{__addon_output}""")'
__expected_xml = [
    '<metric fileName="1.cpp" function="write" id="HISCall" lineNumber="6" value="2"/>',
    '<metric fileName="1.cpp" function="write" id="HISGoto" lineNumber="6" value="0"/>',
    '<metric fileName="1.cpp" function="write" id="HISLevel" lineNumber="6" value="2"/>',
    '<metric fileName="1.cpp" function="write" id="HISParam" lineNumber="6" value="2"/>',
    '<metric fileName="1.cpp" function="write" id="HISPath" lineNumber="6" value="3"/>',
    '<metric fileName="1.cpp" function="write" id="HISReturn" lineNumber="6" value="0"/>',
    '<metric fileName="1.cpp" function="write" id="HISStmt" lineNumber="6" value="15"/>',
    '<metric fileName="1.cpp" function="write" id="cyclomaticComplexity" lineNumber="6" value="3"/>'
]

def __create_addon(tmpdir):
    path = os.path.join(tmpdir, 'addon.py')
    with open(path, 'w') as file:
        file.write(__addon_source)
    return path

def __create_source_file(tmpdir):
    path = os.path.join(tmpdir, 'test.c')
    with open(path, 'w') as _:
        pass
    return path

def test_dummy_metrics_xml_report(tmpdir):
    output_file = os.path.join(tmpdir, "results.xml")
    source_path = __create_source_file(tmpdir)
    addon_path = __create_addon(tmpdir)
    args = [
        f'--output-file={output_file}',
        f'--addon={addon_path}',
        '--xml-version=3',
        source_path
    ]

    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stderr == ''
    assert stdout == f'Checking {source_path} ...\n'

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

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

def test_dummy_metrics_stdout(tmpdir):
    source_path = __create_source_file(tmpdir)
    addon_path = __create_addon(tmpdir)
    args = [
        f'--addon={addon_path}',
        '--xml-version=3',
        source_path
    ]

    ret, stdout, stderr = cppcheck(args)
    assert ret == 0
    assert stdout == f'Checking {source_path} ...\n'

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