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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #ifndef TEST_TESTSUPPORT_FILEUTILS_H_ |
| 14 | #define TEST_TESTSUPPORT_FILEUTILS_H_ |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 15 | |
| 16 | #include <string> |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 24 | // This is the "directory" returned if the ProjectPath() function fails |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 25 | // to find the project root. |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 26 | extern const char* kCannotFindProjectRootDir; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 27 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 28 | // Returns the absolute path to the output directory where log files and other |
| 29 | // test artifacts should be put. The output directory is generally a directory |
| 30 | // named "out" at the project root. This root is assumed to be two levels above |
| 31 | // where the test binary is located; this is because tests execute in a dir |
| 32 | // out/Whatever relative to the project root. This convention is also followed |
| 33 | // in Chromium. |
| 34 | // |
| 35 | // The exception is Android where we use /sdcard/ instead. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 36 | // |
ehmaldonado | d661e9c | 2016-11-22 10:42:59 -0800 | [diff] [blame] | 37 | // If symbolic links occur in the path they will be resolved and the actual |
| 38 | // directory will be returned. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 39 | // |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 40 | // Returns the path WITH a trailing path delimiter. If the project root is not |
| 41 | // found, the current working directory ("./") is returned as a fallback. |
| 42 | std::string OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 43 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 44 | // Generates an empty file with a unique name in the specified directory and |
| 45 | // returns the file name and path. |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 46 | // TODO(titovartem) rename to TempFile and next method to TempFilename |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | std::string TempFilename(const std::string& dir, const std::string& prefix); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 48 | |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 49 | // Generates a unique file name that can be used for file creation. Doesn't |
| 50 | // create any files. |
| 51 | std::string GenerateTempFilename(const std::string& dir, |
| 52 | const std::string& prefix); |
| 53 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 54 | // Returns a path to a resource file for the currently executing platform. |
| 55 | // Adapts to what filenames are currently present in the |
| 56 | // [project-root]/resources/ dir. |
| 57 | // Returns an absolute path according to this priority list (the directory |
| 58 | // part of the path is left out for readability): |
| 59 | // 1. [name]_[platform]_[architecture].[extension] |
| 60 | // 2. [name]_[platform].[extension] |
| 61 | // 3. [name]_[architecture].[extension] |
| 62 | // 4. [name].[extension] |
| 63 | // Where |
| 64 | // * platform is either of "win", "mac" or "linux". |
| 65 | // * architecture is either of "32" or "64". |
| 66 | // |
| 67 | // Arguments: |
| 68 | // name - Name of the resource file. If a plain filename (no directory path) |
| 69 | // is supplied, the file is assumed to be located in resources/ |
| 70 | // If a directory path is prepended to the filename, a subdirectory |
| 71 | // hierarchy reflecting that path is assumed to be present. |
| 72 | // extension - File extension, without the dot, i.e. "bmp" or "yuv". |
Niels Möller | 392f8d0 | 2018-06-01 10:14:44 +0200 | [diff] [blame] | 73 | std::string ResourcePath(const std::string& name, const std::string& extension); |
| 74 | |
| 75 | // Joins directory name and file name, separated by the path delimiter. |
| 76 | std::string JoinFilename(const std::string& dir, const std::string& name); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 77 | |
| 78 | // Gets the current working directory for the executing program. |
| 79 | // Returns "./" if for some reason it is not possible to find the working |
| 80 | // directory. |
| 81 | std::string WorkingDir(); |
| 82 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 83 | // Reads the content of a directory and, in case of success, returns a vector |
| 84 | // of strings with one element for each found file or directory. Each element is |
| 85 | // a path created by prepending |dir| to the file/directory name. "." and ".." |
| 86 | // are never added in the returned vector. |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 87 | absl::optional<std::vector<std::string>> ReadDirectory(std::string path); |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 88 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 89 | // Creates a directory if it not already exists. |
| 90 | // Returns true if successful. Will print an error message to stderr and return |
| 91 | // false if a file with the same name already exists. |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 92 | bool CreateDir(const std::string& directory_name); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 93 | |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 94 | // Removes a directory, which must already be empty. |
| 95 | bool RemoveDir(const std::string& directory_name); |
| 96 | |
| 97 | // Removes a file. |
| 98 | bool RemoveFile(const std::string& file_name); |
| 99 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 100 | // Checks if a file exists. |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 101 | bool FileExists(const std::string& file_name); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 102 | |
alessiob | e49fede | 2017-03-15 06:04:59 -0700 | [diff] [blame] | 103 | // Checks if a directory exists. |
| 104 | bool DirExists(const std::string& directory_name); |
| 105 | |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 106 | // Strips the rightmost path segment from a path. |
| 107 | std::string DirName(const std::string& path); |
| 108 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 109 | // File size of the supplied file in bytes. Will return 0 if the file is |
| 110 | // empty or if the file does not exist/is readable. |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 111 | size_t GetFileSize(const std::string& filename); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 112 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 113 | // Sets the executable path, i.e. the path to the executable that is being used |
| 114 | // when launching it. This is usually the path relative to the working directory |
| 115 | // but can also be an absolute path. The intention with this function is to pass |
| 116 | // the argv[0] being sent into the main function to make it possible for |
| 117 | // fileutils.h to find the correct project paths even when the working directory |
| 118 | // is outside the project tree (which happens in some cases). |
Patrik Höglund | 8434aeb | 2018-10-05 14:52:11 +0200 | [diff] [blame] | 119 | // TODO(bugs.webrtc.org/9792): Deprecated - going away soon. |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 120 | void SetExecutablePath(const std::string& path_to_executable); |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 121 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 122 | } // namespace test |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 123 | } // namespace webrtc |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 124 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 125 | #endif // TEST_TESTSUPPORT_FILEUTILS_H_ |