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