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