Adding GetOutputDir method to test_support library.

The unittest is not ideal for this, but I would have to use similar code as the implementation of the GetOutputDir in order to verify that it actually runs, so it wouldn't make much sense with a test like that.

It compiles and runs on Linux, Win and Mac. The folder gets created and is writeable from other tests.

I have tried using the GetOutputDir from another project that writes output files and it works as intended on all platforms.

Review URL: http://webrtc-codereview.appspot.com/270001

git-svn-id: http://webrtc.googlecode.com/svn/trunk@906 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kjellander@webrtc.org 2011-11-09 11:24:14 +00:00
parent 9dcab8fb14
commit 4d8cd9d055
3 changed files with 113 additions and 18 deletions

View File

@ -13,11 +13,14 @@
#ifdef WIN32
#include <direct.h>
#define GET_CURRENT_DIR _getcwd
#define PATH_DELIMITER "\\"
#else
#include <unistd.h>
#define GET_CURRENT_DIR getcwd
#define PATH_DELIMITER "/"
#endif
#include <sys/stat.h> // To check for directory existence.
#ifndef S_ISDIR // Not defined in stat.h on Windows.
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#include <cstdio>
@ -25,8 +28,14 @@
namespace webrtc {
namespace test {
#ifdef WIN32
static const char* kPathDelimiter = "\\";
#else
static const char* kPathDelimiter = "/";
#endif
// The file we're looking for to identify the project root dir.
static const char* kProjectRootFileName = "DEPS";
static const char* kOutputDirName = "out";
const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
std::string GetProjectRootPath() {
@ -39,18 +48,18 @@ std::string GetProjectRootPath() {
// Check for our file that verifies the root dir.
std::string current_path(path_buffer);
FILE* file = NULL;
int path_delimiter_index = current_path.find_last_of(PATH_DELIMITER);
int path_delimiter_index = current_path.find_last_of(kPathDelimiter);
while (path_delimiter_index > -1) {
std::string root_filename = current_path + PATH_DELIMITER +
std::string root_filename = current_path + kPathDelimiter +
kProjectRootFileName;
file = fopen(root_filename.c_str(), "r");
if (file != NULL) {
return current_path + PATH_DELIMITER;
return current_path + kPathDelimiter;
}
// Move up one directory in the directory tree.
current_path = current_path.substr(0, path_delimiter_index);
path_delimiter_index = current_path.find_last_of(PATH_DELIMITER);
path_delimiter_index = current_path.find_last_of(kPathDelimiter);
}
// Reached the root directory.
@ -58,5 +67,28 @@ std::string GetProjectRootPath() {
return kCannotFindProjectRootDir;
}
} // namespace webrtc
std::string GetOutputDir() {
std::string path = GetProjectRootPath();
if (path == kCannotFindProjectRootDir) {
return kCannotFindProjectRootDir;
}
path += kOutputDirName;
struct stat path_info = {0};
// Check if the path exists already:
if (stat(path.c_str(), &path_info) == 0) {
if (!S_ISDIR(path_info.st_mode)) {
fprintf(stderr, "Path %s exists but is not a directory! Remove this file "
"and re-run to create the output folder.\n", path.c_str());
return kCannotFindProjectRootDir;
}
} else {
#ifdef WIN32
_mkdir(path.c_str());
#else
mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
}
return path + kPathDelimiter;
}
} // namespace test
} // namespace webrtc

View File

@ -88,6 +88,19 @@ extern const char* kCannotFindProjectRootDir;
// kCannotFindProjectRootDir is returned.
std::string GetProjectRootPath();
// Creates and returns the absolute path to the output directory where log files
// and other test artifacts should be put. The output directory is always a
// directory named "out" at the top-level of the project, i.e. a subfolder to
// the path returned by GetProjectRootPath().
//
// Details described for GetProjectRootPath() apply here too.
//
// Returns the absolute path to the output directory (named "out") below the
// project root dir WITH a trailing path delimiter.
// If the project root is not found, the string specified by
// kCannotFindProjectRootDir is returned.
std::string GetOutputDir();
} // namespace test
} // namespace webrtc

View File

