blob: ab80ca4454817ea39c998c3cf0ef5d03243c9330 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
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:53 +000011#include <stdio.h>
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
Steve Anton10542f22019-01-11 09:11:00 -080013#ifndef TEST_TESTSUPPORT_FILE_UTILS_H_
14#define TEST_TESTSUPPORT_FILE_UTILS_H_
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000015
16#include <string>
alessiob00b16f42017-06-01 03:29:40 -070017#include <vector>
18
Ali Tofigh1d6de142022-03-24 19:34:34 +010019#include "absl/base/attributes.h"
20#include "absl/strings/string_view.h"
Danil Chapovalov431abd92018-06-18 12:54:17 +020021#include "absl/types/optional.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000022
23namespace webrtc {
24namespace test {
25
Ali Tofigh1d6de142022-03-24 19:34:34 +010026// Slash or backslash, depending on platform.
27ABSL_CONST_INIT extern const absl::string_view kPathDelimiter;
Niels Möller7b3c76b2018-11-07 09:54:28 +010028
Patrik Höglund8434aeb2018-10-05 14:52:11 +020029// 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.org4d8cd9d2011-11-09 11:24:14 +000037//
ehmaldonadod661e9c2016-11-22 10:42:59 -080038// If symbolic links occur in the path they will be resolved and the actual
39// directory will be returned.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000040//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000041// 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.
43std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000044
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000045// Generates an empty file with a unique name in the specified directory and
46// returns the file name and path.
Artem Titove62f6002018-03-19 15:40:00 +010047// TODO(titovartem) rename to TempFile and next method to TempFilename
Ali Tofigh1d6de142022-03-24 19:34:34 +010048std::string TempFilename(absl::string_view dir, absl::string_view prefix);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000049
Artem Titove62f6002018-03-19 15:40:00 +010050// Generates a unique file name that can be used for file creation. Doesn't
51// create any files.
Ali Tofigh1d6de142022-03-24 19:34:34 +010052std::string GenerateTempFilename(absl::string_view dir,
53 absl::string_view prefix);
Artem Titove62f6002018-03-19 15:40:00 +010054
Patrik Höglund77301932018-10-09 15:09:51 +020055// Returns a path to a resource file in [project-root]/resources/ dir.
56// Returns an absolute path
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000057//
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 Tofigh1d6de142022-03-24 19:34:34 +010064std::string ResourcePath(absl::string_view name, absl::string_view extension);
Niels Möller392f8d02018-06-01 10:14:44 +020065
66// Joins directory name and file name, separated by the path delimiter.
Ali Tofigh1d6de142022-03-24 19:34:34 +010067std::string JoinFilename(absl::string_view dir, absl::string_view name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000068
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.
72std::string WorkingDir();
73
alessiob00b16f42017-06-01 03:29:40 -070074// 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 Titov1ee563d2021-07-27 12:46:29 +020076// a path created by prepending `dir` to the file/directory name. "." and ".."
alessiob00b16f42017-06-01 03:29:40 -070077// are never added in the returned vector.
Ali Tofigh1d6de142022-03-24 19:34:34 +010078absl::optional<std::vector<std::string>> ReadDirectory(absl::string_view path);
alessiob00b16f42017-06-01 03:29:40 -070079
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000080// 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 Tofigh1d6de142022-03-24 19:34:34 +010083bool CreateDir(absl::string_view directory_name);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000084
nisse57efb032017-05-18 03:55:59 -070085// Removes a directory, which must already be empty.
Ali Tofigh1d6de142022-03-24 19:34:34 +010086bool RemoveDir(absl::string_view directory_name);
nisse57efb032017-05-18 03:55:59 -070087
88// Removes a file.
Ali Tofigh1d6de142022-03-24 19:34:34 +010089bool RemoveFile(absl::string_view file_name);
nisse57efb032017-05-18 03:55:59 -070090
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000091// Checks if a file exists.
Ali Tofigh1d6de142022-03-24 19:34:34 +010092// TOOD(alito): Merge these once absl::string_view adoption is complete for this
93// file.
94bool FileExists(absl::string_view file_name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000095
alessiobe49fede2017-03-15 06:04:59 -070096// Checks if a directory exists.
Ali Tofigh1d6de142022-03-24 19:34:34 +010097bool DirExists(absl::string_view directory_name);
alessiobe49fede2017-03-15 06:04:59 -070098
Patrik Höglund8434aeb2018-10-05 14:52:11 +020099// Strips the rightmost path segment from a path.
Ali Tofigh1d6de142022-03-24 19:34:34 +0100100std::string DirName(absl::string_view path);
Patrik Höglund8434aeb2018-10-05 14:52:11 +0200101
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000102// 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 Tofigh1d6de142022-03-24 19:34:34 +0100104size_t GetFileSize(absl::string_view filename);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000105
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000106} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +0000107} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000108
Steve Anton10542f22019-01-11 09:11:00 -0800109#endif // TEST_TESTSUPPORT_FILE_UTILS_H_