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