Skip to content

Commit eb116f9

Browse files
committed
12254: another test fix
1 parent 0b6e270 commit eb116f9

2 files changed

Lines changed: 62 additions & 29 deletions

File tree

test/cli/test-other.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
import time
88

9-
from testutils import cppcheck, assert_cppcheck
9+
from testutils import cppcheck, assert_cppcheck, copy_and_prepare_cppcheck
1010

1111

1212
def __test_missing_include(tmpdir, use_j):
@@ -865,6 +865,10 @@ def test_build_dir_j_memleak(tmpdir): #12111
865865
def test_premium_with_relative_path(tmpdir):
866866
product_name = 'Cppcheck Premium ' + str(time.time())
867867

868+
test_file = os.path.join(tmpdir, 'test.cpp')
869+
with open(test_file, 'wt') as f:
870+
f.write('int main() {}')
871+
868872
test_cfg = tmpdir.join('cppcheck.cfg')
869873
with open(test_cfg, 'wt') as f:
870874
f.write("""
@@ -875,13 +879,48 @@ def test_premium_with_relative_path(tmpdir):
875879
}
876880
""".replace('NAME', product_name))
877881

878-
args = ['--premium=misra-c++-2008', 'test.c']
882+
args = ['--premium=misra-c++-2008', test_file]
879883

880-
exitcode, _, stderr = cppcheck(args, None, True, tmpdir)
881-
assert exitcode == 0
884+
885+
cppcheck_exe = copy_and_prepare_cppcheck(tmpdir)
886+
887+
exitcode, _, stderr = cppcheck(args, None, True, cppcheck_exe)
882888
assert stderr == ''
889+
assert exitcode == 0
883890

884-
exitcode, stdout, stderr = cppcheck(['--version'], None, True, tmpdir)
891+
892+
exitcode, stdout, stderr = cppcheck(['--version'], None, True, cppcheck_exe)
885893
assert stdout == product_name + '\n'
886894
assert stderr == ''
887895
assert exitcode == 0
896+
897+
os.remove(test_cfg)
898+
899+
exitcode, stdout, stderr = cppcheck(['--premium=misra-c++-2008'], None, cppcheck_exe)
900+
assert stderr == ''
901+
assert stdout == 'cppcheck: error: unrecognized command line option: "--premium=misra-c++-2008".\n'
902+
assert exitcode == 1
903+
904+
exitcode, stdout, stderr = cppcheck(['--version'], None, cppcheck_exe)
905+
assert stderr == ''
906+
assert stdout == 'Cppcheck 2.13 dev' + '\n'
907+
assert exitcode == 0
908+
909+
def test_premium_with_relative_path2(tmpdir):
910+
product_name = 'Cppcheck Premium ' + str(time.time())
911+
912+
test_file = os.path.join(tmpdir, 'test.cpp')
913+
with open(test_file, 'wt') as f:
914+
f.write('int main() {}')
915+
916+
args = ['--premium=misra-c++-2008', test_file]
917+
918+
exitcode, stdout, stderr = cppcheck(args, None)
919+
assert stderr == ''
920+
assert stdout == 'cppcheck: error: unrecognized command line option: "--premium=misra-c++-2008".\n'
921+
assert exitcode == 1
922+
923+
exitcode, stdout, stderr = cppcheck(['--version'], None)
924+
assert stdout == 'Cppcheck 2.13 dev' + '\n'
925+
assert stderr == ''
926+
assert exitcode == 0

test/cli/testutils.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ def __create_temporary_copy(tmpdir, temp_file_name):
4949
return temp_path
5050

5151

52-
def __lookup_cppcheck_exe(script_path=None):
52+
def __lookup_cppcheck_exe(cppcheck_exe_path=None):
53+
54+
if cppcheck_exe_path is not None:
55+
return cppcheck_exe_path
56+
5357
# path the script is located in
54-
if script_path == None:
55-
script_path = os.path.dirname(os.path.realpath(__file__))
58+
script_path = os.path.dirname(os.path.realpath(__file__))
5659

5760
exe_name = "cppcheck"
58-
5961
if sys.platform == "win32":
6062
exe_name += ".exe"
61-
6263
exe_path = None
6364

6465
if 'TEST_CPPCHECK_EXE_LOOKUP_PATH' in os.environ:
@@ -78,31 +79,24 @@ def __lookup_cppcheck_exe(script_path=None):
7879
return exe_path
7980

8081

81-
def __copy_and_prepare_cppcheck(tmpdir):
82-
file_name = "cppcheck"
83-
if sys.platform == "win32":
84-
file_name += ".exe"
85-
exe = __create_temporary_copy(tmpdir, file_name)
82+
def copy_and_prepare_cppcheck(tmpdir):
83+
exe = shutil.copy2(__lookup_cppcheck_exe(), tmpdir)
8684

8785
#add minimum cfg
88-
if not os.path.exists(tmpdir + '/cfg'):
89-
test_cfg_folder = tmpdir.mkdir('cfg')
90-
test_cfg = test_cfg_folder.join('std.cfg')
91-
if not os.path.exists(test_cfg):
92-
with open(test_cfg, 'wt') as f:
93-
f.write("""
94-
<?xml version="1.0"?>
95-
<def format="2"/>
96-
""")
86+
test_cfg_folder = tmpdir.mkdir('cfg')
87+
test_cfg = test_cfg_folder.join('std.cfg')
88+
with open(test_cfg, 'wt') as f:
89+
f.write("""
90+
<?xml version="1.0"?>
91+
<def format="2"/>
92+
""")
9793
return exe
9894

9995

10096
# Run Cppcheck with args
101-
def cppcheck(args, env=None, remove_active_checkers=True, tmpdir=None):
102-
if tmpdir != None:
103-
exe = __copy_and_prepare_cppcheck(tmpdir)
104-
else:
105-
exe = __lookup_cppcheck_exe()
97+
def cppcheck(args, env=None, remove_active_checkers=True, cppcheck_exe_path=None):
98+
exe = __lookup_cppcheck_exe(cppcheck_exe_path)
99+
106100
assert exe is not None, 'no cppcheck binary found'
107101

108102
logging.info(exe + ' ' + ' '.join(args))

0 commit comments

Comments
 (0)