MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / test_lookup_path

Function test_lookup_path

test/cli/lookup_test.py:1134–1203  ·  view source on GitHub ↗
(tmpdir, type, file)

Source from the content-addressed store, hash-verified

1132
1133@pytest.mark.parametrize("type,file", [("addon", "misra.py"), ("config", "cppcheck.cfg"), ("library", "gnu.cfg"), ("platform", "avr8.xml")])
1134def test_lookup_path(tmpdir, type, file):
1135 test_file = os.path.join(tmpdir, 'test.c')
1136 with open(test_file, 'wt'):
1137 pass
1138
1139 cppcheck = 'cppcheck' # No path
1140 path = os.path.dirname(__lookup_cppcheck_exe())
1141 env = os.environ.copy()
1142 env['PATH'] = path + (';' if sys.platform == 'win32' else ':') + env.get('PATH', '')
1143 if type == 'config':
1144 with open(os.path.join(path, "cppcheck.cfg"), 'wt') as f:
1145 f.write('{}')
1146 exitcode, stdout, stderr, _ = cppcheck_ex(args=[f'--debug-lookup={type}', test_file], cppcheck_exe=cppcheck, cwd=str(tmpdir), env=env)
1147 os.remove(os.path.join(path, "cppcheck.cfg")) # clean up otherwise other tests may fail
1148 else:
1149 exitcode, stdout, stderr, _ = cppcheck_ex(args=[f'--debug-lookup={type}', f'--{type}={file}', test_file], cppcheck_exe=cppcheck, cwd=str(tmpdir), env=env)
1150 assert exitcode == 0, stdout if stdout else stderr
1151 def format_path(p):
1152 return p.replace('\\', '/').replace('"', '\'')
1153 lines = format_path(stdout).splitlines()
1154
1155 if type == 'addon':
1156 def try_fail(f):
1157 return f"looking for {type} '{format_path(f)}'"
1158 def try_success(f):
1159 return f"looking for {type} '{format_path(f)}'"
1160 assert lines == [
1161 f"looking for {type} '{file}'",
1162 try_fail(os.path.join(path, file)),
1163 try_success(os.path.join(path, 'addons', file)),
1164 f'Checking {format_path(test_file)} ...'
1165 ]
1166 elif type == 'config':
1167 def try_success(f):
1168 return f"looking for '{format_path(f)}'"
1169 assert lines == [
1170 try_success(os.path.join(path, file)),
1171 f'Checking {format_path(test_file)} ...'
1172 ]
1173 elif type == 'platform':
1174 def try_fail(f):
1175 f = format_path(f)
1176 return f"try to load {type} file '{f}' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={f}"
1177 def try_success(f):
1178 f = format_path(f)
1179 return f"try to load {type} file '{f}' ... Success"
1180 assert lines == [
1181 f"looking for {type} '{file}'",
1182 try_fail(os.path.join(tmpdir, file)),
1183 try_fail(os.path.join(tmpdir, 'platforms', file)),
1184 try_fail(os.path.join(path, file)),
1185 try_success(os.path.join(path, 'platforms', file)),
1186 f'Checking {format_path(test_file)} ...'
1187 ]
1188 elif type == 'library':
1189 def try_fail(f):
1190 return f"looking for {type} '{format_path(f)}'"
1191 def try_success(f):

Callers

nothing calls this directly

Calls 10

__lookup_cppcheck_exeFunction · 0.90
cppcheck_exFunction · 0.90
strFunction · 0.85
format_pathFunction · 0.85
try_failFunction · 0.85
try_successFunction · 0.85
joinMethod · 0.45
copyMethod · 0.45
getMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected