Skip to content

Commit

Permalink
disallow multiple --project options / TestCmdlineParser: added more…
Browse files Browse the repository at this point in the history
… `--project` tests
  • Loading branch information
firewave committed Oct 1, 2023
1 parent 0a1fc24 commit d55075f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,12 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])

// --project
else if (std::strncmp(argv[i], "--project=", 10) == 0) {
if (mSettings.project.projectType != ImportProject::Type::NONE)
{
mLogger.printError("multiple --project options are not supported.");
return false;
}

mSettings.checkAllConfigurations = false; // Can be overridden with --max-configs or --force
std::string projectFile = argv[i]+10;
ImportProject::Type projType = mSettings.project.import(projectFile, &mSettings);
Expand Down
3 changes: 2 additions & 1 deletion releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Changed interface:

Other:
- Windows builds now default to the `native` platform instead of `win32A` or `win64`. Please specify it explicitly if you depedent on it.
- The undocumented and deprecated command-line options `--template <template>` and `--template-format <template>` has been removed. Please use `--template=` and `--template-format=` instead.
- The undocumented and deprecated command-line options `--template <template>` and `--template-format <template>` has been removed. Please use `--template=` and `--template-format=` instead.
- Multiple "--project" options are now prohibited. These would have overlapped each other and leads to unexpected behavior. Please use separate runs with a single "--project" option instead.
35 changes: 35 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(typedefMaxTimeInvalid2);
TEST_CASE(templateMaxTime);
TEST_CASE(templateMaxTime);
TEST_CASE(project);
TEST_CASE(projectMultiple);
TEST_CASE(projectEmpty);
TEST_CASE(projectMissing);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -1871,6 +1875,37 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--typedef-max-time=' is not valid - needs to be positive.\n", logger->str());
}

void project() {
REDIRECT;
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(static_cast<int>(ImportProject::Type::CPPCHECK_GUI), static_cast<int>(settings->project.projectType));
ASSERT_EQUALS("", logger->str());
}

void projectMultiple() {
REDIRECT;
ScopedFile file("project.cppcheck", "<project></project>");
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(!parser->parseFromArgs(4, argv));
ASSERT_EQUALS("cppcheck: error: multiple --project options are not supported.\n", logger->str());
}

void projectEmpty() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--project=", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: failed to open project ''. The file does not exist.\n", logger->str());
}

void projectMissing() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--project=project.cppcheck", "file.cpp"};
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: failed to open project 'project.cppcheck'. The file does not exist.\n", logger->str());
}

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

0 comments on commit d55075f

Please sign in to comment.