small cleanup of handling of ignored paths (#5757)

This commit is contained in:
Oliver Stöneberg 2023-12-14 16:55:42 +01:00 committed by GitHub
parent 61127950b0
commit fcb41e2533
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 9 deletions

View File

@ -227,6 +227,11 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
std::copy_if(fileSettings.cbegin(), fileSettings.cend(), std::back_inserter(mFileSettings), [&](const FileSettings &fs) {
return mSettings.library.markupFile(fs.filename) && mSettings.library.processMarkupAfterCode(fs.filename);
});
if (mFileSettings.empty()) {
mLogger.printError("could not find or open any of the paths given.");
return false;
}
}
if (!pathnamesRef.empty()) {
@ -239,6 +244,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
const bool caseSensitive = true;
#endif
// Execute recursiveAddFiles() to each given file parameter
// TODO: verbose log which files were ignored?
const PathMatch matcher(ignored, caseSensitive);
for (const std::string &pathname : pathnamesRef) {
const std::string err = FileLister::recursiveAddFiles(filesResolved, Path::toNativeSeparators(pathname), mSettings.library.markupExtensions(), matcher);
@ -248,6 +254,14 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
}
}
if (filesResolved.empty()) {
mLogger.printError("could not find or open any of the paths given.");
// TODO: PathMatch should provide the information if files were ignored
if (!ignored.empty())
mLogger.printMessage("Maybe all paths were ignored?");
return false;
}
// de-duplicate files
{
auto it = filesResolved.begin();
@ -283,14 +297,12 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
std::copy_if(files.cbegin(), files.cend(), std::inserter(mFiles, mFiles.end()), [&](const decltype(files)::value_type& entry) {
return mSettings.library.markupFile(entry.first) && mSettings.library.processMarkupAfterCode(entry.first);
});
}
if (mFiles.empty() && mFileSettings.empty()) {
if (mFiles.empty()) {
mLogger.printError("could not find or open any of the paths given.");
if (!ignored.empty())
mLogger.printMessage("Maybe all paths were ignored?");
return false;
}
}
return true;
}

View File

@ -371,15 +371,19 @@ def test_project_file_filter_3(tmpdir):
def test_project_file_filter_no_match(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt') as f:
pass
project_file = os.path.join(tmpdir, 'test.cppcheck')
with open(project_file, 'wt') as f:
f.write(
"""<?xml version="1.0" encoding="UTF-8"?>
<project>
<paths>
<dir name="test.cpp"/>
<dir name="{}"/>
</paths>
</project>""")
</project>""".format(test_file))
args = ['--file-filter=*.c', '--project={}'.format(project_file)]
out_lines = [
@ -504,3 +508,27 @@ def test_project_file_duplicate_2(tmpdir):
'3/3 files checked 0% done'
]
assert stderr == ''
def test_project_file_ignore(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt') as f:
pass
project_file = os.path.join(tmpdir, 'test.cppcheck')
with open(project_file, 'wt') as f:
f.write(
"""<?xml version="1.0" encoding="UTF-8"?>
<project>
<paths>
<dir name="{}"/>
</paths>
</project>""".format(test_file))
args = ['-itest.cpp', '--project={}'.format(project_file)]
out_lines = [
'cppcheck: error: could not find or open any of the paths given.',
'cppcheck: Maybe all paths were ignored?'
]
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)

View File

@ -598,7 +598,11 @@ def test_file_filter_3(tmpdir):
def test_file_filter_no_match(tmpdir):
args = ['--file-filter=*.c', 'test.cpp']
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt'):
pass
args = ['--file-filter=*.c', test_file]
out_lines = [
'cppcheck: error: could not find any files matching the filter.'
]
@ -825,3 +829,17 @@ def test_file_duplicate_2(tmpdir):
'3/3 files checked 0% done'
]
assert stderr == ''
def test_file_ignore(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt'):
pass
args = ['-itest.cpp', test_file]
out_lines = [
'cppcheck: error: could not find or open any of the paths given.',
'cppcheck: Maybe all paths were ignored?'
]
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)

View File

@ -350,6 +350,7 @@ private:
TEST_CASE(ignorepaths4);
TEST_CASE(ignorefilepaths1);
TEST_CASE(ignorefilepaths2);
TEST_CASE(ignorefilepaths3);
TEST_CASE(checkconfig);
TEST_CASE(unknownParam);
@ -2228,6 +2229,14 @@ private:
ASSERT_EQUALS("src/foo.cpp", parser->getIgnoredPaths()[0]);
}
void ignorefilepaths3() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-i", "foo.cpp", "file.cpp"};
ASSERT_EQUALS(CmdLineParser::Result::Success, parser->parseFromArgs(4, argv));
ASSERT_EQUALS(1, parser->getIgnoredPaths().size());
ASSERT_EQUALS("foo.cpp", parser->getIgnoredPaths()[0]);
}
void checkconfig() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--check-config", "file.cpp"};