1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include <cstdio> 12 13 // File utilities for testing purposes. 14 // 15 // The ProjectRootPath() method is a convenient way of getting an absolute 16 // path to the project source tree root directory. Using this, it is easy to 17 // refer to test resource files in a portable way. 18 // 19 // Notice that even if Windows platforms use backslash as path delimiter, it is 20 // also supported to use slash, so there's no need for #ifdef checks in test 21 // code for setting up the paths to the resource files. 22 // 23 // Example use: 24 // Assume we have the following code being used in a test source file: 25 // const std::string kInputFile = webrtc::test::ProjectRootPath() + 26 // "test/data/voice_engine/audio_long16.wav"; 27 // // Use the kInputFile for the tests... 28 // 29 // Then here's some example outputs for different platforms: 30 // Linux: 31 // * Source tree located in /home/user/webrtc/trunk 32 // * Test project located in /home/user/webrtc/trunk/src/testproject 33 // * Test binary compiled as: 34 // /home/user/webrtc/trunk/out/Debug/testproject_unittests 35 // Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if 36 // the test binary is executed from standing in either of: 37 // /home/user/webrtc/trunk 38 // or 39 // /home/user/webrtc/trunk/out/Debug 40 // (or any other directory below the trunk for that matter). 41 // 42 // Windows: 43 // * Source tree located in C:\Users\user\webrtc\trunk 44 // * Test project located in C:\Users\user\webrtc\trunk\src\testproject 45 // * Test binary compiled as: 46 // C:\Users\user\webrtc\trunk\src\testproject\Debug\testproject_unittests.exe 47 // Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the 48 // test binary is executed from inside Visual Studio. 49 // It will also return the same path if the test is executed from a command 50 // prompt standing in C:\Users\user\webrtc\trunk\src\testproject\Debug 51 // 52 // Mac: 53 // * Source tree located in /Users/user/webrtc/trunk 54 // * Test project located in /Users/user/webrtc/trunk/src/testproject 55 // * Test binary compiled as: 56 // /Users/user/webrtc/trunk/xcodebuild/Debug/testproject_unittests 57 // Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if 58 // the test binary is executed from standing in either of: 59 // /Users/user/webrtc/trunk 60 // or 61 // /Users/user/webrtc/trunk/out/Debug 62 // (or any other directory below the trunk for that matter). 63 64 #ifndef WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_ 65 #define WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_ 66 67 #include <string> 68 69 namespace webrtc { 70 namespace test { 71 72 // This is the "directory" returned if the ProjectPath() function fails 73 // to find the project root. 74 extern const char* kCannotFindProjectRootDir; 75 76 // Finds the root dir of the project, to be able to set correct paths to 77 // resource files used by tests. 78 // The implementation is simple: it just looks for the file defined by 79 // kProjectRootFileName, starting in the current directory (the working 80 // directory) and then steps upward until it is found (or it is at the root of 81 // the file system). 82 // If the current working directory is above the project root dir, it will not 83 // be found. 84 // 85 // If symbolic links occur in the path they will be resolved and the actual 86 // directory will be returned. 87 // 88 // Returns the absolute path to the project root dir (usually the trunk dir) 89 // WITH a trailing path delimiter. 90 // If the project root is not found, the string specified by 91 // kCannotFindProjectRootDir is returned. 92 std::string ProjectRootPath(); 93 94 // Creates and returns the absolute path to the output directory where log files 95 // and other test artifacts should be put. The output directory is always a 96 // directory named "out" at the top-level of the project, i.e. a subfolder to 97 // the path returned by ProjectRootPath(). 98 // 99 // Details described for ProjectRootPath() apply here too. 100 // 101 // Returns the path WITH a trailing path delimiter. If the project root is not 102 // found, the current working directory ("./") is returned as a fallback. 103 std::string OutputPath(); 104 105 // Returns a path to a resource file for the currently executing platform. 106 // Adapts to what filenames are currently present in the 107 // [project-root]/resources/ dir. 108 // Returns an absolute path according to this priority list (the directory 109 // part of the path is left out for readability): 110 // 1. [name]_[platform]_[architecture].[extension] 111 // 2. [name]_[platform].[extension] 112 // 3. [name]_[architecture].[extension] 113 // 4. [name].[extension] 114 // Where 115 // * platform is either of "win", "mac" or "linux". 116 // * architecture is either of "32" or "64". 117 // 118 // Arguments: 119 // name - Name of the resource file. If a plain filename (no directory path) 120 // is supplied, the file is assumed to be located in resources/ 121 // If a directory path is prepended to the filename, a subdirectory 122 // hierarchy reflecting that path is assumed to be present. 123 // extension - File extension, without the dot, i.e. "bmp" or "yuv". 124 std::string ResourcePath(std::string name, std::string extension); 125 126 // Gets the current working directory for the executing program. 127 // Returns "./" if for some reason it is not possible to find the working 128 // directory. 129 std::string WorkingDir(); 130 131 // Creates a directory if it not already exists. 132 // Returns true if successful. Will print an error message to stderr and return 133 // false if a file with the same name already exists. 134 bool CreateDirectory(std::string directory_name); 135 136 // File size of the supplied file in bytes. Will return 0 if the file is 137 // empty or if the file does not exist/is readable. 138 size_t GetFileSize(std::string filename); 139 140 } // namespace test 141 } // namespace webrtc 142 143 #endif // WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_ 144