Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12505 (cli: Add option --check-version to pin cppcheck version) #6121

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
if (std::strcmp(argv[i], "--version") == 0) {
if (!loadCppcheckCfg())
return Result::Fail;
if (!mSettings.cppcheckCfgProductName.empty()) {
mLogger.printRaw(mSettings.cppcheckCfgProductName);
} else {
const char * const extraVersion = CppCheck::extraVersion();
if (*extraVersion != '\0')
mLogger.printRaw(std::string("Cppcheck ") + CppCheck::version() + " ("+ extraVersion + ')');
else
mLogger.printRaw(std::string("Cppcheck ") + CppCheck::version());
}
const std::string version = getVersion();
mLogger.printRaw(version);
return Result::Exit;
}
}
Expand Down Expand Up @@ -488,6 +481,17 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.checkLibrary = true;
}

else if (std::strncmp(argv[i], "--check-version=", 16) == 0) {
danmar marked this conversation as resolved.
Show resolved Hide resolved
if (!loadCppcheckCfg())
return Result::Fail;
const std::string actualVersion = getVersion();
const std::string wantedVersion = argv[i] + 16;
if (actualVersion != wantedVersion) {
mLogger.printError("--check-version check failed. Aborting.");
return Result::Fail;
}
}

else if (std::strncmp(argv[i], "--checkers-report=", 18) == 0)
mSettings.checkersReportFilename = argv[i] + 18;

Expand Down Expand Up @@ -1736,6 +1740,15 @@ void CmdLineParser::printHelp() const
mLogger.printRaw(oss.str());
}

std::string CmdLineParser::getVersion() const {
if (!mSettings.cppcheckCfgProductName.empty())
return mSettings.cppcheckCfgProductName;
const char * const extraVersion = CppCheck::extraVersion();
if (*extraVersion != '\0')
return std::string("Cppcheck ") + CppCheck::version() + " ("+ extraVersion + ')';
return std::string("Cppcheck ") + CppCheck::version();
}

bool CmdLineParser::isCppcheckPremium() const {
if (mSettings.cppcheckCfgProductName.empty())
Settings::loadCppcheckCfg(mSettings, mSettings.supprs);
Expand Down
5 changes: 5 additions & 0 deletions cli/cmdlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class CmdLineParser {
return mIgnoredPaths;
}

/**
* Get Cppcheck version
*/
std::string getVersion() const;

protected:

/**
Expand Down
3 changes: 2 additions & 1 deletion releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ Other:
- Added CMake option 'DISALLOW_THREAD_EXECUTOR' to control the inclusion of the executor which performs the analysis within a thread of the main process.
- Removed CMake option 'USE_THREADS' in favor of 'DISALLOW_THREAD_EXECUTOR'.
- Fixed crash with '--rule-file=' if some data was missing.
- '--rule-file' will now bail out if a rule could not be added or a file contains unexpected data.
- '--rule-file' will now bail out if a rule could not be added or a file contains unexpected data.
- Add option '--check-version', you can use it to pin the cppcheck version in a script.
17 changes: 17 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(versionWithCfg);
TEST_CASE(versionExclusive);
TEST_CASE(versionWithInvalidCfg);
TEST_CASE(checkVersionCorrect);
TEST_CASE(checkVersionIncorrect);
TEST_CASE(onefile);
TEST_CASE(onepath);
TEST_CASE(optionwithoutfile);
Expand Down Expand Up @@ -471,6 +473,21 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 2 near: \n", logger->str());
}

void checkVersionCorrect() {
REDIRECT;
const std::string currentVersion = parser->getVersion();
const std::string checkVersion = "--check-version=" + currentVersion;
const char * const argv[] = {"cppcheck", checkVersion.c_str(), "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
}

void checkVersionIncorrect() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--check-version=Cppcheck 2.0", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: --check-version check failed. Aborting.\n", logger->str());
}

void onefile() {
REDIRECT;
const char * const argv[] = {"cppcheck", "file.cpp"};
Expand Down
Loading