diff --git a/src/test/test_suite.cc b/src/test/test_suite.cc index 296dd5837..81913fd7a 100644 --- a/src/test/test_suite.cc +++ b/src/test/test_suite.cc @@ -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. } diff --git a/src/test/testsupport/fileutils.cc b/src/test/testsupport/fileutils.cc index ebb64e21b..399d8a623 100644 --- a/src/test/testsupport/fileutils.cc +++ b/src/test/testsupport/fileutils.cc @@ -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; diff --git a/src/test/testsupport/fileutils.h b/src/test/testsupport/fileutils.h index 4fbc7abc2..e642a5f03 100644 --- a/src/test/testsupport/fileutils.h +++ b/src/test/testsupport/fileutils.h @@ -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 diff --git a/src/test/testsupport/fileutils_unittest.cc b/src/test/testsupport/fileutils_unittest.cc index 795ad5bfd..940b07042 100644 --- a/src/test/testsupport/fileutils_unittest.cc +++ b/src/test/testsupport/fileutils_unittest.cc @@ -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) {