From 13e9ca11509264c0981a5d635f3a678b2bbb55be Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 8 Dec 2023 20:44:13 +0100 Subject: [PATCH] small cleanup of handling of ignored paths --- cli/cmdlineparser.cpp | 24 ++++++++++++++++++------ test/cli/test-more-projects.py | 32 ++++++++++++++++++++++++++++++-- test/cli/test-other.py | 20 +++++++++++++++++++- test/testcmdlineparser.cpp | 9 +++++++++ 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index df9bd320428..f2393d87c63 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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,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; diff --git a/test/cli/test-more-projects.py b/test/cli/test-more-projects.py index d84ff874633..36e1b2d98fd 100644 --- a/test/cli/test-more-projects.py +++ b/test/cli/test-more-projects.py @@ -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( """ - + -""") +""".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( + """ + + + + +""".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) \ No newline at end of file diff --git a/test/cli/test-other.py b/test/cli/test-other.py index 0ad6573f694..8787fcb6611 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -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) diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index c4b6ed38ff2..4d219b7132c 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -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); @@ -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"};