Skip to content

Commit

Permalink
Fix #12505 (cli: Add option --check-version to pin cppcheck version)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Mar 13, 2024
1 parent c5ff68f commit e0a55e4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
31 changes: 22 additions & 9 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ static bool addPathsToSet(const std::string& fileName, std::set<std::string>& se
return true;
}

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

namespace {
class XMLErrorMessagesLogger : public ErrorLogger
{
Expand Down Expand Up @@ -356,15 +365,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(mSettings);
mLogger.printRaw(version);
return Result::Exit;
}
}
Expand Down Expand Up @@ -488,6 +490,17 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.checkLibrary = true;
}

else if (std::strncmp(argv[i], "--check-version=", 16) == 0) {
if (!loadCppcheckCfg())
return Result::Fail;
const std::string actualVersion = getVersion(mSettings);
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
3 changes: 2 additions & 1 deletion releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Other:
- Removed deprecated platform type 'Unspecified'. Please use 'unspecified' instead.
- Removed deprecated 'Makefile' option 'SRCDIR'.
- 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'.
- Removed CMake option 'USE_THREADS' in favor of 'DISALLOW_THREAD_EXECUTOR'.
- Add option '--check-version', you can use it to pin the cppcheck version in a script.
18 changes: 18 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(versionWithCfg);
TEST_CASE(versionExclusive);
TEST_CASE(versionWithInvalidCfg);
TEST_CASE(checkVersion);
TEST_CASE(onefile);
TEST_CASE(onepath);
TEST_CASE(optionwithoutfile);
Expand Down Expand Up @@ -466,6 +467,23 @@ 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 checkVersion() {
REDIRECT;
// get version
const char * const argv1[] = {"cppcheck", "--version"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Exit, parser->parseFromArgs(2, argv1));
std::string version = logger->str();
version.erase(version.find('\n'));
const std::string checkCorrectVersion = "--check-version=" + version;
// check version: correct version
const char * const argv2[] = {"cppcheck", checkCorrectVersion.c_str(), "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv2));
// check version: incorrect version
const char * const argv3[] = {"cppcheck", "--check-version=Cppcheck 2.0", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv3));
ASSERT_EQUALS("cppcheck: error: --check-version check failed. Aborting.\n", logger->str());
}

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

0 comments on commit e0a55e4

Please sign in to comment.