blob: cdf3a3157b079086d7daf76068670d3f92ed11da [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:411/*
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.org12dc1a32013-08-05 16:22:5311#include <stdio.h>
kjellander@webrtc.org5b97b122011-12-08 07:42:1812
Steve Anton10542f22019-01-11 17:11:0013#ifndef TEST_TESTSUPPORT_FILE_UTILS_H_
14#define TEST_TESTSUPPORT_FILE_UTILS_H_
kjellander@webrtc.org7951e812011-10-13 12:24:4115
16#include <string>
alessiob00b16f42017-06-01 10:29:4017#include <vector>
18
Danil Chapovalov431abd92018-06-18 10:54:1719#include "absl/types/optional.h"
kjellander@webrtc.org7951e812011-10-13 12:24:4120
21namespace webrtc {
22namespace test {
23
andrew@webrtc.org0db7dc62011-11-13 01:34:0524// This is the "directory" returned if the ProjectPath() function fails
kjellander@webrtc.org7951e812011-10-13 12:24:4125// to find the project root.
andrew@webrtc.org1e10bb32011-10-31 20:22:0226extern const char* kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:4127
Niels Möller7b3c76b2018-11-07 08:54:2828// Slash or backslash, depending on platform. NUL-terminated string.
29extern const char* kPathDelimiter;
30
Patrik Höglund8434aeb32018-10-05 12:52:1131// 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.org4d8cd9d2011-11-09 11:24:1439//
ehmaldonadod661e9c2016-11-22 18:42:5940// If symbolic links occur in the path they will be resolved and the actual
41// directory will be returned.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:1442//
andrew@webrtc.org0db7dc62011-11-13 01:34:0543// 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.
45std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:1446
kjellander@webrtc.org7de47bc2014-04-16 08:04:2647// Generates an empty file with a unique name in the specified directory and
48// returns the file name and path.
Artem Titove62f6002018-03-19 14:40:0049// TODO(titovartem) rename to TempFile and next method to TempFilename
Yves Gerey665174f2018-06-19 13:03:0550std::string TempFilename(const std::string& dir, const std::string& prefix);
kjellander@webrtc.org7de47bc2014-04-16 08:04:2651
Artem Titove62f6002018-03-19 14:40:0052// Generates a unique file name that can be used for file creation. Doesn't
53// create any files.
54std::string GenerateTempFilename(const std::string& dir,
55 const std::string& prefix);
56
Patrik Höglund77301932018-10-09 13:09:5157// Returns a path to a resource file in [project-root]/resources/ dir.
58// Returns an absolute path
kjellander@webrtc.org4ed4f242011-12-05 16:31:1259//
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öller392f8d02018-06-01 08:14:4466std::string ResourcePath(const std::string& name, const std::string& extension);
67
68// Joins directory name and file name, separated by the path delimiter.
69std::string JoinFilename(const std::string& dir, const std::string& name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:1270
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.
74std::string WorkingDir();
75
alessiob00b16f42017-06-01 10:29:4076// 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 Chapovalov431abd92018-06-18 10:54:1780absl::optional<std::vector<std::string>> ReadDirectory(std::string path);
alessiob00b16f42017-06-01 10:29:4081
kjellander@webrtc.org4ed4f242011-12-05 16:31:1282// 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.
ehmaldonado88df0bc2017-02-10 17:27:1485bool CreateDir(const std::string& directory_name);
kjellander@webrtc.org7de47bc2014-04-16 08:04:2686
nisse57efb032017-05-18 10:55:5987// Removes a directory, which must already be empty.
88bool RemoveDir(const std::string& directory_name);
89
90// Removes a file.
91bool RemoveFile(const std::string& file_name);
92
kjellander@webrtc.org7de47bc2014-04-16 08:04:2693// Checks if a file exists.
ehmaldonado88df0bc2017-02-10 17:27:1494bool FileExists(const std::string& file_name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:1295
alessiobe49fede2017-03-15 13:04:5996// Checks if a directory exists.
97bool DirExists(const std::string& directory_name);
98
Patrik Höglund8434aeb32018-10-05 12:52:1199// Strips the rightmost path segment from a path.
100std::string DirName(const std::string& path);
101
kjellander@webrtc.org5b97b122011-12-08 07:42:18102// 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.
ehmaldonado88df0bc2017-02-10 17:27:14104size_t GetFileSize(const std::string& filename);
kjellander@webrtc.org5b97b122011-12-08 07:42:18105
kjellander@webrtc.org7951e812011-10-13 12:24:41106} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02107} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41108
Steve Anton10542f22019-01-11 17:11:00109#endif // TEST_TESTSUPPORT_FILE_UTILS_H_