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 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 11 | #include <cstdio> |
| 12 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 13 | // File utilities for testing purposes. |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 14 | // |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 15 | // The ProjectRootPath() method is a convenient way of getting an absolute |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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: |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 25 | // const std::string kInputFile = webrtc::test::ProjectRootPath() + |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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 |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 35 | // Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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 |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 47 | // Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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 |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 57 | // Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 64 | #ifndef WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_ |
| 65 | #define WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_ |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 66 | |
| 67 | #include <string> |
| 68 | |
| 69 | namespace webrtc { |
| 70 | namespace test { |
| 71 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 72 | // This is the "directory" returned if the ProjectPath() function fails |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 73 | // to find the project root. |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 74 | extern const char* kCannotFindProjectRootDir; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 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. |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 92 | std::string ProjectRootPath(); |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 93 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 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 |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 97 | // the path returned by ProjectRootPath(). |
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 | // Details described for ProjectRootPath() apply here too. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 100 | // |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 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(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 104 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 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 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 140 | } // namespace test |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 141 | } // namespace webrtc |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 142 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 143 | #endif // WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_ |