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
6 changes: 5 additions & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,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
Expand Down
8 changes: 8 additions & 0 deletions lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <sys/stat.h>
#include <utility>

#include <simplecpp.h>
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions lib/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ class CPPCHECKLIB Path {
*/
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
*/
Expand Down
25 changes: 25 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(undefs_noarg3);
TEST_CASE(undefs);
TEST_CASE(undefs2);

TEST_CASE(cppcheckBuildDirExistent);
TEST_CASE(cppcheckBuildDirNonExistent);
TEST_CASE(cppcheckBuildDirEmpty);
}


Expand Down Expand Up @@ -1947,6 +1951,27 @@ class TestCmdlineParser : public TestFixture {
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)