@@ -23,6 +23,20 @@ def cppcheck(args):
2323 stderr = comm [1 ].decode (encoding = 'utf-8' , errors = 'ignore' ).replace ('\r \n ' , '\n ' )
2424 return p .returncode , stdout , stderr
2525
26+ # Run Cppcheck from project path
27+ def cppcheck_local (args ):
28+ cwd = os .getcwd ()
29+ os .chdir ('1-helloworld' )
30+ ret , stdout , stderr = cppcheck (args )
31+ os .chdir (cwd )
32+ return (ret , stdout , stderr )
33+
34+ def getRelativeProjectPath ():
35+ return '1-helloworld'
36+
37+ def getAbsoluteProjectPath ():
38+ return os .path .join (os .getcwd (), '1-helloworld' )
39+
2640# Get Visual Studio configurations checking a file
2741# Checking {file} {config}...
2842def getVsConfigs (stdout , filename ):
@@ -48,34 +62,28 @@ def test_relative_path():
4862
4963
5064def test_local_path ():
51- cwd = os .getcwd ()
52- os .chdir ('1-helloworld' )
53- ret , stdout , stderr = cppcheck ('.' )
54- os .chdir (cwd )
65+ ret , stdout , stderr = cppcheck_local ('.' )
5566 assert ret == 0
5667 assert stdout == 'Checking main.c ...\n '
5768 assert stderr == '[main.c:5]: (error) Division by zero.\n '
5869
5970def test_absolute_path ():
60- prjpath = os . path . join ( os . getcwd (), '1-helloworld' )
71+ prjpath = getAbsoluteProjectPath ( )
6172 ret , stdout , stderr = cppcheck (prjpath )
6273 filename = os .path .join (prjpath , 'main.c' )
6374 assert ret == 0
6475 assert stdout == 'Checking %s ...\n ' % (filename )
6576 assert stderr == '[%s:5]: (error) Division by zero.\n ' % (filename )
6677
6778def test_addon_local_path ():
68- cwd = os .getcwd ()
69- os .chdir ('1-helloworld' )
70- ret , stdout , stderr = cppcheck ('--addon=misra .' )
71- os .chdir (cwd )
79+ ret , stdout , stderr = cppcheck_local ('--addon=misra .' )
7280 assert ret == 0
7381 assert stdout == 'Checking main.c ...\n '
7482 assert stderr == ('[main.c:5]: (error) Division by zero.\n '
7583 '[main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n ' )
7684
7785def test_addon_absolute_path ():
78- prjpath = os . path . join ( os . getcwd (), '1-helloworld' )
86+ prjpath = getAbsoluteProjectPath ( )
7987 ret , stdout , stderr = cppcheck ('--addon=misra %s' % (prjpath ))
8088 filename = os .path .join (prjpath , 'main.c' )
8189 assert ret == 0
@@ -84,7 +92,7 @@ def test_addon_absolute_path():
8492 '[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n ' % (filename , filename ))
8593
8694def test_addon_relative_path ():
87- prjpath = '1-helloworld'
95+ prjpath = getRelativeProjectPath ()
8896 ret , stdout , stderr = cppcheck ('--addon=misra %s' % (prjpath ))
8997 filename = os .path .join (prjpath , 'main.c' )
9098 assert ret == 0
@@ -93,69 +101,62 @@ def test_addon_relative_path():
93101 '[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n ' % (filename , filename ))
94102
95103def test_basepath_relative_path ():
96- prjpath = '1-helloworld'
104+ prjpath = getRelativeProjectPath ()
97105 ret , stdout , stderr = cppcheck ('%s -rp=%s' % (prjpath , prjpath ))
98106 filename = os .path .join (prjpath , 'main.c' )
99107 assert ret == 0
100108 assert stdout == 'Checking %s ...\n ' % (filename )
101109 assert stderr == '[main.c:5]: (error) Division by zero.\n '
102110
103111def test_basepath_absolute_path ():
104- prjpath = os . path . join ( os . getcwd (), '1-helloworld' )
112+ prjpath = getAbsoluteProjectPath ( )
105113 ret , stdout , stderr = cppcheck ('%s -rp=%s' % (prjpath , prjpath ))
106114 filename = os .path .join (prjpath , 'main.c' )
107115 assert ret == 0
108116 assert stdout == 'Checking %s ...\n ' % (filename )
109117 assert stderr == '[main.c:5]: (error) Division by zero.\n '
110118
111119def test_vs_project_local_path ():
112- cwd = os .getcwd ()
113- os .chdir ('1-helloworld' )
114- ret , stdout , stderr = cppcheck ('--project=helloworld.vcxproj' )
115- os .chdir (cwd )
120+ ret , stdout , stderr = cppcheck_local ('--project=helloworld.vcxproj' )
116121 assert ret == 0
117122 assert getVsConfigs (stdout , 'main.c' ) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
118123 assert stderr == '[main.c:5]: (error) Division by zero.\n '
119124
120125def test_vs_project_relative_path ():
121- prjpath = '1-helloworld'
126+ prjpath = getRelativeProjectPath ()
122127 ret , stdout , stderr = cppcheck ('--project=%s' % (os .path .join (prjpath , 'helloworld.vcxproj' )))
123128 filename = os .path .join (prjpath , 'main.c' )
124129 assert ret == 0
125130 assert getVsConfigs (stdout , filename ) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
126131 assert stderr == '[%s:5]: (error) Division by zero.\n ' % (filename )
127132
128133def test_vs_project_absolute_path ():
129- prjpath = os . path . join ( os . getcwd (), '1-helloworld' )
134+ prjpath = getAbsoluteProjectPath ( )
130135 ret , stdout , stderr = cppcheck ('--project=%s' % (os .path .join (prjpath , 'helloworld.vcxproj' )))
131136 filename = os .path .join (prjpath , 'main.c' )
132137 assert ret == 0
133138 assert getVsConfigs (stdout , filename ) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
134139 assert stderr == '[%s:5]: (error) Division by zero.\n ' % (filename )
135140
136141def test_cppcheck_project_local_path ():
137- cwd = os .getcwd ()
138- os .chdir ('1-helloworld' )
139- ret , stdout , stderr = cppcheck ('--platform=win64 --project=helloworld.cppcheck' )
140- os .chdir (cwd )
142+ ret , stdout , stderr = cppcheck_local ('--platform=win64 --project=helloworld.cppcheck' )
141143 assert ret == 0
142144 assert getVsConfigs (stdout , 'main.c' ) == 'Debug|x64'
143145 assert stderr == '[main.c:5]: (error) Division by zero.\n '
144146
145147def test_cppcheck_project_relative_path ():
146- prjpath = '1-helloworld'
148+ prjpath = getRelativeProjectPath ()
147149 ret , stdout , stderr = cppcheck ('--platform=win64 --project=%s' % (os .path .join (prjpath , 'helloworld.cppcheck' )))
148150 filename = os .path .join (prjpath , 'main.c' )
149151 assert ret == 0
150152 assert getVsConfigs (stdout , filename ) == 'Debug|x64'
151153 assert stderr == '[%s:5]: (error) Division by zero.\n ' % (filename )
152154
153155def test_cppcheck_project_absolute_path ():
154- prjpath = os . path . join ( os . getcwd (), '1-helloworld' )
156+ prjpath = getAbsoluteProjectPath ( )
155157 ret , stdout , stderr = cppcheck ('--platform=win64 --project=%s' % (os .path .join (prjpath , 'helloworld.cppcheck' )))
156158 filename = os .path .join (prjpath , 'main.c' )
157159 assert ret == 0
158160 assert getVsConfigs (stdout , filename ) == 'Debug|x64'
159161 assert stderr == '[%s:5]: (error) Division by zero.\n ' % (filename )
160162
161-
0 commit comments