blob: d51bbde21e734b8015335a23b06bdd55e165c20d [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
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000013// File utilities for testing purposes.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000014//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000015// The ProjectRootPath() method is a convenient way of getting an absolute
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016// path to the project source tree root directory. Using this, it is easy to
17// refer to test resource files in a portable way.
18//
19// Notice that even if Windows platforms use backslash as path delimiter, it is
20// also supported to use slash, so there's no need for #ifdef checks in test
21// code for setting up the paths to the resource files.
22//
23// Example use:
24// Assume we have the following code being used in a test source file:
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000025// const std::string kInputFile = webrtc::test::ProjectRootPath() +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000026// "test/data/voice_engine/audio_long16.wav";
27// // Use the kInputFile for the tests...
28//
29// Then here's some example outputs for different platforms:
30// Linux:
31// * Source tree located in /home/user/webrtc/trunk
32// * Test project located in /home/user/webrtc/trunk/src/testproject
33// * Test binary compiled as:
34// /home/user/webrtc/trunk/out/Debug/testproject_unittests
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000035// Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000036// the test binary is executed from standing in either of:
37// /home/user/webrtc/trunk
38// or
39// /home/user/webrtc/trunk/out/Debug
40// (or any other directory below the trunk for that matter).
41//
42// Windows:
43// * Source tree located in C:\Users\user\webrtc\trunk
44// * Test project located in C:\Users\user\webrtc\trunk\src\testproject
45// * Test binary compiled as:
46// C:\Users\user\webrtc\trunk\src\testproject\Debug\testproject_unittests.exe
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000047// Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000048// test binary is executed from inside Visual Studio.
49// It will also return the same path if the test is executed from a command
50// prompt standing in C:\Users\user\webrtc\trunk\src\testproject\Debug
51//
52// Mac:
53// * Source tree located in /Users/user/webrtc/trunk
54// * Test project located in /Users/user/webrtc/trunk/src/testproject
55// * Test binary compiled as:
56// /Users/user/webrtc/trunk/xcodebuild/Debug/testproject_unittests
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000057// Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000058// the test binary is executed from standing in either of:
59// /Users/user/webrtc/trunk
60// or
61// /Users/user/webrtc/trunk/out/Debug
62// (or any other directory below the trunk for that matter).
63
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000064#ifndef WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
65#define WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000066
67#include <string>
68
69namespace webrtc {
70namespace test {
71
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000072// This is the "directory" returned if the ProjectPath() function fails
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000073// to find the project root.
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000074extern const char* kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000075
76// Finds the root dir of the project, to be able to set correct paths to
77// resource files used by tests.
78// The implementation is simple: it just looks for the file defined by
79// kProjectRootFileName, starting in the current directory (the working
80// directory) and then steps upward until it is found (or it is at the root of
81// the file system).
82// If the current working directory is above the project root dir, it will not
83// be found.
84//
85// If symbolic links occur in the path they will be resolved and the actual
86// directory will be returned.
87//
88// Returns the absolute path to the project root dir (usually the trunk dir)
89// WITH a trailing path delimiter.
90// If the project root is not found, the string specified by
91// kCannotFindProjectRootDir is returned.
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000092std::string ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000093
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000094// Creates and returns the absolute path to the output directory where log files
phoglund@webrtc.org4aa57b42012-03-22 12:56:54 +000095// and other test artifacts should be put. The output directory is generally a
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000096// directory named "out" at the top-level of the project, i.e. a subfolder to
phoglund@webrtc.org4aa57b42012-03-22 12:56:54 +000097// the path returned by ProjectRootPath(). The exception is Android where we use
98// /sdcard/ instead.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000099//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000100// Details described for ProjectRootPath() apply here too.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000101//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000102// Returns the path WITH a trailing path delimiter. If the project root is not
103// found, the current working directory ("./") is returned as a fallback.
104std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000105
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000106// Returns a path to a resource file for the currently executing platform.
107// Adapts to what filenames are currently present in the
108// [project-root]/resources/ dir.
109// Returns an absolute path according to this priority list (the directory
110// part of the path is left out for readability):
111// 1. [name]_[platform]_[architecture].[extension]
112// 2. [name]_[platform].[extension]
113// 3. [name]_[architecture].[extension]
114// 4. [name].[extension]
115// Where
116// * platform is either of "win", "mac" or "linux".
117// * architecture is either of "32" or "64".
118//
119// Arguments:
120// name - Name of the resource file. If a plain filename (no directory path)
121// is supplied, the file is assumed to be located in resources/
122// If a directory path is prepended to the filename, a subdirectory
123// hierarchy reflecting that path is assumed to be present.
124// extension - File extension, without the dot, i.e. "bmp" or "yuv".
125std::string ResourcePath(std::string name, std::string extension);
126
127// Gets the current working directory for the executing program.
128// Returns "./" if for some reason it is not possible to find the working
129// directory.
130std::string WorkingDir();
131
132// Creates a directory if it not already exists.
133// Returns true if successful. Will print an error message to stderr and return
134// false if a file with the same name already exists.
135bool CreateDirectory(std::string directory_name);
136
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000137// File size of the supplied file in bytes. Will return 0 if the file is
138// empty or if the file does not exist/is readable.
139size_t GetFileSize(std::string filename);
140
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000141// Sets the executable path, i.e. the path to the executable that is being used
142// when launching it. This is usually the path relative to the working directory
143// but can also be an absolute path. The intention with this function is to pass
144// the argv[0] being sent into the main function to make it possible for
145// fileutils.h to find the correct project paths even when the working directory
146// is outside the project tree (which happens in some cases).
147void SetExecutablePath(const std::string& path_to_executable);
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000148
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000149} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +0000150} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000151
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000152#endif // WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_