55import subprocess
66import time
77import tempfile
8+ import pathlib
89
910# Create Cppcheck project file
1011import sys
@@ -189,6 +190,7 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_
189190
190191 builddir_tmp = None
191192
193+ # TODO: add verify mode and run it twice and compre the results
192194 if 'TEST_CPPCHECK_INJECT_BUILDDIR' in os .environ :
193195 found_builddir = False
194196 for arg in args :
@@ -202,11 +204,40 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_
202204
203205 logging .info (exe + ' ' + ' ' .join (args ))
204206
205- run_subprocess = __run_subprocess_tty if tty else __run_subprocess
206- return_code , stdout , stderr = run_subprocess ([exe ] + args , env = env , cwd = cwd , timeout = timeout )
207+ def run_cppcheck ():
208+ run_subprocess = __run_subprocess_tty if tty else __run_subprocess
209+ rc , out , err = run_subprocess ([exe ] + args , env = env , cwd = cwd , timeout = timeout )
207210
208- stdout = stdout .decode (encoding = 'utf-8' , errors = 'ignore' ).replace ('\r \n ' , '\n ' )
209- stderr = stderr .decode (encoding = 'utf-8' , errors = 'ignore' ).replace ('\r \n ' , '\n ' )
211+ out = out .decode (encoding = 'utf-8' , errors = 'ignore' ).replace ('\r \n ' , '\n ' )
212+ err = err .decode (encoding = 'utf-8' , errors = 'ignore' ).replace ('\r \n ' , '\n ' )
213+
214+ return rc , out , err
215+
216+ return_code , stdout , stderr = run_cppcheck ()
217+
218+ if 'TEST_CPPCHECK_INJECT_BUILDDIR' in os .environ :
219+ # run it again with the generated cache and make sure the output is identical
220+
221+ def get_cache_contents ():
222+ content = {}
223+ for dirpath , dirnames , filenames in os .walk (builddir_tmp .name ):
224+ for fn in filenames :
225+ content [os .path .join (dirpath , fn )] = (pathlib .Path (builddir_tmp .name ) / dirpath / fn ).read_text ()
226+ for dn in dirnames :
227+ content [os .path .join (dirpath , dn )] = None
228+ return content
229+
230+ cache_content = get_cache_contents ()
231+
232+ return_code_1 , stdout_1 , stderr_1 = run_cppcheck ()
233+
234+ assert return_code == return_code_1 , 'exitcode different with cached results'
235+ assert stdout == stdout_1 , 'stdout different with cached results'
236+ assert stderr == stderr_1 , 'stderr different with cached results'
237+
238+ cache_content_1 = get_cache_contents ()
239+
240+ assert cache_content == cache_content_1 , 'cache contents changed in-between runs'
210241
211242 if builddir_tmp :
212243 builddir_tmp .cleanup ()
0 commit comments