blob: a111a7abc6d2d194e5fe504ab886f2044ac15db7 [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.
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000012// The ProjectRootPath() method is a convenient way of getting an absolute
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000013// path to the project source tree root directory. Using this, it is easy to
14// refer to test resource files in a portable way.
15//
16// Notice that even if Windows platforms use backslash as path delimiter, it is
17// also supported to use slash, so there's no need for #ifdef checks in test
18// code for setting up the paths to the resource files.
19//
20// Example use:
21// Assume we have the following code being used in a test source file:
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000022// const std::string kInputFile = webrtc::test::ProjectRootPath() +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000023// "test/data/voice_engine/audio_long16.wav";
24// // Use the kInputFile for the tests...
25//
26// Then here's some example outputs for different platforms:
27// Linux:
28// * Source tree located in /home/user/webrtc/trunk
29// * Test project located in /home/user/webrtc/trunk/src/testproject
30// * Test binary compiled as:
31// /home/user/webrtc/trunk/out/Debug/testproject_unittests
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000032// Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000033// the test binary is executed from standing in either of:
34// /home/user/webrtc/trunk
35// or
36// /home/user/webrtc/trunk/out/Debug
37// (or any other directory below the trunk for that matter).
38//
39// Windows:
40// * Source tree located in C:\Users\user\webrtc\trunk
41// * Test project located in C:\Users\user\webrtc\trunk\src\testproject
42// * Test binary compiled as:
43// C:\Users\user\webrtc\trunk\src\testproject\Debug\testproject_unittests.exe
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000044// Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000045// test binary is executed from inside Visual Studio.
46// It will also return the same path if the test is executed from a command
47// prompt standing in C:\Users\user\webrtc\trunk\src\testproject\Debug
48//
49// Mac:
50// * Source tree located in /Users/user/webrtc/trunk
51// * Test project located in /Users/user/webrtc/trunk/src/testproject
52// * Test binary compiled as:
53// /Users/user/webrtc/trunk/xcodebuild/Debug/testproject_unittests
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000054// Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000055// the test binary is executed from standing in either of:
56// /Users/user/webrtc/trunk
57// or
58// /Users/user/webrtc/trunk/out/Debug
59// (or any other directory below the trunk for that matter).
60
61#ifndef TEST_TESTSUPPORT_FILEUTILS_H_
62#define TEST_TESTSUPPORT_FILEUTILS_H_
63
64#include <string>
65
66namespace webrtc {
67namespace test {
68
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000069// This is the "directory" returned if the ProjectPath() function fails
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000070// to find the project root.
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000071extern const char* kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000072
73// Finds the root dir of the project, to be able to set correct paths to
74// resource files used by tests.
75// The implementation is simple: it just looks for the file defined by
76// kProjectRootFileName, starting in the current directory (the working
77// directory) and then steps upward until it is found (or it is at the root of
78// the file system).
79// If the current working directory is above the project root dir, it will not
80// be found.
81//
82// If symbolic links occur in the path they will be resolved and the actual
83// directory will be returned.
84//
85// Returns the absolute path to the project root dir (usually the trunk dir)
86// WITH a trailing path delimiter.
87// If the project root is not found, the string specified by
88// kCannotFindProjectRootDir is returned.
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000089std::string ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000090
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000091// Creates and returns the absolute path to the output directory where log files
92// and other test artifacts should be put. The output directory is always a
93// directory named "out" at the top-level of the project, i.e. a subfolder to
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000094// the path returned by ProjectRootPath().
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000095//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000096// Details described for ProjectRootPath() apply here too.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000097//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000098// Returns the path WITH a trailing path delimiter. If the project root is not
99// found, the current working directory ("./") is returned as a fallback.
100std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000101
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000102} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +0000103} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000104
105#endif // TEST_TESTSUPPORT_FILEUTILS_H_