44import os
55import sys
66import pytest
7+ import json
78
89from testutils import cppcheck , assert_cppcheck
910
@@ -278,22 +279,6 @@ def test_addon_threadsafety(tmpdir):
278279 assert stderr == '{}:4:12: warning: strerror is MT-unsafe [threadsafety-unsafe-call]\n ' .format (test_file )
279280
280281
281- def test_addon_invalidjson (tmpdir ):
282- addon_file = os .path .join (tmpdir , 'invalid.json' )
283- with open (addon_file , 'wt' ) as f :
284- f .write ("""
285- {
286- "Script": "addons/something.py"
287- }
288- """ )
289-
290- args = ['--addon={}' .format (addon_file ), '--enable=all' , 'nonexistent.cpp' ]
291-
292- exitcode , stdout , stderr = cppcheck (args )
293- assert exitcode != 0
294- assert stdout == 'Loading {} failed. script must be set to a string value.\n ' .format (addon_file )
295-
296-
297282def test_addon_naming (tmpdir ):
298283 # the addon does nothing without a config
299284 addon_file = os .path .join (tmpdir , 'naming1.json' )
@@ -644,12 +629,10 @@ def test_invalid_addon_json(tmpdir):
644629 """ )
645630
646631 test_file = os .path .join (tmpdir , 'file.cpp' )
647- with open (test_file , 'wt' ) as f :
648- f .write ("""
649- typedef int MISRA_5_6_VIOLATION;
650- """ )
632+ with open (test_file , 'wt' ):
633+ pass
651634
652- args = ['--addon={}' .format (addon_file ), '--enable=all' , test_file ]
635+ args = ['--addon={}' .format (addon_file ), test_file ]
653636
654637 exitcode , stdout , stderr = cppcheck (args )
655638 assert exitcode == 1
@@ -1124,3 +1107,56 @@ def test_build_dir_j_memleak(tmpdir): #12111
11241107 ]
11251108
11261109 assert_cppcheck (args , ec_exp = 0 , err_exp = [], out_exp = out_lines )
1110+
1111+
1112+ def __test_addon_json_invalid (tmpdir , addon_json , expected ):
1113+ addon_file = os .path .join (tmpdir , 'invalid.json' )
1114+ with open (addon_file , 'wt' ) as f :
1115+ f .write (addon_json )
1116+
1117+ test_file = os .path .join (tmpdir , 'file.cpp' )
1118+ with open (test_file , 'wt' ):
1119+ pass
1120+
1121+ args = ['--addon={}' .format (addon_file ), test_file ]
1122+
1123+ exitcode , stdout , stderr = cppcheck (args )
1124+ assert exitcode == 1
1125+ lines = stdout .splitlines ()
1126+ assert len (lines ) == 1
1127+ assert lines == [
1128+ 'Loading {} failed. {}' .format (addon_file , expected )
1129+ ]
1130+ assert stderr == ''
1131+
1132+
1133+ def test_addon_json_invalid_no_obj (tmpdir ):
1134+ __test_addon_json_invalid (tmpdir , json .dumps ([]), "JSON is not an object." )
1135+
1136+
1137+ def test_addon_json_invalid_args_1 (tmpdir ):
1138+ __test_addon_json_invalid (tmpdir , json .dumps ({'args' :0 }), "'args' must be an array." )
1139+
1140+
1141+ def test_addon_json_invalid_args_2 (tmpdir ):
1142+ __test_addon_json_invalid (tmpdir , json .dumps ({'args' :[0 ]}), "'args' entry is not a string." )
1143+
1144+
1145+ def test_addon_json_invalid_ctu (tmpdir ):
1146+ __test_addon_json_invalid (tmpdir , json .dumps ({'ctu' :0 }), "'ctu' must be a boolean." )
1147+
1148+
1149+ def test_addon_json_invalid_python (tmpdir ):
1150+ __test_addon_json_invalid (tmpdir , json .dumps ({'python' :0 }), "'python' must be a string." )
1151+
1152+
1153+ def test_addon_json_invalid_executable (tmpdir ):
1154+ __test_addon_json_invalid (tmpdir , json .dumps ({'executable' :0 }), "'executable' must be a string." )
1155+
1156+
1157+ def test_addon_json_invalid_script_1 (tmpdir ):
1158+ __test_addon_json_invalid (tmpdir , json .dumps ({'Script' :'' }), "'script' is missing." )
1159+
1160+
1161+ def test_addon_json_invalid_script_2 (tmpdir ):
1162+ __test_addon_json_invalid (tmpdir , json .dumps ({'script' :0 }), "'script' must be a string." )
0 commit comments