File: dumpfile_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 (134 lines) | stat: -rw-r--r-- 3,665 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# python -m pytest dumpfile_test.py

import json
import os
import pathlib

from testutils import cppcheck
import xml.etree.ElementTree as ET


def test_libraries(tmpdir):  #13701
    test_file = str(tmpdir / 'test.c')
    with open(test_file, 'wt') as f:
        f.write('x=1;\n')

    args = ['--library=posix', '--dump', test_file]
    _, _, _ = cppcheck(args)

    dumpfile = test_file + '.dump'
    assert os.path.isfile(dumpfile)
    with open(dumpfile, 'rt') as f:
        dump = f.read()
    assert '<library lib="posix"/>' in dump
    assert dump.find('<library lib="posix"/>') < dump.find('<dump cfg=')


def __test_language(tmp_path, file_ext, exp_lang, force_lang=None):
    test_file = tmp_path / ('test.' + file_ext)
    with open(test_file, 'wt'):
        pass

    args = [
        '--dump',
        str(test_file)
    ]
    if force_lang:
        args += ['--language=' + force_lang]

    exitcode, stdout, stderr = cppcheck(args)
    assert exitcode == 0, stdout if stdout else stderr

    dump_s = pathlib.Path(str(test_file) + '.dump').read_text()

    dump_xml = ET.fromstring(dump_s)
    assert 'language' in dump_xml.attrib
    assert dump_xml.attrib['language'] == exp_lang


def test_language_c(tmp_path):
    __test_language(tmp_path, 'c', exp_lang='c')


def test_language_c_force_c(tmp_path):
    __test_language(tmp_path, 'c', force_lang='c', exp_lang='c')


def test_language_c_force_cpp(tmp_path):
    __test_language(tmp_path, 'c', force_lang='c++', exp_lang='cpp')


def test_language_cpp(tmp_path):
    __test_language(tmp_path, 'cpp', exp_lang='cpp')


def test_language_cpp_force_cpp(tmp_path):
    __test_language(tmp_path, 'cpp', force_lang='c++', exp_lang='cpp')


def test_language_cpp_force_c(tmp_path):
    __test_language(tmp_path, 'cpp', force_lang='c', exp_lang='c')


# headers default to C
def test_language_h(tmp_path):
    __test_language(tmp_path, 'h', exp_lang='c')


def test_language_h_force_c(tmp_path):
    __test_language(tmp_path, 'h', force_lang='c', exp_lang='c')


def test_language_h_force_cpp(tmp_path):
    __test_language(tmp_path, 'h', force_lang='c++', exp_lang='cpp')


# files with unknown extensions default to C++
def test_language_unk(tmp_path):
    __test_language(tmp_path, 'src', exp_lang='cpp')


def test_language_unk_force_c(tmp_path):
    __test_language(tmp_path, 'src', force_lang='c', exp_lang='c')


def test_language_unk_force_cpp(tmp_path):
    __test_language(tmp_path, 'src', force_lang='c++', exp_lang='cpp')


def test_duplicate_file_entries(tmpdir):  #13333
    test_file = str(tmpdir / 'test.c')
    with open(test_file, 'wt') as f:
        f.write('x=1;\n')

    project_file = str(tmpdir / 'compile_commands.json')
    with open(project_file, 'wt') as f:
        f.write(json.dumps([{
               "file": test_file,
               "directory": str(tmpdir),
               "command": "cc -c test.c"
            },{
               "file": test_file,
               "directory": str(tmpdir),
               "command": "cc -c test.c"
            }]))

    args = ['--project=compile_commands.json', '--dump']
    _, _, _ = cppcheck(args, cwd=str(tmpdir))

    assert os.path.isfile(test_file + '.dump')
    assert os.path.isfile(test_file + '.1.dump')


def test_container_methods(tmpdir):
    test_file = str(tmpdir / 'test.cpp')
    with open(test_file, 'wt') as f:
        f.write('std::string s;\n')

    exitcode, _, stderr = cppcheck(['--dump', '.'], cwd=str(tmpdir))
    assert exitcode == 0, stderr

    dumpfile = test_file + '.dump'
    with open(dumpfile, 'rt') as f:
        dump = f.read()
    assert '<f name="emplace" action="push" yield="iterator"/>' in dump