@ -7,53 +7,103 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <cstdio>
#include "fileutils.h"
#include "gtest/gtest.h"
#ifdef WIN32
#define PATH_DELIMITER "\\"
#include <direct.h>
#define GET_CURRENT_DIR _getcwd
static const char* kPathDelimiter = "\\";
#else
#define PATH_DELIMITER "/"
#include <unistd.h>
#define GET_CURRENT_DIR getcwd
static const char* kPathDelimiter = "/";
#endif
namespace webrtc {
namespace test {
// Tests that the project root path is returnd for the default working directory
// that is automatically set when the test executable is launched.
// Test fixture to restore the working directory between each test, since some
// of them change it with chdir during execution (not restored by the
// gtest framework).
class FileUtilsTest: public testing::Test {
protected:
FileUtilsTest() {
original_working_dir_ = GetWorkingDir();
}
virtual ~FileUtilsTest() {}
void SetUp() {
chdir(original_working_dir_.c_str());
}
void TearDown() {}
private:
std::string original_working_dir_;
static std::string GetWorkingDir() {
char path_buffer[FILENAME_MAX];
EXPECT_TRUE(GET_CURRENT_DIR(path_buffer, sizeof(path_buffer)))
<< "Cannot get current working directory!";
return std::string(path_buffer);
}
};
// Tests that the project root path is returned for the default working
// directory that is automatically set when the test executable is launched.
// The test is not fully testing the implementation, since we cannot be sure
// of where the executable was launched from.
// The test will fail if the top level directory is not named "trunk".
TEST(FileUtilsTest, GetProjectRootPathFromUnchangedWorkingDir) {
TEST_F(FileUtilsTest, GetProjectRootPathFromUnchangedWorkingDir) {
std::string path = GetProjectRootPath();
std::string expected_end = "trunk";
expected_end = PATH_DELIMITER + expected_end + PATH_DELIMITER;
expected_end = kPathDelimiter + expected_end + kPathDelimiter;
ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end));
}
// Similar to the above test, but for the output dir
TEST_F(FileUtilsTest, GetOutputDirFromUnchangedWorkingDir) {
std::string path = GetOutputDir();
std::string expected_end = "out";
expected_end = kPathDelimiter + expected_end + kPathDelimiter;
ASSERT_EQ(path.length() - expected_end.length(), path.find(expected_end));
}
// Tests setting the current working directory to a directory three levels
// deeper from the current one. Then testing that the project path returned
// is still the same, when the function under test is called again.
TEST(FileUtilsTest, GetProjectRootPathFromDeeperWorkingDir) {
TEST_F(FileUtilsTest, GetProjectRootPathFromDeeperWorkingDir) {
std::string path = GetProjectRootPath();
std::string original_working_dir = path; // This is the correct project root
// Change to a subdirectory path (the full path doesn't have to exist).
path += "foo/bar/baz";
chdir(path.c_str());
ASSERT_EQ(original_working_dir, GetProjectRootPath());
}
// Similar to the above test, but for the output dir
TEST_F(FileUtilsTest, GetOutputDirFromDeeperWorkingDir) {
std::string path = GetOutputDir();
std::string original_working_dir = path;
path += "foo/bar/baz";
chdir(path.c_str());
ASSERT_EQ(original_working_dir, GetOutputDir());
}
// Tests with current working directory set to a directory higher up in the
// directory tree than the project root dir. This case shall return a specified
// error string as a directory (which will be an invalid path).
TEST(FileUtilsTest, GetProjectRootPathFromRootWorkingDir) {
TEST_F(FileUtilsTest, GetProjectRootPathFromRootWorkingDir) {
// Change current working dir to the root of the current file system
// (this will always be "above" our project root dir).
chdir(PATH_DELIMITER);
chdir(kPathDelimiter);
ASSERT_EQ(kCannotFindProjectRootDir, GetProjectRootPath());
}
// Similar to the above test, but for the output dir
TEST_F(FileUtilsTest, GetOutputDirFromRootWorkingDir) {
chdir(kPathDelimiter);
ASSERT_EQ(kCannotFindProjectRootDir, GetOutputDir());
}
} // namespace test
} // namespace webrtc