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 #include <stdio.h> 12 13 #ifndef TEST_TESTSUPPORT_FILE_UTILS_H_ 14 #define TEST_TESTSUPPORT_FILE_UTILS_H_ 15 16 #include <string> 17 #include <vector> 18 19 #include "absl/base/attributes.h" 20 #include "absl/strings/string_view.h" 21 #include "absl/types/optional.h" 22 23 namespace webrtc { 24 namespace test { 25 26 // Slash or backslash, depending on platform. 27 ABSL_CONST_INIT extern const absl::string_view kPathDelimiter; 28 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. 37 // 38 // If symbolic links occur in the path they will be resolved and the actual 39 // directory will be returned. 40 // 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(); 44 45 // Generates an empty file with a unique name in the specified directory and 46 // returns the file name and path. 47 // TODO(titovartem) rename to TempFile and next method to TempFilename 48 std::string TempFilename(absl::string_view dir, absl::string_view prefix); 49 50 // Generates a unique file name that can be used for file creation. Doesn't 51 // create any files. 52 std::string GenerateTempFilename(absl::string_view dir, 53 absl::string_view prefix); 54 55 // Returns a path to a resource file in [project-root]/resources/ dir. 56 // Returns an absolute path 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". 64 std::string ResourcePath(absl::string_view name, absl::string_view extension); 65 66 // Joins directory name and file name, separated by the path delimiter. 67 std::string JoinFilename(absl::string_view dir, absl::string_view name); 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 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 76 // a path created by prepending `dir` to the file/directory name. "." and ".." 77 // are never added in the returned vector. 78 absl::optional<std::vector<std::string>> ReadDirectory(absl::string_view path); 79 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. 83 bool CreateDir(absl::string_view directory_name); 84 85 // Removes a directory, which must already be empty. 86 bool RemoveDir(absl::string_view directory_name); 87 88 // Removes a file. 89 bool RemoveFile(absl::string_view file_name); 90 91 // Checks if a file exists. 92 // TOOD(alito): Merge these once absl::string_view adoption is complete for this 93 // file. 94 bool FileExists(absl::string_view file_name); 95 96 // Checks if a directory exists. 97 bool DirExists(absl::string_view directory_name); 98 99 // Strips the rightmost path segment from a path. 100 std::string DirName(absl::string_view path); 101 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. 104 size_t GetFileSize(absl::string_view filename); 105 106 } // namespace test 107 } // namespace webrtc 108 109 #endif // TEST_TESTSUPPORT_FILE_UTILS_H_ 110