blob: cd43c692c553f9c6b3168204a4c3f3f1aaaaa280 [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
11// File utilities for testing purposes.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000012//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000013// The ProjectRootPath() method is a convenient way of getting an absolute
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000014// path to the project source tree root directory. Using this, it is easy to
15// refer to test resource files in a portable way.
16//
17// Notice that even if Windows platforms use backslash as path delimiter, it is
18// also supported to use slash, so there's no need for #ifdef checks in test
19// code for setting up the paths to the resource files.
20//
21// Example use:
22// Assume we have the following code being used in a test source file:
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000023// const std::string kInputFile = webrtc::test::ProjectRootPath() +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000024// "test/data/voice_engine/audio_long16.wav";
25// // Use the kInputFile for the tests...
26//
27// Then here's some example outputs for different platforms:
28// Linux:
29// * Source tree located in /home/user/webrtc/trunk
30// * Test project located in /home/user/webrtc/trunk/src/testproject
31// * Test binary compiled as:
32// /home/user/webrtc/trunk/out/Debug/testproject_unittests
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000033// Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000034// the test binary is executed from standing in either of:
35// /home/user/webrtc/trunk
36// or
37// /home/user/webrtc/trunk/out/Debug
38// (or any other directory below the trunk for that matter).
39//
40// Windows:
41// * Source tree located in C:\Users\user\webrtc\trunk
42// * Test project located in C:\Users\user\webrtc\trunk\src\testproject
43// * Test binary compiled as:
44// C:\Users\user\webrtc\trunk\src\testproject\Debug\testproject_unittests.exe
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000045// Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000046// test binary is executed from inside Visual Studio.
47// It will also return the same path if the test is executed from a command
48// prompt standing in C:\Users\user\webrtc\trunk\src\testproject\Debug
49//
50// Mac:
51// * Source tree located in /Users/user/webrtc/trunk
52// * Test project located in /Users/user/webrtc/trunk/src/testproject
53// * Test binary compiled as:
54// /Users/user/webrtc/trunk/xcodebuild/Debug/testproject_unittests
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000055// Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000056// the test binary is executed from standing in either of:
57// /Users/user/webrtc/trunk
58// or
59// /Users/user/webrtc/trunk/out/Debug
60// (or any other directory below the trunk for that matter).
61
62#ifndef TEST_TESTSUPPORT_FILEUTILS_H_
63#define TEST_TESTSUPPORT_FILEUTILS_H_
64
65#include <string>
66
67namespace webrtc {
68namespace test {
69
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000070// This is the "directory" returned if the ProjectPath() function fails
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000071// to find the project root.
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000072extern const char* kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000073
74// Finds the root dir of the project, to be able to set correct paths to
75// resource files used by tests.
76// The implementation is simple: it just looks for the file defined by
77// kProjectRootFileName, starting in the current directory (the working
78// directory) and then steps upward until it is found (or it is at the root of
79// the file system).
80// If the current working directory is above the project root dir, it will not
81// be found.
82//
83// If symbolic links occur in the path they will be resolved and the actual
84// directory will be returned.
85//
86// Returns the absolute path to the project root dir (usually the trunk dir)
87// WITH a trailing path delimiter.
88// If the project root is not found, the string specified by
89// kCannotFindProjectRootDir is returned.
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000090std::string ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000091
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000092// Creates and returns the absolute path to the output directory where log files
93// and other test artifacts should be put. The output directory is always a
94// directory named "out" at the top-level of the project, i.e. a subfolder to
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000095// the path returned by ProjectRootPath().
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000096//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000097// Details described for ProjectRootPath() apply here too.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000098//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000099// Returns the path WITH a trailing path delimiter. If the project root is not
100// found, the current working directory ("./") is returned as a fallback.
101std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000102
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000103// Returns a path to a resource file for the currently executing platform.
104// Adapts to what filenames are currently present in the
105// [project-root]/resources/ dir.
106// Returns an absolute path according to this priority list (the directory
107// part of the path is left out for readability):
108// 1. [name]_[platform]_[architecture].[extension]
109// 2. [name]_[platform].[extension]
110// 3. [name]_[architecture].[extension]
111// 4. [name].[extension]
112// Where
113// * platform is either of "win", "mac" or "linux".
114// * architecture is either of "32" or "64".
115//
116// Arguments:
117// name - Name of the resource file. If a plain filename (no directory path)
118// is supplied, the file is assumed to be located in resources/
119// If a directory path is prepended to the filename, a subdirectory
120// hierarchy reflecting that path is assumed to be present.
121// extension - File extension, without the dot, i.e. "bmp" or "yuv".
122std::string ResourcePath(std::string name, std::string extension);
123
124// Gets the current working directory for the executing program.
125// Returns "./" if for some reason it is not possible to find the working
126// directory.
127std::string WorkingDir();
128
129// Creates a directory if it not already exists.
130// Returns true if successful. Will print an error message to stderr and return
131// false if a file with the same name already exists.
132bool CreateDirectory(std::string directory_name);
133
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000134} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +0000135} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000136
137#endif // TEST_TESTSUPPORT_FILEUTILS_H_