kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [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 | [diff] [blame] | 11 | #include <stdio.h> |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 | [diff] [blame] | 12 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 13 | #ifndef TEST_TESTSUPPORT_FILE_UTILS_H_ |
| 14 | #define TEST_TESTSUPPORT_FILE_UTILS_H_ |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [diff] [blame] | 15 | |
| 16 | #include <string> |
alessiob | 00b16f4 | 2017-06-01 10:29:40 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Danil Chapovalov | 431abd9 | 2018-06-18 10:54:17 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 | [diff] [blame] | 24 | // This is the "directory" returned if the ProjectPath() function fails |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [diff] [blame] | 25 | // to find the project root. |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 | [diff] [blame] | 26 | extern const char* kCannotFindProjectRootDir; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [diff] [blame] | 27 | |
Niels Möller | 7b3c76b | 2018-11-07 08:54:28 | [diff] [blame] | 28 | // Slash or backslash, depending on platform. NUL-terminated string. |
| 29 | extern const char* kPathDelimiter; |
| 30 | |
Patrik Höglund | 8434aeb3 | 2018-10-05 12:52:11 | [diff] [blame] | 31 | // Returns the absolute path to the output directory where log files and other |
| 32 | // test artifacts should be put. The output directory is generally a directory |
| 33 | // named "out" at the project root. This root is assumed to be two levels above |
| 34 | // where the test binary is located; this is because tests execute in a dir |
| 35 | // out/Whatever relative to the project root. This convention is also followed |
| 36 | // in Chromium. |
| 37 | // |
| 38 | // The exception is Android where we use /sdcard/ instead. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 | [diff] [blame] | 39 | // |
ehmaldonado | d661e9c | 2016-11-22 18:42:59 | [diff] [blame] | 40 | // If symbolic links occur in the path they will be resolved and the actual |
| 41 | // directory will be returned. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 | [diff] [blame] | 42 | // |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 | [diff] [blame] | 43 | // Returns the path WITH a trailing path delimiter. If the project root is not |
| 44 | // found, the current working directory ("./") is returned as a fallback. |
| 45 | std::string OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 | [diff] [blame] | 46 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 | [diff] [blame] | 47 | // Generates an empty file with a unique name in the specified directory and |
| 48 | // returns the file name and path. |
Artem Titov | e62f600 | 2018-03-19 14:40:00 | [diff] [blame] | 49 | // TODO(titovartem) rename to TempFile and next method to TempFilename |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 50 | std::string TempFilename(const std::string& dir, const std::string& prefix); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 | [diff] [blame] | 51 | |
Artem Titov | e62f600 | 2018-03-19 14:40:00 | [diff] [blame] | 52 | // Generates a unique file name that can be used for file creation. Doesn't |
| 53 | // create any files. |
| 54 | std::string GenerateTempFilename(const std::string& dir, |
| 55 | const std::string& prefix); |
| 56 | |
Patrik Höglund | 7730193 | 2018-10-09 13:09:51 | [diff] [blame] | 57 | // Returns a path to a resource file in [project-root]/resources/ dir. |
| 58 | // Returns an absolute path |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 | [diff] [blame] | 59 | // |
| 60 | // Arguments: |
| 61 | // name - Name of the resource file. If a plain filename (no directory path) |
| 62 | // is supplied, the file is assumed to be located in resources/ |
| 63 | // If a directory path is prepended to the filename, a subdirectory |
| 64 | // hierarchy reflecting that path is assumed to be present. |
| 65 | // extension - File extension, without the dot, i.e. "bmp" or "yuv". |
Niels Möller | 392f8d0 | 2018-06-01 08:14:44 | [diff] [blame] | 66 | std::string ResourcePath(const std::string& name, const std::string& extension); |
| 67 | |
| 68 | // Joins directory name and file name, separated by the path delimiter. |
| 69 | std::string JoinFilename(const std::string& dir, const std::string& name); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 | [diff] [blame] | 70 | |
| 71 | // Gets the current working directory for the executing program. |
| 72 | // Returns "./" if for some reason it is not possible to find the working |
| 73 | // directory. |
| 74 | std::string WorkingDir(); |
| 75 | |
alessiob | 00b16f4 | 2017-06-01 10:29:40 | [diff] [blame] | 76 | // Reads the content of a directory and, in case of success, returns a vector |
| 77 | // of strings with one element for each found file or directory. Each element is |
| 78 | // a path created by prepending |dir| to the file/directory name. "." and ".." |
| 79 | // are never added in the returned vector. |
Danil Chapovalov | 431abd9 | 2018-06-18 10:54:17 | [diff] [blame] | 80 | absl::optional<std::vector<std::string>> ReadDirectory(std::string path); |
alessiob | 00b16f4 | 2017-06-01 10:29:40 | [diff] [blame] | 81 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 | [diff] [blame] | 82 | // Creates a directory if it not already exists. |
| 83 | // Returns true if successful. Will print an error message to stderr and return |
| 84 | // false if a file with the same name already exists. |
ehmaldonado | 88df0bc | 2017-02-10 17:27:14 | [diff] [blame] | 85 | bool CreateDir(const std::string& directory_name); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 | [diff] [blame] | 86 | |
nisse | 57efb03 | 2017-05-18 10:55:59 | [diff] [blame] | 87 | // Removes a directory, which must already be empty. |
| 88 | bool RemoveDir(const std::string& directory_name); |
| 89 | |
| 90 | // Removes a file. |
| 91 | bool RemoveFile(const std::string& file_name); |
| 92 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 | [diff] [blame] | 93 | // Checks if a file exists. |
ehmaldonado | 88df0bc | 2017-02-10 17:27:14 | [diff] [blame] | 94 | bool FileExists(const std::string& file_name); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 | [diff] [blame] | 95 | |
alessiob | e49fede | 2017-03-15 13:04:59 | [diff] [blame] | 96 | // Checks if a directory exists. |
| 97 | bool DirExists(const std::string& directory_name); |
| 98 | |
Patrik Höglund | 8434aeb3 | 2018-10-05 12:52:11 | [diff] [blame] | 99 | // Strips the rightmost path segment from a path. |
| 100 | std::string DirName(const std::string& path); |
| 101 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 | [diff] [blame] | 102 | // File size of the supplied file in bytes. Will return 0 if the file is |
| 103 | // empty or if the file does not exist/is readable. |
ehmaldonado | 88df0bc | 2017-02-10 17:27:14 | [diff] [blame] | 104 | size_t GetFileSize(const std::string& filename); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 | [diff] [blame] | 105 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [diff] [blame] | 106 | } // namespace test |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 | [diff] [blame] | 107 | } // namespace webrtc |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 | [diff] [blame] | 108 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 109 | #endif // TEST_TESTSUPPORT_FILE_UTILS_H_ |