Skip to content

Commit 9fd2a71

Browse files
authored
test/cli/other_test.py: improved tests for -i (#7487)
1 parent 0258e15 commit 9fd2a71

File tree

3 files changed

+151
-105
lines changed

3 files changed

+151
-105
lines changed

cli/cmdlineparser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,8 +1627,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16271627
path = Path::simplifyPath(std::move(path));
16281628

16291629
bool isdir = false;
1630-
if (!Path::exists(path, &isdir) && mSettings.debugignore)
1631-
std::cout << "path to ignore does not exist: " << path << std::endl;
1630+
if (!Path::exists(path, &isdir) && mSettings.debugignore) {
1631+
// FIXME: this is misleading because we match from the end of the path so it does not require to exist
1632+
//std::cout << "path to ignore does not exist: " << path << std::endl;
1633+
}
16321634
// TODO: this only works when it exists
16331635
if (isdir) {
16341636
// If directory name doesn't end with / or \, add it

lib/importproject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "json.h"
4343

4444
// TODO: align the exclusion logic with PathMatch
45+
// TODO: PathMatch lacks glob support
4546
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool debug)
4647
{
4748
for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {

test/cli/other_test.py

Lines changed: 146 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,81 +2028,98 @@ def test_checkers_report_misra_json(tmpdir):
20282028
assert '<checker id="Misra C 2012: 8.1"/>' in stderr
20292029

20302030

2031-
def test_ignore(tmpdir):
2031+
def __test_ignore_file(tmpdir, ign, append=False, inject_path=False):
20322032
os.mkdir(os.path.join(tmpdir, 'src'))
20332033
test_file = os.path.join(tmpdir, 'src', 'test.cpp')
20342034
with open(test_file, 'wt'):
20352035
pass
20362036

20372037
# TODO: this should say that all paths are ignored
20382038
lines_exp = [
2039+
'ignored path: {}'.format(test_file),
20392040
'cppcheck: error: could not find or open any of the paths given.',
20402041
'cppcheck: Maybe all paths were ignored?'
20412042
]
20422043

20432044
args = [
2044-
'-itest.cpp',
2045+
'--debug-ignore',
20452046
test_file
20462047
]
20472048

2048-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2049-
assert exitcode == 1, stdout
2050-
assert stdout.splitlines() == lines_exp
2049+
if inject_path:
2050+
ign = ign.replace('$path', str(test_file))
20512051

2052-
# make sure it also matches when specified after the file
2053-
args = [
2054-
test_file,
2055-
'-itest.cpp'
2056-
]
2052+
if append:
2053+
args += ['-i{}'.format(ign)]
2054+
else:
2055+
args = ['-i{}'.format(ign)] + args
20572056

2058-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2059-
assert exitcode == 1, stdout
2057+
exitcode, stdout, stderr = cppcheck(args, cwd=tmpdir)
2058+
assert exitcode == 1, stdout if stdout else stderr
20602059
assert stdout.splitlines() == lines_exp
20612060

2062-
args = [
2063-
'-isrc/test.cpp',
2064-
test_file
2065-
]
20662061

2067-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2068-
assert exitcode == 1, stdout
2069-
assert stdout.splitlines() == lines_exp
2062+
def test_ignore_file(tmpdir):
2063+
__test_ignore_file(tmpdir, 'test.cpp')
20702064

2071-
args = [
2072-
'-isrc\\test.cpp',
2073-
test_file
2074-
]
20752065

2076-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2077-
assert exitcode == 1, stdout
2078-
assert stdout.splitlines() == lines_exp
2066+
def test_ignore_file_append(tmpdir):
2067+
__test_ignore_file(tmpdir, 'test.cpp', append=True)
20792068

2080-
args = [
2081-
'-isrc/',
2082-
test_file
2083-
]
20842069

2085-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2086-
assert exitcode == 1, stdout
2087-
assert stdout.splitlines() == lines_exp
2070+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2071+
def test_ignore_file_wildcard_back(tmpdir):
2072+
__test_ignore_file(tmpdir, 'test.c*')
20882073

2089-
args = [
2090-
'-isrc\\',
2091-
test_file
2092-
]
20932074

2094-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2095-
assert exitcode == 1, stdout
2096-
assert stdout.splitlines() == lines_exp
2075+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2076+
def test_ignore_file_wildcard_front(tmpdir):
2077+
__test_ignore_file(tmpdir, '*test.cpp')
20972078

2098-
args = [
2099-
'-i{}'.format(test_file),
2100-
test_file
2101-
]
21022079

2103-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2104-
assert exitcode == 1, stdout
2105-
assert stdout.splitlines() == lines_exp
2080+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2081+
def test_ignore_file_placeholder(tmpdir):
2082+
__test_ignore_file(tmpdir, 't?st.cpp')
2083+
2084+
2085+
def test_ignore_file_relative(tmpdir):
2086+
__test_ignore_file(tmpdir, 'src/test.cpp')
2087+
2088+
2089+
def test_ignore_file_relative_backslash(tmpdir):
2090+
__test_ignore_file(tmpdir, 'src\\test.cpp')
2091+
2092+
2093+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2094+
def test_ignore_file_relative_wildcard(tmpdir):
2095+
__test_ignore_file(tmpdir, 'src/test.c*')
2096+
2097+
2098+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2099+
def test_ignore_file_relative_wildcard_backslash(tmpdir):
2100+
__test_ignore_file(tmpdir, 'src\\test.c*')
2101+
2102+
2103+
def test_ignore_path_relative(tmpdir):
2104+
__test_ignore_file(tmpdir, 'src/')
2105+
2106+
2107+
def test_ignore_path_relative_backslash(tmpdir):
2108+
__test_ignore_file(tmpdir, 'src\\')
2109+
2110+
2111+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2112+
def test_ignore_path_relative_wildcard(tmpdir):
2113+
__test_ignore_file(tmpdir, 'src*/')
2114+
2115+
2116+
@pytest.mark.xfail(strict=True) # TODO: glob syntax is not supported?
2117+
def test_ignore_path_relative_wildcard_backslash(tmpdir):
2118+
__test_ignore_file(tmpdir, 'src*\\')
2119+
2120+
2121+
def test_ignore_abspath(tmpdir):
2122+
__test_ignore_file(tmpdir, '$path', inject_path=True)
21062123

21072124

21082125
def __write_gui_project(tmpdir, test_file, ignore):
@@ -2122,92 +2139,83 @@ def __write_gui_project(tmpdir, test_file, ignore):
21222139
return project_file
21232140

21242141

2125-
def test_ignore_project(tmpdir):
2142+
def __test_ignore_project(tmpdir, ign_proj, ign_cli=None, append_cli=False, inject_path_proj=False):
21262143
os.mkdir(os.path.join(tmpdir, 'src'))
21272144
test_file = os.path.join(tmpdir, 'src', 'test.cpp')
21282145
with open(test_file, 'wt'):
21292146
pass
21302147

2148+
# TODO: this should say that all paths were ignored
21312149
lines_exp = [
2150+
'ignored path: {}'.format(test_file),
21322151
'cppcheck: error: could not find or open any of the paths given.',
21332152
'cppcheck: Maybe all paths were ignored?'
21342153
]
21352154

2136-
project_file = __write_gui_project(tmpdir, test_file, 'test.cpp')
2155+
if inject_path_proj:
2156+
ign_proj = ign_proj.replace('$path', str(test_file))
2157+
2158+
project_file = __write_gui_project(tmpdir, test_file, ign_proj)
21372159
args = [
2160+
'--debug-ignore',
21382161
'--project={}'.format(project_file)
21392162
]
21402163

2164+
if append_cli:
2165+
args += ['-i{}'.format(ign_cli)]
2166+
else:
2167+
args = ['-i{}'.format(ign_cli)] + args
2168+
21412169
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
21422170
assert exitcode == 1, stdout
21432171
assert stdout.splitlines() == lines_exp
21442172

2145-
# make sure -i works when specified before project
2146-
project_file = __write_gui_project(tmpdir, test_file, 'test2.cpp')
2147-
args = [
2148-
'-itest.cpp',
2149-
'--project={}'.format(project_file)
2150-
]
21512173

2152-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2153-
assert exitcode == 1, stdout
2154-
assert stdout.splitlines() == lines_exp
2174+
def test_ignore_project_file(tmpdir):
2175+
__test_ignore_project(tmpdir, 'test.cpp')
21552176

2156-
# make sure -i works when specified after project
2157-
project_file = __write_gui_project(tmpdir, test_file, 'test2.cpp')
2158-
args = [
2159-
'--project={}'.format(project_file),
2160-
'-itest.cpp'
2161-
]
21622177

2163-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2164-
assert exitcode == 1, stdout
2165-
assert stdout.splitlines() == lines_exp
2178+
def test_ignore_project_file_cli_prepend(tmpdir):
2179+
__test_ignore_project(tmpdir, ign_proj='test2.cpp', ign_cli='test.cpp')
21662180

2167-
project_file = __write_gui_project(tmpdir, test_file, 'src/test.cpp')
2168-
args = [
2169-
'--project={}'.format(project_file)
2170-
]
21712181

2172-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2173-
assert exitcode == 1, stdout
2174-
assert stdout.splitlines() == lines_exp
2182+
def test_ignore_project_file_cli_append(tmpdir):
2183+
__test_ignore_project(tmpdir, ign_proj='test2.cpp', ign_cli='test.cpp', append_cli=True)
21752184

2176-
project_file = __write_gui_project(tmpdir, test_file, 'src\\test.cpp')
2177-
args = [
2178-
'--project={}'.format(project_file)
2179-
]
21802185

2181-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2182-
assert exitcode == 1, stdout
2183-
assert stdout.splitlines() == lines_exp
2186+
@pytest.mark.xfail(strict=True) # TODO: ?
2187+
def test_ignore_project_file_wildcard_back(tmpdir):
2188+
__test_ignore_project(tmpdir, 'test.c*')
21842189

2185-
project_file = __write_gui_project(tmpdir, test_file, 'src/')
2186-
args = [
2187-
'--project={}'.format(project_file)
2188-
]
21892190

2190-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2191-
assert exitcode == 1, stdout
2192-
assert stdout.splitlines() == lines_exp
2191+
@pytest.mark.xfail(strict=True) # TODO: ?
2192+
def test_ignore_project_file_wildcard_front(tmpdir):
2193+
__test_ignore_project(tmpdir, '*test.cpp')
21932194

2194-
project_file = __write_gui_project(tmpdir, test_file, 'src\\')
2195-
args = [
2196-
'--project={}'.format(project_file)
2197-
]
21982195

2199-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2200-
assert exitcode == 1, stdout
2201-
assert stdout.splitlines() == lines_exp
2196+
@pytest.mark.xfail(strict=True) # TODO: ?
2197+
def test_ignore_project_file_placeholder(tmpdir):
2198+
__test_ignore_project(tmpdir, 't?st.cpp')
22022199

2203-
project_file = __write_gui_project(tmpdir, test_file, test_file)
2204-
args = [
2205-
'--project={}'.format(project_file)
2206-
]
22072200

2208-
exitcode, stdout, _ = cppcheck(args, cwd=tmpdir)
2209-
assert exitcode == 1, stdout
2210-
assert stdout.splitlines() == lines_exp
2201+
def test_ignore_project_file_relative(tmpdir):
2202+
__test_ignore_project(tmpdir, 'src/test.cpp')
2203+
2204+
2205+
def test_ignore_project_file_relative_backslash(tmpdir):
2206+
__test_ignore_project(tmpdir, 'src\\test.cpp')
2207+
2208+
2209+
def test_ignore_project_path_relative(tmpdir):
2210+
__test_ignore_project(tmpdir, 'src/')
2211+
2212+
2213+
def test_ignore_project_path_relative_backslash(tmpdir):
2214+
__test_ignore_project(tmpdir, 'src\\')
2215+
2216+
2217+
def test_ignore_project_abspath(tmpdir):
2218+
__test_ignore_project(tmpdir, '$path', inject_path_proj=True)
22112219

22122220

22132221
def __write_compdb(tmpdir, test_file):
@@ -2231,12 +2239,13 @@ def __test_ignore_project_2(tmpdir, extra_args, append=False, inject_path=False)
22312239
pass
22322240

22332241
lines_exp = [
2242+
'ignored path: {}'.format(str(test_file).replace('\\', '/')),
22342243
'cppcheck: error: no C or C++ source files found.',
22352244
'cppcheck: all paths were ignored'
22362245
]
22372246
project_file = __write_compdb(tmpdir, test_file)
22382247
args = [
2239-
'-q',
2248+
'--debug-ignore',
22402249
'--project={}'.format(project_file)
22412250
]
22422251

@@ -2264,6 +2273,20 @@ def test_ignore_project_2_file_append(tmpdir):
22642273
__test_ignore_project_2(tmpdir, ['-itest.cpp'], append=True)
22652274

22662275

2276+
@pytest.mark.xfail(strict=True) # TODO: PathMatch lacks wildcard support / -i appears to be ignored
2277+
def test_ignore_project_2_file_wildcard_back(tmpdir):
2278+
__test_ignore_project_2(tmpdir, ['-itest.c*'])
2279+
2280+
2281+
def test_ignore_project_2_file_wildcard_front(tmpdir):
2282+
__test_ignore_project_2(tmpdir, ['-i*test.cpp'])
2283+
2284+
2285+
@pytest.mark.xfail(strict=True) # TODO: PathMatch lacks wildcard support / -i appears to be ignored
2286+
def test_ignore_project_2_file_placeholder(tmpdir):
2287+
__test_ignore_project_2(tmpdir, ['-it?st.cpp'])
2288+
2289+
22672290
@pytest.mark.xfail(strict=True) # TODO: -i appears to be ignored
22682291
def test_ignore_project_2_file_relative(tmpdir):
22692292
__test_ignore_project_2(tmpdir, ['-isrc/test.cpp'])
@@ -2274,6 +2297,16 @@ def test_ignore_project_2_file_relative_backslash(tmpdir):
22742297
__test_ignore_project_2(tmpdir, ['-isrc\\test.cpp'])
22752298

22762299

2300+
@pytest.mark.xfail(strict=True) # TODO: PathMatch lacks wildcard support / -i appears to be ignored
2301+
def test_ignore_project_2_file_relative_wildcard(tmpdir):
2302+
__test_ignore_project_2(tmpdir, ['-isrc/test.c*'])
2303+
2304+
2305+
@pytest.mark.xfail(strict=True) # TODO: PathMatch lacks wildcard support / -i appears to be ignored
2306+
def test_ignore_project_2_file_relative_wildcard_backslash(tmpdir):
2307+
__test_ignore_project_2(tmpdir, ['-isrc\\test.c*'])
2308+
2309+
22772310
def test_ignore_project_2_path_relative(tmpdir):
22782311
__test_ignore_project_2(tmpdir, ['-isrc/'])
22792312

@@ -2282,6 +2315,16 @@ def test_ignore_project_2_path_relative_backslash(tmpdir):
22822315
__test_ignore_project_2(tmpdir, ['-isrc\\'])
22832316

22842317

2318+
@pytest.mark.xfail(strict=True) # TODO: PathMatch lacks wildcard support
2319+
def test_ignore_project_2_path_relative_wildcard(tmpdir):
2320+
__test_ignore_project_2(tmpdir, ['-isrc*/'])
2321+
2322+
2323+
@pytest.mark.xfail(strict=True) # TODO: PathMatch lacks wildcard support
2324+
def test_ignore_project_2_path_relative_wildcard_backslash(tmpdir):
2325+
__test_ignore_project_2(tmpdir, ['-isrc*\\'])
2326+
2327+
22852328
def test_ignore_project_2_abspath(tmpdir):
22862329
__test_ignore_project_2(tmpdir, ['-i$path'], inject_path=True)
22872330

0 commit comments

Comments
 (0)