Skip to content
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
10 changes: 10 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,16 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
mSettings->plistOutput = "./";
else if (!endsWith(mSettings->plistOutput,'/'))
mSettings->plistOutput += '/';

const std::string plistOutput = Path::toNativeSeparators(mSettings->plistOutput);
if (!FileLister::isDirectory(plistOutput))
{
std::string message("cppcheck: error: plist folder does not exist: \"");
message += plistOutput;
message += "\".";
printMessage(message);
return false;
}
}

// --project
Expand Down
2 changes: 1 addition & 1 deletion cli/filelister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static BOOL myFileExists(const std::string& path)
if (fa != INVALID_FILE_ATTRIBUTES && !(fa & FILE_ATTRIBUTE_DIRECTORY))
result = TRUE;
#else
const BOOL result = PathFileExistsA(path.c_str());
const BOOL result = PathFileExistsA(path.c_str()) && !PathIsDirectoryA(path.c_str());
#endif
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
filename2 = filename.substr(filename.rfind('/') + 1);
else
filename2 = filename;
filename2 = mSettings.plistOutput + filename2.substr(0, filename2.find('.')) + ".plist";
std::size_t fileNameHash = std::hash<std::string>{}(filename);
filename2 = mSettings.plistOutput + filename2.substr(0, filename2.find('.')) + "_" + std::to_string(fileNameHash) + ".plist";
plistFile.open(filename2);
plistFile << ErrorLogger::plistHeader(version(), files);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ class CPPCHECKLIB Path {
*/
static std::string stripDirectoryPart(const std::string &file);

/**
* @brief Checks if a File exists
* @param path Path to be checked if it is a File
* @return true if given path is a File
*/
static bool fileExists(const std::string &file);
};

Expand Down
18 changes: 18 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(stdc99);
TEST_CASE(stdcpp11);
TEST_CASE(platform);
TEST_CASE(plistEmpty);
TEST_CASE(plistDoesNotExist);
TEST_CASE(suppressionsOld); // TODO: Create and test real suppression file
TEST_CASE(suppressions);
TEST_CASE(suppressionsNoFile);
Expand Down Expand Up @@ -717,6 +719,22 @@ class TestCmdlineParser : public TestFixture {
ASSERT(settings.platformType == Settings::Win64);
}

void plistEmpty() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--plist-output=", "file.cpp"};
settings.plistOutput = "";
ASSERT(defParser.parseFromArgs(3, argv));
ASSERT(settings.plistOutput == "./");
}

void plistDoesNotExist() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--plist-output=./cppcheck_reports", "file.cpp"};
settings.plistOutput = "";
// Fails since folder pointed by --plist-output= does not exist
ASSERT_EQUALS(false, defParser.parseFromArgs(3, argv));
}

void suppressionsOld() {
// TODO: Fails because there is no suppr.txt file!
REDIRECT;
Expand Down
6 changes: 6 additions & 0 deletions test/testfilelister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TestFileLister: public TestFixture {

TEST_CASE(isDirectory);
TEST_CASE(recursiveAddFiles);
TEST_CASE(fileExists);
}

void isDirectory() const {
Expand Down Expand Up @@ -79,6 +80,11 @@ class TestFileLister: public TestFixture {
// Make sure headers are not added..
ASSERT(files.find("lib/tokenize.h") == files.end());
}

void fileExists() const {
ASSERT_EQUALS(false, FileLister::fileExists("lib"));
ASSERT_EQUALS(true, FileLister::fileExists("readme.txt"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this assertion. The testrunner binary is placed in different paths.. we cant expect that there will be a readme.txt in the build output folder.

The previous assertion is fine. If testrunner is placed in cppcheck root path then it checks that fileExists returns false for a existing folder. Otherwise the assertion will check that fileExists returns false for a non-existing path.. that is good also. The problem would be if the build output folder has a file with the name "lib" but we can maybe ignore that problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check if we are in the correct place during the test setup see line 41 of testfilelister.cpp
I can still remove it but for me it seems like a valid test.

}
};

REGISTER_TEST(TestFileLister)