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:
parent
363efef64a
commit
193600b7cf
@ -18,7 +18,7 @@ namespace webrtc {
|
|||||||
namespace test {
|
namespace test {
|
||||||
|
|
||||||
TestSuite::TestSuite(int argc, char** argv) {
|
TestSuite::TestSuite(int argc, char** argv) {
|
||||||
SetRelativeExecutablePath(argv[0]);
|
SetExecutablePath(argv[0]);
|
||||||
testing::InitGoogleMock(&argc, argv); // Runs InitGoogleTest() internally.
|
testing::InitGoogleMock(&argc, argv); // Runs InitGoogleTest() internally.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,9 +54,16 @@ char relative_dir_path[FILENAME_MAX];
|
|||||||
bool relative_dir_path_set = false;
|
bool relative_dir_path_set = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetRelativeExecutablePath(const std::string& path) {
|
void SetExecutablePath(const std::string& path) {
|
||||||
// Trim away the executable name; we only want to store the relative dir path.
|
std::string working_dir = WorkingDir();
|
||||||
std::string temp_path = path.substr(0, path.find_last_of(kPathDelimiter));
|
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);
|
strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
|
||||||
relative_dir_path_set = true;
|
relative_dir_path_set = true;
|
||||||
}
|
}
|
||||||
@ -91,8 +98,8 @@ std::string ProjectRootPath() {
|
|||||||
path = path + kPathDelimiter + relative_dir_path;
|
path = path + kPathDelimiter + relative_dir_path;
|
||||||
}
|
}
|
||||||
// Check for our file that verifies the root dir.
|
// Check for our file that verifies the root dir.
|
||||||
int path_delimiter_index = path.find_last_of(kPathDelimiter);
|
size_t path_delimiter_index = path.find_last_of(kPathDelimiter);
|
||||||
while (path_delimiter_index > -1) {
|
while (path_delimiter_index != std::string::npos) {
|
||||||
std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
|
std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
|
||||||
if (FileExists(root_filename)) {
|
if (FileExists(root_filename)) {
|
||||||
return path + kPathDelimiter;
|
return path + kPathDelimiter;
|
||||||
|
@ -138,12 +138,13 @@ bool CreateDirectory(std::string directory_name);
|
|||||||
// empty or if the file does not exist/is readable.
|
// empty or if the file does not exist/is readable.
|
||||||
size_t GetFileSize(std::string filename);
|
size_t GetFileSize(std::string filename);
|
||||||
|
|
||||||
// Sets the relative executable path, i.e. the path to the executable relative
|
// Sets the executable path, i.e. the path to the executable that is being used
|
||||||
// to the working directory (the value of argv[0] for the main function on most
|
// when launching it. This is usually the path relative to the working directory
|
||||||
// platforms). By using this function, it becomes possible to fileutils.h to
|
// but can also be an absolute path. The intention with this function is to pass
|
||||||
// find the correct project paths even when the working directory is outside the
|
// the argv[0] being sent into the main function to make it possible for
|
||||||
// project tree when running programs linked with the test_support_main target.
|
// fileutils.h to find the correct project paths even when the working directory
|
||||||
void SetRelativeExecutablePath(const std::string& relative_path_to_executable);
|
// is outside the project tree (which happens in some cases).
|
||||||
|
void SetExecutablePath(const std::string& path_to_executable);
|
||||||
|
|
||||||
} // namespace test
|
} // namespace test
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -111,17 +111,7 @@ TEST_F(FileUtilsTest, OutputPathFromUnchangedWorkingDir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tests with current working directory set to a directory higher up in the
|
// 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
|
// directory tree than the project root dir.
|
||||||
// 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
|
|
||||||
TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
|
TEST_F(FileUtilsTest, OutputPathFromRootWorkingDir) {
|
||||||
ASSERT_EQ(0, chdir(kPathDelimiter));
|
ASSERT_EQ(0, chdir(kPathDelimiter));
|
||||||
ASSERT_EQ("./", webrtc::test::OutputPath());
|
ASSERT_EQ("./", webrtc::test::OutputPath());
|
||||||
@ -151,8 +141,14 @@ TEST_F(FileUtilsTest, ResourcePathReturnsValue) {
|
|||||||
std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
|
std::string resource = webrtc::test::ResourcePath(kTestName, kExtension);
|
||||||
ASSERT_GT(resource.find(kTestName), 0u);
|
ASSERT_GT(resource.find(kTestName), 0u);
|
||||||
ASSERT_GT(resource.find(kExtension), 0u);
|
ASSERT_GT(resource.find(kExtension), 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(FileUtilsTest, ResourcePathFromRootWorkingDir) {
|
||||||
ASSERT_EQ(0, chdir(kPathDelimiter));
|
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) {
|
TEST_F(FileUtilsTest, GetFileSizeExistingFile) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user