Adding fileutils.h support for absolute paths

This CL completes the effort done in http://review.webrtc.org/858014/ to make tests find the resources in various scenarios.

Slightly modified tests since there were a bit confusing and that their description conflicted with the current functionality.

BUG=Tests that are run with full absolute paths cannot find resources.
TEST=Local tests using absolute paths + trybots

Review URL: https://webrtc-codereview.appspot.com/878007

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2936 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kjellander@webrtc.org 2012-10-17 04:39:44 +00:00
parent 363efef64a
commit 193600b7cf
4 changed files with 28 additions and 24 deletions

View File

@ -18,7 +18,7 @@ namespace webrtc {
namespace test {
TestSuite::TestSuite(int argc, char** argv) {
SetRelativeExecutablePath(argv[0]);
SetExecutablePath(argv[0]);
testing::InitGoogleMock(&argc, argv); // Runs InitGoogleTest() internally.
}

View File

@ -54,9 +54,16 @@ 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));
void SetExecutablePath(const std::string& path) {
std::string working_dir = WorkingDir();
std::string temp_path = path;
// Handle absolute paths; convert them to relative paths to the working dir.
if (path.find(working_dir) != std::string::npos) {
temp_path = path.substr(working_dir.length() + 1);
}
// Trim away the executable name; only store the relative dir path.
temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
relative_dir_path_set = true;
}
@ -91,8 +98,8 @@ std::string ProjectRootPath() {
path = path + kPathDelimiter + relative_dir_path;
}
// Check for our file that verifies the root dir.
int path_delimiter_index = path.find_last_of(kPathDelimiter);
while (path_delimiter_index > -1) {
size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
while (path_delimiter_index != std::string::npos) {
std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
if (FileExists(root_filename)) {
return path + kPathDelimiter;

View File

@ -138,12 +138,13 @@ bool CreateDirectory(std::string directory_name);
// empty or if the file does not exist/is readable.
size_t GetFileSize(std::string filename);
// Sets the relative executable path, i.e. the path to the executable relative
// to the working directory (the value of argv[0] for the main function on most
// platforms). By using this function, it becomes possible to fileutils.h to
// find the correct project paths even when the working directory is outside the
// project tree when running programs linked with the test_support_main target.
void SetRelativeExecutablePath(const std::string& relative_path_to_executable);
// Sets the executable path, i.e. the path to the executable that is being used
// when launching it. This is usually the path relative to the working directory
// but can also be an absolute path. The intention with this function is to pass
// the argv[0] being sent into the main function to make it possible for
// fileutils.h to find the correct project paths even when the working directory
// is outside the project tree (which happens in some cases).
void SetExecutablePath(const std::string& path_to_executable);
} // namespace test
} // namespace webrtc

View File

@ -111,17 +111,7 @@ TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
}
// 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_F(FileUtilsTest, ProjectRootPathFromRootWorkingDir) {
// Change current working dir to the root of the current file system
// (this will always be "above" our project root dir).
ASSERT_EQ(0, chdir(kPathDelimiter));
ASSERT_EQ(webrtc::test::kCannotFindProjectRootDir,
webrtc::test::ProjectRootPath());
}
// Similar to the above test, but for the output dir
// directory tree than the project root dir.
TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
ASSERT_EQ(0, chdir(kPathDelimiter));
ASSERT_EQ("./", webrtc::test::OutputPath());
@ -151,8 +141,14 @@ TEST_F(FileUtilsTest, ResourcePathReturnsValue) {
std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
ASSERT_GT(resource.find(kTestName), 0u);
ASSERT_GT(resource.find(kExtension), 0u);
}
TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
ASSERT_EQ(0, chdir(kPathDelimiter));
ASSERT_EQ("./", webrtc::test::OutputPath());
std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
ASSERT_NE(resource.find("resources"), std::string::npos);
ASSERT_GT(resource.find(kTestName), 0u);
ASSERT_GT(resource.find(kExtension), 0u);
}
TEST_F(FileUtilsTest, GetFileSizeExistingFile) {