@@ -381,6 +381,36 @@ def test_platform_lookup_ext(tmpdir):
381381 ]
382382
383383
384+ def test_platform_lookup_path (tmpdir ):
385+ test_file = os .path .join (tmpdir , 'test.c' )
386+ with open (test_file , 'wt' ):
387+ pass
388+
389+ cppcheck = 'cppcheck' # No path
390+ path = os .path .dirname (__lookup_cppcheck_exe ())
391+ env = os .environ .copy ()
392+ env ['PATH' ] = path
393+ exitcode , stdout , stderr , _ = cppcheck_ex (args = ['--debug-lookup=platform' , '--platform=avr8.xml' , test_file ], cppcheck_exe = cppcheck , cwd = str (tmpdir ), env = env )
394+ assert exitcode == 0 , stdout if stdout else stderr
395+ def format_path (p ):
396+ return p .replace ('\\ ' , '/' ).replace ('"' , '\' ' )
397+ def try_fail (f ):
398+ f = format_path (f )
399+ return "try to load platform file '{}' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}" .format (f , f )
400+ def try_success (f ):
401+ f = format_path (f )
402+ return "try to load platform file '{}' ... Success" .format (f )
403+ lines = stdout .replace ('\\ ' , '/' ).replace ('"' , '\' ' ).splitlines ()
404+ assert lines == [
405+ "looking for platform 'avr8.xml'" ,
406+ try_fail (os .path .join (tmpdir , 'avr8.xml' )),
407+ try_fail (os .path .join (tmpdir , 'platforms' , 'avr8.xml' )),
408+ try_fail (os .path .join (path , 'avr8.xml' )),
409+ try_success (os .path .join (path , 'platforms' , 'avr8.xml' )),
410+ 'Checking {} ...' .format (format_path (test_file ))
411+ ]
412+
413+
384414def test_platform_lookup_notfound (tmpdir ):
385415 test_file = os .path .join (tmpdir , 'test.c' )
386416 with open (test_file , 'wt' ):
@@ -897,4 +927,76 @@ def test_config_invalid(tmpdir):
897927 'cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 1 near: '
898928 ]
899929
900- # TODO: test with FILESDIR
930+ # TODO: test with FILESDIR
931+
932+ @pytest .mark .parametrize ("type,file" , [("addon" , "misra.py" ), ("config" , "cppcheck.cfg" ), ("library" , "gnu.cfg" ), ("platform" , "avr8.xml" )])
933+ def test_lookup_path (tmpdir , type , file ):
934+ test_file = os .path .join (tmpdir , 'test.c' )
935+ with open (test_file , 'wt' ):
936+ pass
937+
938+ cppcheck = 'cppcheck' # No path
939+ path = os .path .dirname (__lookup_cppcheck_exe ())
940+ env = os .environ .copy ()
941+ env ['PATH' ] = path + (';' if sys .platform == 'win32' else ':' ) + env .get ('PATH' , '' )
942+ if type == 'config' :
943+ with open (os .path .join (path , "cppcheck.cfg" ), 'wt' ) as f :
944+ f .write ('{}' )
945+ exitcode , stdout , stderr , _ = cppcheck_ex (args = [f'--debug-lookup={ type } ' , test_file ], cppcheck_exe = cppcheck , cwd = str (tmpdir ), env = env )
946+ os .remove (os .path .join (path , "cppcheck.cfg" )) # clean up otherwise other tests may fail
947+ else :
948+ exitcode , stdout , stderr , _ = cppcheck_ex (args = [f'--debug-lookup={ type } ' , f'--{ type } ={ file } ' , test_file ], cppcheck_exe = cppcheck , cwd = str (tmpdir ), env = env )
949+ assert exitcode == 0 , stdout if stdout else stderr
950+ def format_path (p ):
951+ return p .replace ('\\ ' , '/' ).replace ('"' , '\' ' )
952+ lines = format_path (stdout ).splitlines ()
953+
954+ if type == 'addon' :
955+ def try_fail (f ):
956+ return f"looking for { type } '{ format_path (f )} '"
957+ def try_success (f ):
958+ return f"looking for { type } '{ format_path (f )} '"
959+ assert lines == [
960+ f"looking for { type } '{ file } '" ,
961+ try_fail (os .path .join (path , file )),
962+ try_success (os .path .join (path , 'addons' , file )),
963+ f'Checking { format_path (test_file )} ...'
964+ ]
965+ elif type == 'config' :
966+ def try_success (f ):
967+ return f"looking for '{ format_path (f )} '"
968+ assert lines == [
969+ try_success (os .path .join (path , file )),
970+ f'Checking { format_path (test_file )} ...'
971+ ]
972+ elif type == 'platform' :
973+ def try_fail (f ):
974+ f = format_path (f )
975+ return f"try to load { type } file '{ f } ' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={ f } "
976+ def try_success (f ):
977+ f = format_path (f )
978+ return f"try to load { type } file '{ f } ' ... Success"
979+ assert lines == [
980+ f"looking for { type } '{ file } '" ,
981+ try_fail (os .path .join (tmpdir , file )),
982+ try_fail (os .path .join (tmpdir , 'platforms' , file )),
983+ try_fail (os .path .join (path , file )),
984+ try_success (os .path .join (path , 'platforms' , file )),
985+ f'Checking { format_path (test_file )} ...'
986+ ]
987+ elif type == 'library' :
988+ def try_fail (f ):
989+ return f"looking for { type } '{ format_path (f )} '"
990+ def try_success (f ):
991+ return f"looking for { type } '{ format_path (f )} '"
992+ assert lines == [
993+ f"looking for { type } 'std.cfg'" ,
994+ try_fail (os .path .join (path , 'std.cfg' )),
995+ try_success (os .path .join (path , 'cfg' , 'std.cfg' )),
996+ f"looking for { type } '{ file } '" ,
997+ try_fail (os .path .join (path , file )),
998+ try_success (os .path .join (path , 'cfg' , file )),
999+ f'Checking { format_path (test_file )} ...'
1000+ ]
1001+ else :
1002+ assert False , type + " not tested properly"
0 commit comments