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
diff --git a/test/testsupport/fileutils.cc b/test/testsupport/fileutils.cc
index 098dad7..9b54989 100644
--- a/test/testsupport/fileutils.cc
+++ b/test/testsupport/fileutils.cc
@@ -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 @@
// 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 @@
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