
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
199 lines
5.4 KiB
C++
199 lines
5.4 KiB
C++
/*
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "test/testsupport/fileutils.h"
|
|
|
|
#ifdef WIN32
|
|
#include <direct.h>
|
|
#define GET_CURRENT_DIR _getcwd
|
|
#else
|
|
#include <unistd.h>
|
|
#define GET_CURRENT_DIR getcwd
|
|
#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>
|
|
#include <cstring>
|
|
|
|
#include "typedefs.h" // For architecture defines
|
|
|
|
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";
|
|
static const char* kFallbackPath = "./";
|
|
#ifdef WEBRTC_ANDROID
|
|
static const char* kResourcesDirName = "/sdcard/";
|
|
#else
|
|
static const char* kResourcesDirName = "resources";
|
|
#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 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.
|
|
int path_delimiter_index = path.find_last_of(kPathDelimiter);
|
|
while (path_delimiter_index > -1) {
|
|
std::string root_filename = path + kPathDelimiter + kProjectRootFileName;
|
|
if (FileExists(root_filename)) {
|
|
return path + kPathDelimiter;
|
|
}
|
|
// Move up one directory in the directory tree.
|
|
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");
|
|
return kCannotFindProjectRootDir;
|
|
}
|
|
|
|
#ifdef WEBRTC_ANDROID
|
|
|
|
std::string OutputPath() {
|
|
// We need to touch this variable so it doesn't get flagged as unused.
|
|
(void)kOutputDirName;
|
|
return "/sdcard/";
|
|
}
|
|
|
|
#else // WEBRTC_ANDROID
|
|
|
|
std::string OutputPath() {
|
|
std::string path = ProjectRootPath();
|
|
if (path == kCannotFindProjectRootDir) {
|
|
return kFallbackPath;
|
|
}
|
|
path += kOutputDirName;
|
|
if (!CreateDirectory(path)) {
|
|
return kFallbackPath;
|
|
}
|
|
return path + kPathDelimiter;
|
|
}
|
|
|
|
#endif // !WEBRTC_ANDROID
|
|
|
|
std::string WorkingDir() {
|
|
char path_buffer[FILENAME_MAX];
|
|
if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
|
|
fprintf(stderr, "Cannot get current directory!\n");
|
|
return kFallbackPath;
|
|
} else {
|
|
return std::string(path_buffer);
|
|
}
|
|
}
|
|
|
|
bool CreateDirectory(std::string directory_name) {
|
|
struct stat path_info = {0};
|
|
// Check if the path exists already:
|
|
if (stat(directory_name.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 directory.\n",
|
|
directory_name.c_str());
|
|
return false;
|
|
}
|
|
} else {
|
|
#ifdef WIN32
|
|
return _mkdir(directory_name.c_str()) == 0;
|
|
#else
|
|
return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
|
|
#endif
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string ResourcePath(std::string name, std::string extension) {
|
|
std::string platform = "win";
|
|
#ifdef WEBRTC_LINUX
|
|
platform = "linux";
|
|
#endif // WEBRTC_LINUX
|
|
#ifdef WEBRTC_MAC
|
|
platform = "mac";
|
|
#endif // WEBRTC_MAC
|
|
|
|
#ifdef WEBRTC_ARCH_64_BITS
|
|
std::string architecture = "64";
|
|
#else
|
|
std::string architecture = "32";
|
|
#endif // WEBRTC_ARCH_64_BITS
|
|
|
|
#ifdef WEBRTC_ANDROID
|
|
std::string resources_path = kResourcesDirName;
|
|
#else
|
|
std::string resources_path = ProjectRootPath() + kResourcesDirName +
|
|
kPathDelimiter;
|
|
std::string resource_file = resources_path + name + "_" + platform + "_" +
|
|
architecture + "." + extension;
|
|
if (FileExists(resource_file)) {
|
|
return resource_file;
|
|
}
|
|
// Try without architecture.
|
|
resource_file = resources_path + name + "_" + platform + "." + extension;
|
|
if (FileExists(resource_file)) {
|
|
return resource_file;
|
|
}
|
|
// Try without platform.
|
|
resource_file = resources_path + name + "_" + architecture + "." + extension;
|
|
if (FileExists(resource_file)) {
|
|
return resource_file;
|
|
}
|
|
#endif
|
|
// Fall back on name without architecture or platform.
|
|
return resources_path + name + "." + extension;
|
|
}
|
|
|
|
size_t GetFileSize(std::string filename) {
|
|
FILE* f = fopen(filename.c_str(), "rb");
|
|
size_t size = 0;
|
|
if (f != NULL) {
|
|
if (fseek(f, 0, SEEK_END) == 0) {
|
|
size = ftell(f);
|
|
}
|
|
fclose(f);
|
|
}
|
|
return size;
|
|
}
|
|
|
|
} // namespace test
|
|
} // namespace webrtc
|