Improving the way fileutil.h finds test resources.
Adding the argv[0] path to the working directory to make it possible find the project root even when the test is executed from outside the project dir (like it is on some buildbots).
Removed tests that moves into directories since they no longer work with this approach. The new functionality have been verified by manual tests of the following cases, example with a checkout root dir called webrtc/:
Working dir: Command line:
webrtc trunk/out/Debug/test
webrtc/trunk out/Debug/test
webrtc/trunk/out Debug/test
webrtc/trunk/out ./Debug/test
webrtc/trunk/out/Debug ./test
webrtc/trunk/out/Debug/subdir ../test
webrtc/trunk/out/Debug/subdir ./../test
I also made another program with its own main method (only links with 'test_support', not 'test_support_main') and made sure that it was still possible to use as before (i.e. works within the project tree but not above it):
#include "testsupport/fileutils.h"
int main(int argc, char** argv) {
printf("Working dir: %s\n", webrtc::test::WorkingDir().c_str());
printf("Project root: %s\n", webrtc::test::ProjectRootPath().c_str());
printf("Output path: %s\n", webrtc::test::OutputPath().c_str());
}
BUG=Existing implementation cannot handle when the working directory is outside the project checkout.
TEST=test_support_unittests and manual tests with video_codecs_test_framework_integration_tests + passing all trybots + memcheck tool
Review URL: https://webrtc-codereview.appspot.com/858014
git-svn-id: http://webrtc.googlecode.com/svn/trunk@2927 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/src/test/testsupport/fileutils.cc b/src/test/testsupport/fileutils.cc
index 0679b1f..9ec8d5f 100644
--- a/src/test/testsupport/fileutils.cc
+++ b/src/test/testsupport/fileutils.cc
@@ -24,6 +24,7 @@
#endif
#include <cstdio>
+#include <cstring>
#include "typedefs.h" // For architecture defines
@@ -46,26 +47,41 @@
#endif
const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
+namespace {
+char relative_dir_path[FILENAME_MAX];
+bool relative_dir_path_set = false;
+}
+
+void SetRelativeExecutablePath(const std::string& path) {
+ // Trim away the executable name; we only want to store the relative dir path.
+ std::string temp_path = path.substr(0, path.find_last_of(kPathDelimiter));
+ strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
+ relative_dir_path_set = true;
+}
+
+bool FileExists(std::string& file_name) {
+ struct stat file_info = {0};
+ return stat(file_name.c_str(), &file_info) == 0;
+}
+
std::string ProjectRootPath() {
- std::string working_dir = WorkingDir();
- if (working_dir == kFallbackPath) {
+ std::string path = WorkingDir();
+ if (path == kFallbackPath) {
return kCannotFindProjectRootDir;
}
+ if (relative_dir_path_set) {
+ path = path + kPathDelimiter + relative_dir_path;
+ }
// Check for our file that verifies the root dir.
- std::string current_path(working_dir);
- FILE* file = NULL;
- int path_delimiter_index = current_path.find_last_of(kPathDelimiter);
+ int path_delimiter_index = path.find_last_of(kPathDelimiter);
while (path_delimiter_index > -1) {
- std::string root_filename = current_path + kPathDelimiter +
- kProjectRootFileName;
- file = fopen(root_filename.c_str(), "r");
- if (file != NULL) {
- fclose(file);
- return current_path + kPathDelimiter;
+ std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
+ if (FileExists(root_filename)) {
+ return 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(kPathDelimiter);
+ path = path.substr(0, path_delimiter_index);
+ path_delimiter_index = path.find_last_of(kPathDelimiter);
}
// Reached the root directory.
fprintf(stderr, "Cannot find project root directory!\n");
@@ -126,11 +142,6 @@
return true;
}
-bool FileExists(std::string file_name) {
- struct stat file_info = {0};
- return stat(file_name.c_str(), &file_info) == 0;
-}
-
std::string ResourcePath(std::string name, std::string extension) {
std::string platform = "win";
#ifdef WEBRTC_LINUX