1+ import os
2+ import sys
3+ import pytest
4+
5+ from testutils import cppcheck_ex , cppcheck
6+
7+ def __remove_std_lookup_log (l : list , exepath ):
8+ l .remove ("looking for library 'std.cfg'" )
9+ l .remove ("looking for library '{}/std.cfg'" .format (exepath ))
10+ l .remove ("looking for library '{}/cfg/std.cfg'" .format (exepath ))
11+ return l
12+
13+
14+ def __remove_verbose_log (l : list ):
15+ l .remove ('Defines:' )
16+ l .remove ('Undefines:' )
17+ l .remove ('Includes:' )
18+ l .remove ('Platform:native' )
19+ return l
20+
21+
22+ # TODO: test with FILESDIR
23+ def test_lib_lookup (tmpdir ):
24+ test_file = os .path .join (tmpdir , 'test.c' )
25+ with open (test_file , 'wt' ):
26+ pass
27+
28+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=library' , '--library=gnu' , test_file ])
29+ exepath = os .path .dirname (exe )
30+ if sys .platform == 'win32' :
31+ exepath = exepath .replace ('\\ ' , '/' )
32+ assert exitcode == 0 , stdout if stdout else stderr
33+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
34+ assert lines == [
35+ "looking for library 'gnu'" ,
36+ "looking for library 'gnu.cfg'" ,
37+ "looking for library '{}/gnu.cfg'" .format (exepath ),
38+ "looking for library '{}/cfg/gnu.cfg'" .format (exepath ),
39+ 'Checking {} ...' .format (test_file )
40+ ]
41+
42+
43+ # TODO: test with FILESDIR
44+ def test_lib_lookup_notfound (tmpdir ):
45+ test_file = os .path .join (tmpdir , 'test.c' )
46+ with open (test_file , 'wt' ):
47+ pass
48+
49+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=library' , '--library=none' , test_file ])
50+ exepath = os .path .dirname (exe )
51+ if sys .platform == 'win32' :
52+ exepath = exepath .replace ('\\ ' , '/' )
53+ assert exitcode == 1 , stdout
54+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
55+ assert lines == [
56+ # TODO: specify which folder is actually used for lookup here
57+ "looking for library 'none'" , # TODO: this could conflict with the platform lookup
58+ "looking for library 'none.cfg'" ,
59+ # TODO: lookup of '{exepath}/none' missing - could conflict with the platform lookup though
60+ "looking for library '{}/none.cfg'" .format (exepath ),
61+ # TODO: lookup of '{exepath}/cfg/none' missing
62+ "looking for library '{}/cfg/none.cfg'" .format (exepath ),
63+ "library not found: 'none'" ,
64+ "cppcheck: Failed to load library configuration file 'none'. File not found"
65+ ]
66+
67+
68+ def test_lib_lookup_absolute (tmpdir ):
69+ test_file = os .path .join (tmpdir , 'test.c' )
70+ with open (test_file , 'wt' ):
71+ pass
72+
73+ cfg_file = os .path .join (tmpdir , 'test.cfg' )
74+ with open (cfg_file , 'wt' ) as f :
75+ f .write ('''
76+ <?xml version="1.0"?>
77+ <def format="2">
78+ </def>
79+ ''' )
80+
81+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=library' , '--library={}' .format (cfg_file ), test_file ])
82+ exepath = os .path .dirname (exe )
83+ if sys .platform == 'win32' :
84+ exepath = exepath .replace ('\\ ' , '/' )
85+ assert exitcode == 0 , stdout if stdout else stderr
86+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
87+ assert lines == [
88+ "looking for library '{}'" .format (cfg_file ),
89+ 'Checking {} ...' .format (test_file )
90+ ]
91+
92+
93+ def test_lib_lookup_absolute_notfound (tmpdir ):
94+ test_file = os .path .join (tmpdir , 'test.c' )
95+ with open (test_file , 'wt' ):
96+ pass
97+
98+ cfg_file = os .path .join (tmpdir , 'test.cfg' )
99+
100+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=library' , '--library={}' .format (cfg_file ), test_file ])
101+ exepath = os .path .dirname (exe )
102+ if sys .platform == 'win32' :
103+ exepath = exepath .replace ('\\ ' , '/' )
104+ assert exitcode == 1 , stdout
105+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
106+ assert lines == [
107+ "looking for library '{}'" .format (cfg_file ),
108+ "library not found: '{}'" .format (cfg_file ),
109+ "cppcheck: Failed to load library configuration file '{}'. File not found" .format (cfg_file )
110+ ]
111+
112+
113+ def test_lib_lookup_nofile (tmpdir ):
114+ test_file = os .path .join (tmpdir , 'test.c' )
115+ with open (test_file , 'wt' ):
116+ pass
117+
118+ # make sure we do not produce an error when the attempted lookup path is a directory and not a file
119+ gtk_dir = os .path .join (tmpdir , 'gtk' )
120+ os .mkdir (gtk_dir )
121+ gtk_cfg_dir = os .path .join (tmpdir , 'gtk.cfg' )
122+ os .mkdir (gtk_cfg_dir )
123+
124+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=library' , '--library=gtk' , test_file ], cwd = tmpdir )
125+ exepath = os .path .dirname (exe )
126+ if sys .platform == 'win32' :
127+ exepath = exepath .replace ('\\ ' , '/' )
128+ assert exitcode == 0 , stdout if stdout else stderr
129+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
130+ assert lines == [
131+ "looking for library 'gtk'" ,
132+ "looking for library 'gtk.cfg'" ,
133+ "looking for library '{}/gtk.cfg'" .format (exepath ),
134+ "looking for library '{}/cfg/gtk.cfg'" .format (exepath ),
135+ 'Checking {} ...' .format (test_file )
136+ ]
137+
138+
139+ def test_lib_lookup_multi (tmpdir ):
140+ test_file = os .path .join (tmpdir , 'test.c' )
141+ with open (test_file , 'wt' ):
142+ pass
143+
144+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=library' , '--library=posix,gnu' , test_file ])
145+ exepath = os .path .dirname (exe )
146+ if sys .platform == 'win32' :
147+ exepath = exepath .replace ('\\ ' , '/' )
148+ assert exitcode == 0 , stdout if stdout else stderr
149+ lines = __remove_std_lookup_log (stdout .splitlines (), exepath )
150+ assert lines == [
151+ "looking for library 'posix'" ,
152+ "looking for library 'posix.cfg'" ,
153+ "looking for library '{}/posix.cfg'" .format (exepath ),
154+ "looking for library '{}/cfg/posix.cfg'" .format (exepath ),
155+ "looking for library 'gnu'" ,
156+ "looking for library 'gnu.cfg'" ,
157+ "looking for library '{}/gnu.cfg'" .format (exepath ),
158+ "looking for library '{}/cfg/gnu.cfg'" .format (exepath ),
159+ 'Checking {} ...' .format (test_file )
160+ ]
161+
162+
163+ def test_platform_lookup_builtin (tmpdir ):
164+ test_file = os .path .join (tmpdir , 'test.c' )
165+ with open (test_file , 'wt' ):
166+ pass
167+
168+ exitcode , stdout , stderr = cppcheck (['--debug-lookup=platform' , '--platform=unix64' , test_file ])
169+ assert exitcode == 0 , stdout if stdout else stderr
170+ lines = stdout .splitlines ()
171+ # built-in platform are not being looked up
172+ assert lines == [
173+ 'Checking {} ...' .format (test_file )
174+ ]
175+
176+
177+ # TODO: behaves differently when using a CMake build
178+ # TODO: test with FILESDIR
179+ @pytest .mark .skip
180+ def test_platform_lookup_external (tmpdir ):
181+ test_file = os .path .join (tmpdir , 'test.c' )
182+ with open (test_file , 'wt' ):
183+ pass
184+
185+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=platform' , '--platform=avr8' , test_file ])
186+ exepath = os .path .dirname (exe )
187+ if sys .platform == 'win32' :
188+ exepath = exepath .replace ('\\ ' , '/' )
189+ assert exitcode == 0 , stdout if stdout else stderr
190+ lines = stdout .splitlines ()
191+ assert lines == [
192+ "looking for platform 'avr8' in '{}'" .format (os .path .join (exepath , 'cppcheck' )), # TODO: this not not the path *of* the executable but the the path *to* the executable
193+ "try to load platform file 'avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8" ,
194+ "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml" ,
195+ "try to load platform file 'platforms/avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/avr8" ,
196+ "try to load platform file 'platforms/avr8.xml' ... Success" ,
197+ 'Checking {} ...' .format (test_file )
198+ ]
199+
200+
201+ # TODO: test with FILESDIR
202+ def test_platform_lookup_external_notfound (tmpdir ):
203+ test_file = os .path .join (tmpdir , 'test.c' )
204+ with open (test_file , 'wt' ):
205+ pass
206+
207+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=platform' , '--platform=none' , test_file ])
208+ exepath = os .path .dirname (exe )
209+ exepath_bin = os .path .join (exepath , 'cppcheck' )
210+ if sys .platform == 'win32' :
211+ exepath = exepath .replace ('\\ ' , '/' )
212+ exepath_bin += '.exe'
213+ assert exitcode == 1 , stdout
214+ lines = stdout .splitlines ()
215+ assert lines == [
216+ "looking for platform 'none' in '{}'" .format (exepath_bin ), # TODO: this is not the path *of* the executable but the the path *to* the executable
217+ "try to load platform file 'none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none" ,
218+ "try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml" ,
219+ "try to load platform file 'platforms/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none" ,
220+ "try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml" ,
221+ "try to load platform file '{}/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none" .format (exepath , exepath ),
222+ # TODO: lookup of '{exepath}/none.xml' missing
223+ "try to load platform file '{}/platforms/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/none" .format (exepath , exepath ),
224+ "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 ),
225+ "cppcheck: error: unrecognized platform: 'none'."
226+ ]
227+
228+
229+ # TODO: test with FILESDIR
230+ def test_addon_lookup (tmpdir ):
231+ test_file = os .path .join (tmpdir , 'test.c' )
232+ with open (test_file , 'wt' ):
233+ pass
234+
235+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=misra' , test_file ])
236+ exepath = os .path .dirname (exe )
237+ exepath_sep = exepath + os .path .sep
238+ assert exitcode == 0 , stdout if stdout else stderr
239+ lines = stdout .splitlines ()
240+ assert lines == [
241+ "looking for addon 'misra.py'" ,
242+ "looking for addon '{}misra.py'" .format (exepath_sep ),
243+ "looking for addon '{}addons/misra.py'" .format (exepath_sep ), # TODO: mixed separators
244+ 'Checking {} ...' .format (test_file )
245+ ]
246+
247+
248+ # TODO: test with FILESDIR
249+ def test_addon_lookup_ext (tmpdir ):
250+ test_file = os .path .join (tmpdir , 'test.c' )
251+ with open (test_file , 'wt' ):
252+ pass
253+
254+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=misra.py' , test_file ])
255+ exepath = os .path .dirname (exe )
256+ exepath_sep = exepath + os .path .sep
257+ assert exitcode == 0 , stdout if stdout else stderr
258+ lines = stdout .splitlines ()
259+ assert lines == [
260+ "looking for addon 'misra.py'" ,
261+ "looking for addon '{}misra.py'" .format (exepath_sep ),
262+ "looking for addon '{}addons/misra.py'" .format (exepath_sep ), # TODO: mixed separators
263+ 'Checking {} ...' .format (test_file )
264+ ]
265+
266+
267+ # TODO: test with FILESDIR
268+ def test_addon_lookup_notfound (tmpdir ):
269+ test_file = os .path .join (tmpdir , 'test.c' )
270+ with open (test_file , 'wt' ):
271+ pass
272+
273+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=none' , test_file ])
274+ exepath = os .path .dirname (exe )
275+ exepath_sep = exepath + os .path .sep
276+ assert exitcode == 1 , stdout
277+ lines = stdout .splitlines ()
278+ assert lines == [
279+ "looking for addon 'none.py'" ,
280+ "looking for addon '{}none.py'" .format (exepath_sep ),
281+ "looking for addon '{}addons/none.py'" .format (exepath_sep ), # TODO: mixed separators
282+ 'Did not find addon none.py'
283+ ]
284+
285+
286+ # TODO: test with FILESDIR
287+ def test_addon_lookup_ext_notfound (tmpdir ):
288+ test_file = os .path .join (tmpdir , 'test.c' )
289+ with open (test_file , 'wt' ):
290+ pass
291+
292+ exitcode , stdout , _ , exe = cppcheck_ex (['--debug-lookup=addon' , '--addon=none.py' , test_file ])
293+ exepath = os .path .dirname (exe )
294+ exepath_sep = exepath + os .path .sep
295+ assert exitcode == 1 , stdout
296+ lines = stdout .splitlines ()
297+ assert lines == [
298+ "looking for addon 'none.py'" ,
299+ "looking for addon '{}none.py'" .format (exepath_sep ),
300+ "looking for addon '{}addons/none.py'" .format (exepath_sep ), # TODO: mixed separators
301+ 'Did not find addon none.py'
302+ ]
303+
304+
305+ # TODO: test with FILESDIR
306+ @pytest .mark .skip
307+ def test_config_lookup (tmpdir ):
308+ test_file = os .path .join (tmpdir , 'test.c' )
309+ with open (test_file , 'wt' ):
310+ pass
311+
312+ # TODO: needs to be in exepath so this is found
313+ config_file = os .path .join (tmpdir , 'cppcheck.cfg' )
314+ with open (config_file , 'wt' ):
315+ pass
316+
317+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=config' , '--addon=misra' , test_file ], cwd = tmpdir )
318+ exepath = os .path .dirname (exe )
319+ exepath_sep = exepath + os .path .sep
320+ assert exitcode == 0 , stdout if stdout else stderr
321+ lines = stdout .splitlines ()
322+ assert lines == [
323+ "looking for '{}cppcheck.cfg'" .format (exepath_sep ),
324+ 'no configuration found' ,
325+ 'Checking {} ...' .format (test_file )
326+ ]
327+
328+
329+ # TODO: test with FILESDIR
330+ def test_config_lookup_notfound (tmpdir ):
331+ test_file = os .path .join (tmpdir , 'test.c' )
332+ with open (test_file , 'wt' ):
333+ pass
334+
335+ exitcode , stdout , stderr , exe = cppcheck_ex (['--debug-lookup=config' , test_file ])
336+ exepath = os .path .dirname (exe )
337+ exepath_sep = exepath + os .path .sep
338+ assert exitcode == 0 , stdout if stdout else stderr
339+ lines = stdout .splitlines ()
340+ assert lines == [
341+ "looking for '{}cppcheck.cfg'" .format (exepath_sep ),
342+ 'no configuration found' ,
343+ 'Checking {} ...' .format (test_file )
344+ ]
0 commit comments