Skip to content

Commit

Permalink
small cleanup of handling of ignored paths (#5757)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Dec 14, 2023
1 parent 6112795 commit fcb41e2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
24 changes: 18 additions & 6 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -283,13 +297,11 @@ 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()) {
mLogger.printError("could not find or open any of the paths given.");
if (!ignored.empty())
mLogger.printMessage("Maybe all paths were ignored?");
return false;
if (mFiles.empty()) {
mLogger.printError("could not find or open any of the paths given.");
return false;
}
}

return true;
Expand Down
32 changes: 30 additions & 2 deletions test/cli/test-more-projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)
20 changes: 19 additions & 1 deletion test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
]
Expand Down Expand Up @@ -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)
9 changes: 9 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(ignorepaths4);
TEST_CASE(ignorefilepaths1);
TEST_CASE(ignorefilepaths2);
TEST_CASE(ignorefilepaths3);

TEST_CASE(checkconfig);
TEST_CASE(unknownParam);
Expand Down Expand Up @@ -2228,6 +2229,14 @@ class TestCmdlineParser : public TestFixture {
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"};
Expand Down

0 comments on commit fcb41e2

Please sign in to comment.