pytest-cppcheck 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,98 @@
1
+ import subprocess
2
+
3
+ import pytest
4
+ from cppcheck import get_cppcheck_dir
5
+
6
+ CPPCHECK_BIN = str(get_cppcheck_dir() / "cppcheck")
7
+ CACHE_KEY = "cppcheck/mtimes"
8
+
9
+
10
+ def pytest_addoption(parser):
11
+ group = parser.getgroup("cppcheck", "cppcheck static analysis")
12
+ group.addoption(
13
+ "--cppcheck",
14
+ action="store_true",
15
+ default=False,
16
+ help="run cppcheck on C/C++ source files",
17
+ )
18
+ parser.addini(
19
+ "cppcheck_extensions",
20
+ type="args",
21
+ default=[".c", ".cpp"],
22
+ help="file extensions to collect for cppcheck (default: .c .cpp)",
23
+ )
24
+ parser.addini(
25
+ "cppcheck_args",
26
+ type="args",
27
+ default=[],
28
+ help="extra arguments forwarded to every cppcheck invocation",
29
+ )
30
+
31
+
32
+ def pytest_configure(config):
33
+ if not config.getoption("cppcheck"):
34
+ return
35
+ cache = getattr(config, "cache", None)
36
+ config._cppcheck_mtimes = cache.get(CACHE_KEY, {}) if cache else {}
37
+
38
+
39
+ def pytest_unconfigure(config):
40
+ mtimes = getattr(config, "_cppcheck_mtimes", None)
41
+ if mtimes is None:
42
+ return
43
+ cache = getattr(config, "cache", None)
44
+ if cache:
45
+ cache.set(CACHE_KEY, mtimes)
46
+
47
+
48
+ def pytest_collect_file(parent, file_path):
49
+ if not parent.config.getoption("cppcheck"):
50
+ return None
51
+ extensions = parent.config.getini("cppcheck_extensions")
52
+ if file_path.suffix in extensions:
53
+ return CppcheckFile.from_parent(parent, path=file_path)
54
+ return None
55
+
56
+
57
+ class CppcheckError(Exception):
58
+ pass
59
+
60
+
61
+ class CppcheckFile(pytest.File):
62
+ def collect(self):
63
+ yield CppcheckItem.from_parent(self, name="CPPCHECK")
64
+
65
+
66
+ class CppcheckItem(pytest.Item):
67
+ def setup(self):
68
+ mtimes = getattr(self.config, "_cppcheck_mtimes", {})
69
+ self._mtime = self.path.stat().st_mtime_ns
70
+ args = self.config.getini("cppcheck_args")
71
+ old = mtimes.get(str(self.path))
72
+ if old == [self._mtime, args]:
73
+ pytest.skip("previously passed cppcheck")
74
+
75
+ def runtest(self):
76
+ args = self.config.getini("cppcheck_args")
77
+ cmd = [CPPCHECK_BIN, "--quiet", "--error-exitcode=1"] + args + [str(self.path)]
78
+ result = subprocess.run(cmd, capture_output=True, text=True)
79
+ if result.returncode != 0:
80
+ output = result.stderr or result.stdout
81
+ if not output:
82
+ output = f"cppcheck exited with code {result.returncode}"
83
+ raise CppcheckError(output)
84
+ # Cache only on success
85
+ if hasattr(self.config, "_cppcheck_mtimes"):
86
+ self._mtime = getattr(self, "_mtime", self.path.stat().st_mtime_ns)
87
+ self.config._cppcheck_mtimes[str(self.path)] = [
88
+ self._mtime,
89
+ args,
90
+ ]
91
+
92
+ def repr_failure(self, excinfo):
93
+ if excinfo.errisinstance(CppcheckError):
94
+ return str(excinfo.value)
95
+ return super().repr_failure(excinfo)
96
+
97
+ def reportinfo(self):
98
+ return self.path, None, f"{self.path}::CPPCHECK"
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-cppcheck
3
+ Version: 0.1.0
4
+ Summary: A pytest plugin that runs cppcheck static analysis on C/C++ source files
5
+ Classifier: Framework :: Pytest
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Requires-Python: >=3.8
9
+ Requires-Dist: pytest>=7.0
10
+ Requires-Dist: cppcheck>=1.4.0
@@ -0,0 +1,7 @@
1
+ pytest_cppcheck/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ pytest_cppcheck/plugin.py,sha256=SwfmtFBUHmN2dsBqUObT9dimHSfvuICkJqRULpsaSYY,2992
3
+ pytest_cppcheck-0.1.0.dist-info/METADATA,sha256=QV4jJVfgKs5TLtV4V8Wudn2Q2PkLZtM9m6GzkA-YhNI,353
4
+ pytest_cppcheck-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
5
+ pytest_cppcheck-0.1.0.dist-info/entry_points.txt,sha256=okwinEioXu1FDs5BTe5_Q_-aN7P5Os3fbfNC9OZivmY,45
6
+ pytest_cppcheck-0.1.0.dist-info/top_level.txt,sha256=O_lMHFWMjhxZRGjVXmDQkOZAqQPzFIzetw6RgRWllI0,16
7
+ pytest_cppcheck-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [pytest11]
2
+ cppcheck = pytest_cppcheck.plugin
@@ -0,0 +1 @@
1
+ pytest_cppcheck