Skip to content

Commit 43c64dc

Browse files
committed
re-run analysis in Python tests with cached results when TEST_CPPCHECK_INJECT_BUILDDIR is specified
1 parent 83fd311 commit 43c64dc

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

test/cli/testutils.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import time
77
import tempfile
8+
import pathlib
89

910
# Create Cppcheck project file
1011
import sys
@@ -205,11 +206,40 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_
205206

206207
logging.info(exe + ' ' + ' '.join(args))
207208

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)
210212

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'
213243

214244
if builddir_tmp:
215245
builddir_tmp.cleanup()

0 commit comments

Comments
 (0)