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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/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 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 28 | // Creates and returns the absolute path to the output directory where log files |
phoglund@webrtc.org | 4aa57b4 | 2012-03-22 12:56:54 +0000 | [diff] [blame] | 29 | // and other test artifacts should be put. The output directory is generally a |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 30 | // directory named "out" at the top-level of the project, i.e. a subfolder to |
phoglund@webrtc.org | 4aa57b4 | 2012-03-22 12:56:54 +0000 | [diff] [blame] | 31 | // the path returned by ProjectRootPath(). The exception is Android where we use |
| 32 | // /sdcard/ instead. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 33 | // |
ehmaldonado | d661e9c | 2016-11-22 10:42:59 -0800 | [diff] [blame] | 34 | // If symbolic links occur in the path they will be resolved and the actual |
| 35 | // directory will be returned. |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 36 | // |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 37 | // Returns the path WITH a trailing path delimiter. If the project root is not |
| 38 | // found, the current working directory ("./") is returned as a fallback. |
| 39 | std::string OutputPath(); |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 40 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 41 | // Generates an empty file with a unique name in the specified directory and |
| 42 | // returns the file name and path. |
| 43 | std::string TempFilename(const std::string &dir, const std::string &prefix); |
| 44 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 45 | // Returns a path to a resource file for the currently executing platform. |
| 46 | // Adapts to what filenames are currently present in the |
| 47 | // [project-root]/resources/ dir. |
| 48 | // Returns an absolute path according to this priority list (the directory |
| 49 | // part of the path is left out for readability): |
| 50 | // 1. [name]_[platform]_[architecture].[extension] |
| 51 | // 2. [name]_[platform].[extension] |
| 52 | // 3. [name]_[architecture].[extension] |
| 53 | // 4. [name].[extension] |
| 54 | // Where |
| 55 | // * platform is either of "win", "mac" or "linux". |
| 56 | // * architecture is either of "32" or "64". |
| 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". |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 64 | std::string ResourcePath(const std::string& name, |
| 65 | const std::string& extension); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 66 | |
| 67 | // Gets the current working directory for the executing program. |
| 68 | // Returns "./" if for some reason it is not possible to find the working |
| 69 | // directory. |
| 70 | std::string WorkingDir(); |
| 71 | |
alessiob | 00b16f4 | 2017-06-01 03:29:40 -0700 | [diff] [blame] | 72 | // Reads the content of a directory and, in case of success, returns a vector |
| 73 | // of strings with one element for each found file or directory. Each element is |
| 74 | // a path created by prepending |dir| to the file/directory name. "." and ".." |
| 75 | // are never added in the returned vector. |
| 76 | rtc::Optional<std::vector<std::string>> ReadDirectory(std::string path); |
| 77 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 78 | // Creates a directory if it not already exists. |
| 79 | // Returns true if successful. Will print an error message to stderr and return |
| 80 | // false if a file with the same name already exists. |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 81 | bool CreateDir(const std::string& directory_name); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 82 | |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 83 | // Removes a directory, which must already be empty. |
| 84 | bool RemoveDir(const std::string& directory_name); |
| 85 | |
| 86 | // Removes a file. |
| 87 | bool RemoveFile(const std::string& file_name); |
| 88 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 89 | // Checks if a file exists. |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 90 | bool FileExists(const std::string& file_name); |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 91 | |
alessiob | e49fede | 2017-03-15 06:04:59 -0700 | [diff] [blame] | 92 | // Checks if a directory exists. |
| 93 | bool DirExists(const std::string& directory_name); |
| 94 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 95 | // File size of the supplied file in bytes. Will return 0 if the file is |
| 96 | // empty or if the file does not exist/is readable. |
ehmaldonado | 88df0bc | 2017-02-10 09:27:14 -0800 | [diff] [blame] | 97 | size_t GetFileSize(const std::string& filename); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 98 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 99 | // Sets the executable path, i.e. the path to the executable that is being used |
| 100 | // when launching it. This is usually the path relative to the working directory |
| 101 | // but can also be an absolute path. The intention with this function is to pass |
| 102 | // the argv[0] being sent into the main function to make it possible for |
| 103 | // fileutils.h to find the correct project paths even when the working directory |
| 104 | // is outside the project tree (which happens in some cases). |
| 105 | void SetExecutablePath(const std::string& path_to_executable); |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 106 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 107 | } // namespace test |
andrew@webrtc.org | 1e10bb3 | 2011-10-31 20:22:02 +0000 | [diff] [blame] | 108 | } // namespace webrtc |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 109 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 110 | #endif // TEST_TESTSUPPORT_FILEUTILS_H_ |