|
| 1 | +# python -m pytest metrics_test.py |
| 2 | + |
| 3 | +import os |
| 4 | +from testutils import cppcheck |
| 5 | + |
| 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') |
| 9 | +__expected_xml = [ |
| 10 | + '<fileName="1.cpp" function="write" id="HISCall" lineNumber="6" value="2"/>', |
| 11 | + '<fileName="1.cpp" function="write" id="HISGoto" lineNumber="6" value="0"/>', |
| 12 | + '<fileName="1.cpp" function="write" id="HISLevel" lineNumber="6" value="2"/>', |
| 13 | + '<fileName="1.cpp" function="write" id="HISParam" lineNumber="6" value="2"/>', |
| 14 | + '<fileName="1.cpp" function="write" id="HISPath" lineNumber="6" value="3"/>', |
| 15 | + '<fileName="1.cpp" function="write" id="HISReturn" lineNumber="6" value="0"/>', |
| 16 | + '<fileName="1.cpp" function="write" id="HISStmt" lineNumber="6" value="15"/>', |
| 17 | + '<fileName="1.cpp" function="write" id="cyclomaticComplexity" lineNumber="6" value="3"/>' |
| 18 | +] |
| 19 | + |
| 20 | +def test_dummy_metrics_xml_report(tmpdir): |
| 21 | + output_file = os.path.join(tmpdir, "results.xml") |
| 22 | + args = [ |
| 23 | + f'--output-file={output_file}', |
| 24 | + f'--addon={__addon_path}', |
| 25 | + '--xml-version=3', |
| 26 | + __source_path |
| 27 | + ] |
| 28 | + |
| 29 | + ret, stdout, stderr = cppcheck(args) |
| 30 | + assert ret == 0 |
| 31 | + assert stderr == '' |
| 32 | + assert stdout == f'Checking {__source_path} ...\n' |
| 33 | + |
| 34 | + with open(output_file, 'r') as file: |
| 35 | + xml = file.read() |
| 36 | + |
| 37 | + for expected in __expected_xml: |
| 38 | + assert xml.find(expected) >= 0 |
| 39 | + |
| 40 | +def test_dummy_metrics_stdout(): |
| 41 | + args = [ |
| 42 | + f'--addon={__addon_path}', |
| 43 | + '--xml-version=3', |
| 44 | + __source_path |
| 45 | + ] |
| 46 | + |
| 47 | + ret, stdout, stderr = cppcheck(args) |
| 48 | + assert ret == 0 |
| 49 | + assert stdout == f'Checking {__source_path} ...\n' |
| 50 | + |
| 51 | + for expected in __expected_xml: |
| 52 | + assert stderr.find(expected) >= 0 |
| 53 | + |
0 commit comments