File: project_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 (159 lines) | stat: -rw-r--r-- 5,443 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# python -m pytest test-projects.py

import pytest
import os
import json
import sys
from testutils import cppcheck


@pytest.mark.parametrize("project_ext", ["json", "sln", "vcxproj", "bpr", "cppcheck"])
def test_missing_project(project_ext):
    project_file = "file.{}".format(project_ext)

    ret, stdout, stderr = cppcheck(['--project=' + project_file, '--template=cppcheck1'])
    assert 1 == ret
    assert "cppcheck: error: failed to open project '{}'. The file does not exist.\n".format(project_file) == stdout
    assert "" == stderr


def __test_project_error(tmpdir, ext, content, expected):
    project_file = os.path.join(tmpdir, "file.{}".format(ext))

    with open(project_file, 'w') as f:
        if content is not None:
            f.write(content)

    ret, stdout, stderr = cppcheck(['--project=' + str(project_file)])
    assert 1 == ret
    assert "cppcheck: error: " + expected + "\ncppcheck: error: failed to load project '{}'. An error occurred.\n".format(project_file) == stdout
    assert "" == stderr


@pytest.mark.parametrize("project_ext, expected", [
    ("json", "compilation database is not a JSON array"),
    ("sln", "Visual Studio solution file is empty"),
    ("vcxproj", "Visual Studio project file is not a valid XML - XML_ERROR_EMPTY_DOCUMENT"),
    ("bpr", "Borland project file is not a valid XML - XML_ERROR_EMPTY_DOCUMENT"),
    ("cppcheck", "Cppcheck GUI project file is not a valid XML - XML_ERROR_EMPTY_DOCUMENT")
])
def test_empty_project(tmpdir, project_ext, expected):
    __test_project_error(tmpdir, project_ext, None, expected)


def test_json_entry_file_not_found(tmpdir):
    compilation_db = [
        {"directory": str(tmpdir),
         "command": "c++ -o bug1.o -c bug1.cpp",
         "file": "bug1.cpp",
         "output": "bug1.o"}
    ]

    project_file = os.path.join(tmpdir, "file.json")
    missing_file = os.path.join(tmpdir, "bug1.cpp")
    missing_file_posix = missing_file

    if sys.platform == "win32":
        missing_file_posix = missing_file_posix.replace('\\', '/')

    with open(project_file, 'w') as f:
        f.write(json.dumps(compilation_db))

    ret, _, stderr = cppcheck(["--project=" + str(project_file)])
    assert 0 == ret
    assert f"{missing_file}:1:0: error: File is missing: {missing_file_posix} [syntaxError]\n\n^\n" == stderr


def test_json_no_arguments(tmpdir):
    compilation_db = [
        {"directory": str(tmpdir),
         "file": "bug1.cpp",
         "output": "bug1.o"}
    ]

    expected = "no 'arguments' or 'command' field found in compilation database entry"

    __test_project_error(tmpdir, "json", json.dumps(compilation_db), expected)


def test_json_invalid_arguments(tmpdir):
    compilation_db = [
        {"directory": str(tmpdir),
         "arguments": "",
         "file": "bug1.cpp",
         "output": "bug1.o"}
    ]

    expected = "'arguments' field in compilation database entry is not a JSON array"

    __test_project_error(tmpdir, "json", json.dumps(compilation_db), expected)


def test_sln_invalid_file(tmpdir):
    content = "some file"

    expected = "Visual Studio solution file header not found"

    __test_project_error(tmpdir, "sln", content, expected)


def test_sln_no_header(tmpdir):
    content = "\xEF\xBB\xBF\r\n" \
              "some header"

    expected = "Visual Studio solution file header not found"

    __test_project_error(tmpdir, "sln", content, expected)


def test_sln_no_projects(tmpdir):
    content = "\xEF\xBB\xBF\r\n" \
              "Microsoft Visual Studio Solution File, Format Version 12.00\r\n"

    expected = "no projects found in Visual Studio solution file"

    __test_project_error(tmpdir, "sln", content, expected)


def test_sln_project_file_not_found(tmpdir):
    content = "\xEF\xBB\xBF\r\n" \
              "Microsoft Visual Studio Solution File, Format Version 12.00\r\n" \
              "# Visual Studio Version 16\r\n" \
              "VisualStudioVersion = 16.0.29020.237\r\n" \
              "MinimumVisualStudioVersion = 10.0.40219.1\r\n" \
              'Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cli", "cli\\cli.vcxproj", "{35CBDF51-2456-3EC3-99ED-113C30858883}"\r\n' \
              "ProjectSection(ProjectDependencies) = postProject\r\n" \
              "{C183DB5B-AD6C-423D-80CA-1F9549555A1A} = {C183DB5B-AD6C-423D-80CA-1F9549555A1A}\r\n" \
              "EndProjectSection\r\n" \
              "EndProject\r\n"

    expected = "Visual Studio project file is not a valid XML - XML_ERROR_FILE_NOT_FOUND\n" \
               "cppcheck: error: failed to load '{}' from Visual Studio solution".format(os.path.join(tmpdir, "cli/cli.vcxproj"))
    if sys.platform == "win32":
        expected = expected.replace('\\', '/')

    __test_project_error(tmpdir, "sln", content, expected)


def test_vcxproj_no_xml_root(tmpdir):
    content = '<?xml version="1.0" encoding="utf-8"?>'

    expected = "Visual Studio project file has no XML root node"

    __test_project_error(tmpdir, "vcxproj", content, expected)


def test_bpr_no_xml_root(tmpdir):
    content = '<?xml version="1.0" encoding="utf-8"?>'

    expected = "Borland project file has no XML root node"

    __test_project_error(tmpdir, "bpr", content, expected)


def test_cppcheck_no_xml_root(tmpdir):
    content = '<?xml version="1.0" encoding="utf-8"?>'

    expected = "Cppcheck GUI project file has no XML root node"

    __test_project_error(tmpdir, "cppcheck", content, expected)