22import sys
33import pytest
44import shutil
5+ import json
56
67from testutils import cppcheck_ex , cppcheck , __lookup_cppcheck_exe
78
@@ -12,6 +13,48 @@ def __remove_std_lookup_log(l : list, exepath):
1213 return l
1314
1415
16+ def __create_gui_project (tmpdir ):
17+ file_name = 'test.c'
18+ test_file = os .path .join (tmpdir , file_name )
19+ with open (test_file , 'wt' ):
20+ pass
21+
22+ project_file = os .path .join (tmpdir , 'project.cppcheck' )
23+ with open (project_file , 'wt' ) as f :
24+ f .write (
25+ """<?xml version="1.0" encoding="UTF-8"?>
26+ <project version="1">
27+ <paths>
28+ <dir name="{}"/>
29+ </paths>
30+ </project>""" .format (test_file )
31+ )
32+
33+ return project_file , test_file
34+
35+
36+ def __create_compdb (tmpdir ):
37+ file_name = 'test.c'
38+ test_file = os .path .join (tmpdir , file_name )
39+ with open (test_file , 'wt' ):
40+ pass
41+
42+ compilation_db = [
43+ {
44+ "directory" : str (tmpdir ),
45+ "command" : "c++ -o {}.o -c {}" .format (os .path .basename (file_name ), file_name ),
46+ "file" : file_name ,
47+ "output" : "{}.o" .format (os .path .basename (file_name ))
48+ }
49+ ]
50+
51+ compile_commands = os .path .join (tmpdir , 'compile_commands.json' )
52+ with open (compile_commands , 'wt' ) as f :
53+ f .write (json .dumps (compilation_db ))
54+
55+ return compile_commands , test_file
56+
57+
1558def test_lib_lookup (tmpdir ):
1659 test_file = os .path .join (tmpdir , 'test.c' )
1760 with open (test_file , 'wt' ):
@@ -71,6 +114,46 @@ def test_lib_lookup_notfound(tmpdir):
71114 ]
72115
73116
117+ def test_lib_lookup_notfound_project (tmpdir ): # #13938
118+ project_file , _ = __create_gui_project (tmpdir )
119+
120+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=library' , '--library=none' , '--project={}' .format (project_file )])
121+ exepath = os .path .dirname (exe )
122+ if sys .platform == 'win32' :
123+ exepath = exepath .replace ('\\ ' , '/' )
124+ assert exitcode == 1 , stdout
125+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
126+ assert lines == [
127+ # TODO: needs to look relative to the project first
128+ # TODO: specify which folder is actually used for lookup here
129+ "looking for library 'none.cfg'" ,
130+ "looking for library '{}/none.cfg'" .format (exepath ),
131+ "looking for library '{}/cfg/none.cfg'" .format (exepath ),
132+ "library not found: 'none'" ,
133+ "cppcheck: Failed to load library configuration file 'none'. File not found"
134+ ]
135+
136+
137+ def test_lib_lookup_notfound_compdb (tmpdir ): # #13938
138+ compdb_file , _ = __create_compdb (tmpdir )
139+
140+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=library' , '--library=none' , '--project={}' .format (compdb_file )])
141+ exepath = os .path .dirname (exe )
142+ if sys .platform == 'win32' :
143+ exepath = exepath .replace ('\\ ' , '/' )
144+ assert exitcode == 1 , stdout
145+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
146+ assert lines == [
147+ # TODO: needs to look relative to the project first
148+ # TODO: specify which folder is actually used for lookup here
149+ "looking for library 'none.cfg'" ,
150+ "looking for library '{}/none.cfg'" .format (exepath ),
151+ "looking for library '{}/cfg/none.cfg'" .format (exepath ),
152+ "library not found: 'none'" ,
153+ "cppcheck: Failed to load library configuration file 'none'. File not found"
154+ ]
155+
156+
74157def test_lib_lookup_ext_notfound (tmpdir ):
75158 test_file = os .path .join (tmpdir , 'test.c' )
76159 with open (test_file , 'wt' ):
@@ -273,7 +356,7 @@ def test_platform_lookup(tmpdir):
273356 assert exitcode == 0 , stdout if stdout else stderr
274357 lines = stdout .splitlines ()
275358 assert lines == [
276- "looking for platform 'avr8' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
359+ "looking for platform 'avr8' relative to '{}'" .format (exepath_bin ),
277360 "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml" ,
278361 "try to load platform file 'platforms/avr8.xml' ... Success" ,
279362 'Checking {} ...' .format (test_file )
@@ -294,7 +377,7 @@ def test_platform_lookup_ext(tmpdir):
294377 assert exitcode == 0 , stdout if stdout else stderr
295378 lines = stdout .splitlines ()
296379 assert lines == [
297- "looking for platform 'avr8.xml' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
380+ "looking for platform 'avr8.xml' relative to '{}'" .format (exepath_bin ),
298381 "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml" ,
299382 "try to load platform file 'platforms/avr8.xml' ... Success" ,
300383 'Checking {} ...' .format (test_file )
@@ -315,7 +398,51 @@ def test_platform_lookup_notfound(tmpdir):
315398 assert exitcode == 1 , stdout
316399 lines = stdout .splitlines ()
317400 assert lines == [
318- "looking for platform 'none' in '{}'" .format (exepath_bin ), # TODO: this is not the path *of* the executable but the the path *to* the executable
401+ "looking for platform 'none' relative to '{}'" .format (exepath_bin ),
402+ "try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml" ,
403+ "try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml" ,
404+ "try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml" .format (exepath , exepath ),
405+ "try to load platform file '{}/platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/none.xml" .format (exepath , exepath ),
406+ "cppcheck: error: unrecognized platform: 'none'."
407+ ]
408+
409+
410+ def test_platform_lookup_notfound_project (tmpdir ): # #13939
411+ project_file , _ = __create_gui_project (tmpdir )
412+
413+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=platform' , '--platform=none' , '--project={}' .format (project_file )])
414+ exepath = os .path .dirname (exe )
415+ exepath_bin = os .path .join (exepath , 'cppcheck' )
416+ if sys .platform == 'win32' :
417+ exepath = exepath .replace ('\\ ' , '/' )
418+ exepath_bin += '.exe'
419+ assert exitcode == 1 , stdout
420+ lines = stdout .splitlines ()
421+ assert lines == [
422+ # TODO: needs to look relative to project file first
423+ "looking for platform 'none' relative to '{}'" .format (exepath_bin ),
424+ "try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml" ,
425+ "try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml" ,
426+ "try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml" .format (exepath , exepath ),
427+ "try to load platform file '{}/platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/none.xml" .format (exepath , exepath ),
428+ "cppcheck: error: unrecognized platform: 'none'."
429+ ]
430+
431+
432+ def test_platform_lookup_notfound_compdb (tmpdir ): # #13939
433+ compdb_file , _ = __create_compdb (tmpdir )
434+
435+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=platform' , '--platform=none' , '--project={}' .format (compdb_file )])
436+ exepath = os .path .dirname (exe )
437+ exepath_bin = os .path .join (exepath , 'cppcheck' )
438+ if sys .platform == 'win32' :
439+ exepath = exepath .replace ('\\ ' , '/' )
440+ exepath_bin += '.exe'
441+ assert exitcode == 1 , stdout
442+ lines = stdout .splitlines ()
443+ assert lines == [
444+ # TODO: needs to look relative to project file first
445+ "looking for platform 'none' relative to '{}'" .format (exepath_bin ),
319446 "try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml" ,
320447 "try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml" ,
321448 "try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml" .format (exepath , exepath ),
@@ -338,7 +465,7 @@ def test_platform_lookup_ext_notfound(tmpdir):
338465 assert exitcode == 1 , stdout if stdout else stderr
339466 lines = stdout .splitlines ()
340467 assert lines == [
341- "looking for platform 'none.xml' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
468+ "looking for platform 'none.xml' relative to '{}'" .format (exepath_bin ),
342469 "try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml" ,
343470 "try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml" ,
344471 "try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml" .format (exepath , exepath ),
@@ -361,7 +488,7 @@ def test_platform_lookup_relative_notfound(tmpdir):
361488 assert exitcode == 1 , stdout if stdout else stderr
362489 lines = stdout .splitlines ()
363490 assert lines == [
364- "looking for platform 'platform/none.xml' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
491+ "looking for platform 'platform/none.xml' relative to '{}'" .format (exepath_bin ),
365492 "try to load platform file 'platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platform/none.xml" ,
366493 "try to load platform file 'platforms/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/platform/none.xml" ,
367494 "try to load platform file '{}/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platform/none.xml" .format (exepath , exepath ),
@@ -384,7 +511,7 @@ def test_platform_lookup_relative_noext_notfound(tmpdir):
384511 assert exitcode == 1 , stdout if stdout else stderr
385512 lines = stdout .splitlines ()
386513 assert lines == [
387- "looking for platform 'platform/none' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
514+ "looking for platform 'platform/none' relative to '{}'" .format (exepath_bin ),
388515 "try to load platform file 'platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platform/none.xml" ,
389516 "try to load platform file 'platforms/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/platform/none.xml" ,
390517 "try to load platform file '{}/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platform/none.xml" .format (exepath , exepath ),
@@ -412,7 +539,7 @@ def test_platform_lookup_absolute(tmpdir):
412539 assert exitcode == 0 , stdout if stdout else stderr
413540 lines = stdout .splitlines ()
414541 assert lines == [
415- "looking for platform '{}' in '{}'" .format (platform_file , exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
542+ "looking for platform '{}' relative to '{}'" .format (platform_file , exepath_bin ),
416543 "try to load platform file '{}' ... Success" .format (platform_file ),
417544 'Checking {} ...' .format (test_file )
418545 ]
@@ -433,7 +560,7 @@ def test_platform_lookup_absolute_notfound(tmpdir):
433560 assert exitcode == 1 , stdout if stdout else stderr
434561 lines = stdout .splitlines ()
435562 assert lines == [
436- "looking for platform '{}' in '{}'" .format (platform_file , exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
563+ "looking for platform '{}' relative to '{}'" .format (platform_file , exepath_bin ),
437564 "try to load platform file '{}' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}" .format (platform_file , platform_file ),
438565 "cppcheck: error: unrecognized platform: '{}'." .format (platform_file )
439566 ]
@@ -457,7 +584,7 @@ def test_platform_lookup_nofile(tmpdir):
457584 assert exitcode == 0 , stdout if stdout else stderr
458585 lines = stdout .splitlines ()
459586 assert lines == [
460- "looking for platform 'avr8' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
587+ "looking for platform 'avr8' relative to '{}'" .format (exepath_bin ),
461588 "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml" ,
462589 "try to load platform file 'platforms/avr8.xml' ... Success" ,
463590 'Checking {} ...' .format (test_file )
@@ -481,7 +608,7 @@ def test_platform_lookup_invalid(tmpdir):
481608 assert exitcode == 1 , stdout if stdout else stderr
482609 lines = stdout .splitlines ()
483610 assert lines == [
484- "looking for platform 'avr8' in '{}'" .format (exepath_bin ), # TODO: this not not the path *of* the executable but the the path *to* the executable
611+ "looking for platform 'avr8' relative to '{}'" .format (exepath_bin ),
485612 "try to load platform file 'avr8.xml' ... Error=XML_ERROR_PARSING_TEXT ErrorID=8 (0x8) Line number=1" ,
486613 "cppcheck: error: unrecognized platform: 'avr8'."
487614 ]
@@ -541,6 +668,41 @@ def test_addon_lookup_notfound(tmpdir):
541668 ]
542669
543670
671+ @pytest .mark .xfail (strict = True ) # TODO: no addon lookup is being performed at all
672+ def test_addon_lookup_notfound_project (tmpdir ): # #13940 / #13941
673+ project_file , _ = __create_gui_project (tmpdir )
674+
675+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=none' , '--project={}' .format (project_file )])
676+ exepath = os .path .dirname (exe )
677+ exepath_sep = exepath + os .path .sep
678+ assert exitcode == 0 , stdout
679+ lines = stdout .splitlines ()
680+ assert lines == [
681+ # TODO: needs to look relative to the project file first
682+ "looking for addon 'none.py'" ,
683+ "looking for addon '{}none.py'" .format (exepath_sep ),
684+ "looking for addon '{}addons/none.py'" .format (exepath_sep ), # TODO: mixed separators
685+ 'Did not find addon none.py'
686+ ]
687+
688+
689+ def test_addon_lookup_notfound_compdb (tmpdir ): # #13940
690+ compdb_file , _ = __create_compdb (tmpdir )
691+
692+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=none' , '--project={}' .format (compdb_file )])
693+ exepath = os .path .dirname (exe )
694+ exepath_sep = exepath + os .path .sep
695+ assert exitcode == 1 , stdout
696+ lines = stdout .splitlines ()
697+ assert lines == [
698+ # TODO: needs to look relative to the project file first
699+ "looking for addon 'none.py'" ,
700+ "looking for addon '{}none.py'" .format (exepath_sep ),
701+ "looking for addon '{}addons/none.py'" .format (exepath_sep ), # TODO: mixed separators
702+ 'Did not find addon none.py'
703+ ]
704+
705+
544706def test_addon_lookup_ext_notfound (tmpdir ):
545707 test_file = os .path .join (tmpdir , 'test.c' )
546708 with open (test_file , 'wt' ):
@@ -715,6 +877,7 @@ def test_config_lookup_notfound(tmpdir):
715877 'Checking {} ...' .format (test_file )
716878 ]
717879
880+
718881def test_config_invalid (tmpdir ):
719882 cppcheck_exe = __lookup_cppcheck_exe ()
720883 bin_dir = os .path .dirname (cppcheck_exe )
0 commit comments