Skip to content

Commit 26a9eda

Browse files
committed
test/cli/test_other.py: added showtime tests with FileSettings [skip ci]
1 parent caff3d4 commit 26a9eda

2 files changed

Lines changed: 74 additions & 14 deletions

File tree

test/cli/other_test.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import subprocess
1010
import shutil
1111

12-
from testutils import cppcheck, assert_cppcheck, cppcheck_ex, __lookup_cppcheck_exe
12+
from testutils import cppcheck, assert_cppcheck, cppcheck_ex, __lookup_cppcheck_exe, create_compile_commands
1313
from xml.etree import ElementTree
1414

1515

@@ -956,9 +956,8 @@ def test_unused_function_include(tmpdir):
956956

957957
# TODO: test with clang-tidy
958958
# TODO: test with --addon
959-
# TODO: test with FileSettings
960959
# TODO: test with multiple files
961-
def __test_showtime(tmp_path, showtime, exp_res, exp_last, extra_args=None):
960+
def __test_showtime(tmp_path, showtime, exp_res, exp_last, use_compdb, extra_args=None):
962961
test_file = tmp_path / 'test.cpp'
963962
with open(test_file, 'wt') as f:
964963
f.write(
@@ -972,10 +971,17 @@ def __test_showtime(tmp_path, showtime, exp_res, exp_last, extra_args=None):
972971
args = [
973972
f'--showtime={showtime}',
974973
'--quiet',
975-
'--inline-suppr',
976-
str(test_file)
974+
'--inline-suppr'
977975
]
978976

977+
if use_compdb:
978+
compdb_file = tmp_path / 'compile_commands.json'
979+
create_compile_commands(compdb_file, [test_file])
980+
981+
args.append(f'--project={compdb_file}')
982+
else:
983+
args.append(str(test_file))
984+
979985
if extra_args:
980986
args += extra_args
981987

@@ -994,50 +1000,87 @@ def __test_showtime(tmp_path, showtime, exp_res, exp_last, extra_args=None):
9941000
assert stderr == ''
9951001

9961002

1003+
def __test_showtime_top5_file(tmp_path, use_compdb):
1004+
__test_showtime(tmp_path, 'top5_file', 5, 'Check time: ', use_compdb)
1005+
1006+
9971007
def test_showtime_top5_file(tmp_path):
998-
__test_showtime(tmp_path, 'top5_file', 5, 'Check time: ')
1008+
__test_showtime_top5_file(tmp_path, False)
1009+
1010+
1011+
def test_showtime_top5_file_compdb(tmp_path):
1012+
__test_showtime_top5_file(tmp_path, True)
9991013

10001014

10011015
# TODO: remove extra args when --executor=process works works
1016+
def __test_showtime_top5_summary(tmp_path, use_compdb):
1017+
__test_showtime(tmp_path, 'top5_summary', 5, 'Overall time: ', use_compdb, ['-j1'])
1018+
1019+
10021020
def test_showtime_top5_summary(tmp_path):
1003-
__test_showtime(tmp_path, 'top5_summary', 5, 'Overall time: ', ['-j1'])
1021+
__test_showtime_top5_summary(tmp_path, False)
10041022

10051023

1024+
def test_showtime_top5_summary_compdb(tmp_path):
1025+
__test_showtime_top5_summary(tmp_path, True)
1026+
1027+
1028+
# TODO: compdb
10061029
# TODO: remove when --executor=process works works
10071030
def test_showtime_top5_summary_j_thread(tmp_path):
1008-
__test_showtime(tmp_path, 'top5_summary', 5, 'Overall time: ', ['-j2', '--executor=thread'])
1031+
__test_showtime(tmp_path, 'top5_summary', 5, 'Overall time: ', False, ['-j2', '--executor=thread'])
10091032

10101033

1034+
# TODO: compdb
10111035
# TODO: remove override when fixed
10121036
@pytest.mark.skipif(sys.platform == 'win32', reason="requires ProcessExecutor")
10131037
@pytest.mark.xfail(strict=True) # TODO: need to transfer the timer results to parent process - see #4452
10141038
def test_showtime_top5_summary_j_process(tmp_path):
1015-
__test_showtime(tmp_path, 'top5_summary', 5, 'Overall time: ', ['-j2', '--executor=process'])
1039+
__test_showtime(tmp_path, 'top5_summary', 5, 'Overall time: ', False, ['-j2', '--executor=process'])
1040+
1041+
1042+
def __test_showtime_file(tmp_path, use_compdb):
1043+
__test_showtime(tmp_path, 'file', 79, 'Check time: ', use_compdb)
10161044

10171045

10181046
def test_showtime_file(tmp_path):
1019-
__test_showtime(tmp_path, 'file', 79, 'Check time: ')
1047+
__test_showtime_file(tmp_path, False)
1048+
10201049

1050+
def test_showtime_file_compdb(tmp_path):
1051+
__test_showtime_file(tmp_path, True)
10211052

1053+
1054+
# TODO: compdb
10221055
# TODO: remove extra args when --executor=process works works
10231056
def test_showtime_summary(tmp_path):
1024-
__test_showtime(tmp_path, 'summary', 79, 'Overall time: ', ['-j1'])
1057+
__test_showtime(tmp_path, 'summary', 79, 'Overall time: ', False, ['-j1'])
10251058

10261059

1060+
# TODO: compdb
10271061
# TODO: remove when --executor=process works works
10281062
def test_showtime_summary_j_thread(tmp_path):
1029-
__test_showtime(tmp_path, 'summary', 79, 'Overall time: ', ['-j2', '--executor=thread'])
1063+
__test_showtime(tmp_path, 'summary', 79, 'Overall time: ', False, ['-j2', '--executor=thread'])
10301064

10311065

1066+
# TODO: compdb
10321067
# TODO: remove override when fixed
10331068
@pytest.mark.skipif(sys.platform == 'win32', reason="requires ProcessExecutor")
10341069
@pytest.mark.xfail(strict=True) # TODO: need to transfer the timer results to parent process - see #4452
10351070
def test_showtime_summary_j_process(tmp_path):
1036-
__test_showtime(tmp_path, 'summary', 79, 'Overall time: ', ['-j2', '--executor=process'])
1071+
__test_showtime(tmp_path, 'summary', 79, 'Overall time: ', False, ['-j2', '--executor=process'])
1072+
1073+
1074+
def __test_showtime_file_total(tmp_path, use_compdb):
1075+
__test_showtime(tmp_path, 'file-total', 0, 'Check time: ', use_compdb)
10371076

10381077

10391078
def test_showtime_file_total(tmp_path):
1040-
__test_showtime(tmp_path, 'file-total', 0, 'Check time: ')
1079+
__test_showtime_file_total(tmp_path, False)
1080+
1081+
1082+
def test_showtime_file_total_compdb(tmp_path):
1083+
__test_showtime_file_total(tmp_path, True)
10411084

10421085

10431086
def test_showtime_unique(tmp_path):

test/cli/testutils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import time
77
import tempfile
8+
import json
89

910
# Create Cppcheck project file
1011
import sys
@@ -45,6 +46,22 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
4546
f.write(cppcheck_xml)
4647

4748

49+
def create_compile_commands(compdb_file, files):
50+
compdb = []
51+
# TODO: bail out on empty list
52+
for f in files:
53+
compdb.append(
54+
{
55+
"file": str(f),
56+
"directory": os.path.dirname(f),
57+
"command": "gcc -c {}".format(os.path.basename(f))
58+
}
59+
)
60+
compdb_j = json.dumps(compdb)
61+
with open(compdb_file, 'wt') as f:
62+
f.write(compdb_j)
63+
64+
4865
def __lookup_cppcheck_exe():
4966
# path the script is located in
5067
script_path = os.path.dirname(os.path.realpath(__file__))

0 commit comments

Comments
 (0)