11import os
22import sys
33import pytest
4+ import shutil
45
5- from testutils import cppcheck_ex , cppcheck
6+ from testutils import cppcheck_ex , cppcheck , __lookup_cppcheck_exe
67
78def __remove_std_lookup_log (l : list , exepath ):
89 l .remove ("looking for library 'std.cfg'" )
@@ -63,9 +64,7 @@ def test_lib_lookup_notfound(tmpdir):
6364 assert lines == [
6465 # TODO: specify which folder is actually used for lookup here
6566 "looking for library 'none.cfg'" ,
66- # TODO: lookup of '{exepath}/none' missing - could conflict with the platform lookup though
6767 "looking for library '{}/none.cfg'" .format (exepath ),
68- # TODO: lookup of '{exepath}/cfg/none' missing
6968 "looking for library '{}/cfg/none.cfg'" .format (exepath ),
7069 "library not found: 'none'" ,
7170 "cppcheck: Failed to load library configuration file 'none'. File not found"
@@ -260,7 +259,7 @@ def test_platform_lookup_builtin(tmpdir):
260259 ]
261260
262261
263- @pytest .mark .skip # TODO: perform additional lookups when run via symlink in CI
262+ @pytest .mark .skip # TODO: performs additional lookups when run via symlink in CI
264263def test_platform_lookup (tmpdir ):
265264 test_file = os .path .join (tmpdir , 'test.c' )
266265 with open (test_file , 'wt' ):
@@ -281,7 +280,7 @@ def test_platform_lookup(tmpdir):
281280 ]
282281
283282
284- @pytest .mark .skip # TODO: perform additional lookups when run via symlink in CI
283+ @pytest .mark .skip # TODO: performs additional lookups when run via symlink in CI
285284def test_platform_lookup_ext (tmpdir ):
286285 test_file = os .path .join (tmpdir , 'test.c' )
287286 with open (test_file , 'wt' ):
@@ -440,7 +439,7 @@ def test_platform_lookup_absolute_notfound(tmpdir):
440439 ]
441440
442441
443- @pytest .mark .skip # TODO: perform additional lookups when run via symlink in CI
442+ @pytest .mark .skip # TODO: performs additional lookups when run via symlink in CI
444443def test_platform_lookup_nofile (tmpdir ):
445444 test_file = os .path .join (tmpdir , 'test.c' )
446445 with open (test_file , 'wt' ):
@@ -648,7 +647,7 @@ def test_addon_lookup_nofile(tmpdir):
648647 exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=misra' , test_file ])
649648 exepath = os .path .dirname (exe )
650649 exepath_sep = exepath + os .path .sep
651- assert exitcode == 0 , stdout if stdout else stderr # TODO. should fail when addon is not found
650+ assert exitcode == 0 , stdout if stdout else stderr
652651 lines = stdout .splitlines ()
653652 assert lines == [
654653 "looking for addon 'misra.py'" ,
@@ -677,25 +676,29 @@ def test_addon_lookup_invalid(tmpdir):
677676 ]
678677
679678
680- @pytest .mark .skip # TODO
681679def test_config_lookup (tmpdir ):
680+ cppcheck_exe = __lookup_cppcheck_exe ()
681+ bin_dir = os .path .dirname (cppcheck_exe )
682+ tmp_cppcheck_exe = shutil .copy2 (cppcheck_exe , tmpdir )
683+ if sys .platform == 'win32' :
684+ shutil .copy2 (os .path .join (bin_dir , 'cppcheck-core.dll' ), tmpdir )
685+ shutil .copytree (os .path .join (bin_dir , 'cfg' ), os .path .join (tmpdir , 'cfg' ))
686+
682687 test_file = os .path .join (tmpdir , 'test.c' )
683688 with open (test_file , 'wt' ):
684689 pass
685690
686- # TODO: needs to be in exepath so this is found
687691 config_file = os .path .join (tmpdir , 'cppcheck.cfg' )
688- with open (config_file , 'wt' ):
689- pass
692+ with open (config_file , 'wt' ) as f :
693+ f . write ( '{}' )
690694
691- exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=config' , '--addon=misra' , test_file ], cwd = tmpdir )
695+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=config' , test_file ], cwd = tmpdir , cppcheck_exe = tmp_cppcheck_exe )
692696 exepath = os .path .dirname (exe )
693697 exepath_sep = exepath + os .path .sep
694698 assert exitcode == 0 , stdout if stdout else stderr
695699 lines = stdout .splitlines ()
696700 assert lines == [
697701 "looking for '{}cppcheck.cfg'" .format (exepath_sep ),
698- 'no configuration found' ,
699702 'Checking {} ...' .format (test_file )
700703 ]
701704
@@ -716,6 +719,30 @@ def test_config_lookup_notfound(tmpdir):
716719 'Checking {} ...' .format (test_file )
717720 ]
718721
719- # TODO: test handling of invalid configuration
722+ def test_config_invalid (tmpdir ):
723+ cppcheck_exe = __lookup_cppcheck_exe ()
724+ bin_dir = os .path .dirname (cppcheck_exe )
725+ tmp_cppcheck_exe = shutil .copy2 (cppcheck_exe , tmpdir )
726+ if sys .platform == 'win32' :
727+ shutil .copy2 (os .path .join (bin_dir , 'cppcheck-core.dll' ), tmpdir )
728+ shutil .copytree (os .path .join (bin_dir , 'cfg' ), os .path .join (tmpdir , 'cfg' ))
729+
730+ test_file = os .path .join (tmpdir , 'test.c' )
731+ with open (test_file , 'wt' ):
732+ pass
733+
734+ config_file = os .path .join (tmpdir , 'cppcheck.cfg' )
735+ with open (config_file , 'wt' ):
736+ pass
737+
738+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=config' , test_file ], cwd = tmpdir , cppcheck_exe = tmp_cppcheck_exe )
739+ exepath = os .path .dirname (exe )
740+ exepath_sep = exepath + os .path .sep
741+ assert exitcode == 1 , stdout if stdout else stderr
742+ lines = stdout .splitlines ()
743+ assert lines == [
744+ "looking for '{}cppcheck.cfg'" .format (exepath_sep ),
745+ 'cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 1 near: '
746+ ]
720747
721748# TODO: test with FILESDIR
0 commit comments