|
5 | 5 | import subprocess |
6 | 6 | import time |
7 | 7 | import tempfile |
| 8 | +import pathlib |
8 | 9 |
|
9 | 10 | # Create Cppcheck project file |
10 | 11 | import sys |
@@ -205,11 +206,40 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_ |
205 | 206 |
|
206 | 207 | logging.info(exe + ' ' + ' '.join(args)) |
207 | 208 |
|
208 | | - run_subprocess = __run_subprocess_tty if tty else __run_subprocess |
209 | | - return_code, stdout, stderr = run_subprocess([exe] + args, env=env, cwd=cwd, timeout=timeout) |
| 209 | + def run_cppcheck(): |
| 210 | + run_subprocess = __run_subprocess_tty if tty else __run_subprocess |
| 211 | + rc, out, err = run_subprocess([exe] + args, env=env, cwd=cwd, timeout=timeout) |
210 | 212 |
|
211 | | - stdout = stdout.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
212 | | - stderr = stderr.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 213 | + out = out.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 214 | + err = err.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') |
| 215 | + |
| 216 | + return rc, out, err |
| 217 | + |
| 218 | + return_code, stdout, stderr = run_cppcheck() |
| 219 | + |
| 220 | + if 'TEST_CPPCHECK_INJECT_BUILDDIR' in os.environ: |
| 221 | + # run it again with the generated cache and make sure the output is identical |
| 222 | + |
| 223 | + def get_cache_contents(): |
| 224 | + content = {} |
| 225 | + for dirpath, dirnames, filenames in os.walk(builddir_tmp.name): |
| 226 | + for fn in filenames: |
| 227 | + content[os.path.join(dirpath, fn)] = (pathlib.Path(builddir_tmp.name) / dirpath / fn).read_text() |
| 228 | + for dn in dirnames: |
| 229 | + content[os.path.join(dirpath, dn)] = None |
| 230 | + return content |
| 231 | + |
| 232 | + cache_content = get_cache_contents() |
| 233 | + |
| 234 | + return_code_1, stdout_1, stderr_1 = run_cppcheck() |
| 235 | + |
| 236 | + assert return_code == return_code_1, 'exitcode different with cached results' |
| 237 | + assert stdout == stdout_1, 'stdout different with cached results' |
| 238 | + assert stderr == stderr_1, 'stderr different with cached results' |
| 239 | + |
| 240 | + cache_content_1 = get_cache_contents() |
| 241 | + |
| 242 | + assert cache_content == cache_content_1, 'cache contents changed in-between runs' |
213 | 243 |
|
214 | 244 | if builddir_tmp: |
215 | 245 | builddir_tmp.cleanup() |
|
0 commit comments