Check if --cppcheck-build-dir exists (#5254)

Cppcheck does not report that cppcheck build dir does not exist and also
does not report any write issues to the non-existent directory.

This means that cppcheck build dir is actually not used.

We should either create the directory or fail.
This commit is contained in:
Samuel Poláček 2023-08-04 17:38:43 +02:00 committed by GitHub
parent 5d201c4e87
commit b2511fb3ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 1 deletions

View File

@ -285,10 +285,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
}
else if (std::strncmp(argv[i], "--cppcheck-build-dir=", 21) == 0) {
// TODO: bail out when the folder does not exist? will silently do nothing
mSettings.buildDir = Path::fromNativeSeparators(argv[i] + 21);
if (endsWith(mSettings.buildDir, '/'))
mSettings.buildDir.pop_back();
if (!Path::directoryExists(mSettings.buildDir)) {
printError("Directory '" + mSettings.buildDir + "' specified by --cppcheck-build-dir argument has to be existent.");
return false;
}
}
// Show --debug output after the first simplifications

View File

@ -26,6 +26,7 @@
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <sys/stat.h>
#include <utility>
#include <simplecpp.h>
@ -266,6 +267,13 @@ bool Path::fileExists(const std::string &file)
return f.is_open();
}
bool Path::directoryExists(const std::string &path)
{
struct stat info;
return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
}
std::string Path::join(std::string path1, std::string path2) {
if (path1.empty() || path2.empty())
return path1 + path2;

View File

@ -187,6 +187,13 @@ public:
*/
static bool fileExists(const std::string &file);
/**
* @brief Checks if a directory exists
* @param path Path to be checked
* @return true if given path is a directory
*/
static bool directoryExists(const std::string &path);
/**
* join 2 paths with '/' separators
*/

View File

@ -237,6 +237,10 @@ private:
TEST_CASE(undefs_noarg3);
TEST_CASE(undefs);
TEST_CASE(undefs2);
TEST_CASE(cppcheckBuildDirExistent);
TEST_CASE(cppcheckBuildDirNonExistent);
TEST_CASE(cppcheckBuildDirEmpty);
}
@ -1947,6 +1951,27 @@ private:
ASSERT_EQUALS(false, defParser.parseFromArgs(4, argv));
ASSERT_EQUALS("cppcheck: error: argument to '-U' is missing.\n", GET_REDIRECT_OUTPUT);
}
void cppcheckBuildDirExistent() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--cppcheck-build-dir=."};
ASSERT_EQUALS(true, defParser.parseFromArgs(2, argv));
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}
void cppcheckBuildDirNonExistent() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--cppcheck-build-dir=non-existent-path"};
ASSERT_EQUALS(false, defParser.parseFromArgs(2, argv));
ASSERT_EQUALS("cppcheck: error: Directory 'non-existent-path' specified by --cppcheck-build-dir argument has to be existent.\n", GET_REDIRECT_OUTPUT);
}
void cppcheckBuildDirEmpty() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--cppcheck-build-dir="};
ASSERT_EQUALS(false, defParser.parseFromArgs(2, argv));
ASSERT_EQUALS("cppcheck: error: Directory '' specified by --cppcheck-build-dir argument has to be existent.\n", GET_REDIRECT_OUTPUT);
}
};
REGISTER_TEST(TestCmdlineParser)