Fixed #2358 (Compilation fail on Mac)

Added test case to prove it works. Not a true unit-test as it accesses the file-system.
This commit is contained in:
Pete Johns 2010-12-26 20:53:09 +11:00
parent 4fd8d2e056
commit 64e2c8668c
3 changed files with 71 additions and 6 deletions

View File

@ -65,6 +65,7 @@ TESTOBJ = test/options.o \
test/testdivision.o \
test/testerrorlogger.o \
test/testexceptionsafety.o \
test/testfilelister_unix.o \
test/testincompletestatement.o \
test/testmathlib.o \
test/testmemleak.o \
@ -171,7 +172,7 @@ lib/checkunusedfunctions.o: lib/checkunusedfunctions.cpp lib/checkunusedfunction
lib/cppcheck.o: lib/cppcheck.cpp lib/cppcheck.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/preprocessor.h lib/filelister.h lib/path.h lib/timer.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/cppcheck.o lib/cppcheck.cpp
lib/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/path.h
lib/errorlogger.o: lib/errorlogger.cpp lib/errorlogger.h lib/path.h lib/cppcheck.h lib/settings.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h
$(CXX) $(CXXFLAGS) -Ilib -c -o lib/errorlogger.o lib/errorlogger.cpp
lib/executionpath.o: lib/executionpath.cpp lib/executionpath.h lib/token.h
@ -255,6 +256,9 @@ test/testerrorlogger.o: test/testerrorlogger.cpp test/testsuite.h lib/errorlogge
test/testexceptionsafety.o: test/testexceptionsafety.cpp lib/tokenize.h lib/checkexceptionsafety.h lib/check.h lib/token.h lib/settings.h lib/errorlogger.h test/testsuite.h test/redirect.h
$(CXX) $(CXXFLAGS) -Ilib -Icli -Iexternals -c -o test/testexceptionsafety.o test/testexceptionsafety.cpp
test/testfilelister_unix.o: test/testfilelister_unix.cpp test/testsuite.h lib/errorlogger.h test/redirect.h
$(CXX) $(CXXFLAGS) -Ilib -Icli -Iexternals -c -o test/testfilelister_unix.o test/testfilelister_unix.cpp
test/testincompletestatement.o: test/testincompletestatement.cpp test/testsuite.h lib/errorlogger.h test/redirect.h lib/tokenize.h lib/checkother.h lib/check.h lib/token.h lib/settings.h
$(CXX) $(CXXFLAGS) -Ilib -Icli -Iexternals -c -o test/testincompletestatement.o test/testincompletestatement.cpp

View File

@ -58,14 +58,15 @@ void FileListerUnix::recursiveAddFiles2(std::vector<std::string> &relative,
if (filename[filename.length()-1] != '/')
{
// File
char* const fname = realpath(filename.c_str(), NULL);
if (!fname)
char fname[PATH_MAX];
if (realpath(filename.c_str(), fname) == NULL)
{
continue;
}
// Does absolute path exist? then bail out
if (std::find(absolute.begin(), absolute.end(), std::string(fname)) != absolute.end())
{
free(fname);
continue;
}
@ -74,8 +75,6 @@ void FileListerUnix::recursiveAddFiles2(std::vector<std::string> &relative,
relative.push_back(filename);
absolute.push_back(fname);
}
free(fname);
}
else
{

View File

@ -0,0 +1,62 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "testsuite.h"
#define private public
#include "filelister_unix.h"
class TestFileLister: public TestFixture
{
public:
TestFileLister()
:TestFixture("TestFileLister")
{}
private:
void run()
{
TEST_CASE(test_recursiveAddFiles2);
}
void test_recursiveAddFiles2()
{
std::vector<std::string> relative, absolute;
FileListerUnix ful;
ful.recursiveAddFiles2(relative, absolute, ".");
ASSERT(relative.size() != 0);
ASSERT_EQUALS(relative.size(), absolute.size());
for (std::vector<std::string>::const_iterator r = relative.begin(), r_end = relative.end(),
a = absolute.begin(), a_end = absolute.end();
r != r_end && a != a_end;
++r, ++a
)
{
static const size_t start_at_relative = std::string("./").size();
static const size_t start_at_absolute = std::string("./").size() + a->size() - r->size();
ASSERT_EQUALS(r->substr(start_at_relative), a->substr(start_at_absolute));
}
}
};
REGISTER_TEST(TestFileLister)