OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/test/paths.h" | |
16 | |
17 #include <stdlib.h> | |
18 #include <sys/stat.h> | |
19 | |
20 #include "base/logging.h" | |
21 #include "build/build_config.h" | |
22 | |
23 namespace crashpad { | |
24 namespace test { | |
25 | |
26 namespace { | |
27 | |
28 bool IsTestDataRoot(const base::FilePath& candidate) { | |
29 const base::FilePath marker_path = | |
30 candidate.Append(FILE_PATH_LITERAL("util")) | |
31 .Append(FILE_PATH_LITERAL("test")) | |
32 .Append(FILE_PATH_LITERAL("paths_test_data_root.txt")); | |
33 | |
34 #if !defined(OS_WIN) | |
35 struct stat stat_buf; | |
36 int rv = stat(marker_path.value().c_str(), &stat_buf); | |
37 #else | |
38 struct _stat stat_buf; | |
39 int rv = _wstat(marker_path.value().c_str(), &stat_buf); | |
40 #endif | |
41 | |
42 return rv == 0; | |
43 } | |
44 | |
45 base::FilePath TestDataRootInternal() { | |
46 #if !defined(OS_WIN) | |
47 const char* environment_value = getenv("CRASHPAD_TEST_DATA_ROOT"); | |
48 #else // defined(OS_WIN) | |
49 const wchar_t* environment_value = _wgetenv(L"CRASHPAD_TEST_DATA_ROOT"); | |
50 #endif | |
51 | |
52 if (environment_value) { | |
53 // It was specified explicitly, so use it even if it seems incorrect. | |
54 if (!IsTestDataRoot(base::FilePath(environment_value))) { | |
55 LOG(WARNING) << "CRASHPAD_TEST_DATA_ROOT seems invalid, honoring anyway"; | |
56 } | |
57 | |
58 return base::FilePath(environment_value); | |
59 } | |
60 | |
61 // In a standalone build, the test executable is usually at | |
62 // out/{Debug,Release} relative to the Crashpad root. | |
63 const base::FilePath executable = Paths::Executable(); | |
64 base::FilePath candidate = | |
65 base::FilePath(executable.DirName() | |
66 .Append(base::FilePath::kParentDirectory) | |
67 .Append(base::FilePath::kParentDirectory)); | |
68 if (IsTestDataRoot(candidate)) { | |
69 return candidate; | |
70 } | |
71 | |
72 // In an in-Chromium build, the test executable is usually at | |
73 // out/{Debug,Release} relative to the Chromium root, and the Crashpad root is | |
74 // at third_party/crashpad/crashpad relative to the Chromium root. | |
75 candidate = candidate.Append(FILE_PATH_LITERAL("third_party")) | |
76 .Append(FILE_PATH_LITERAL("crashpad")) | |
77 .Append(FILE_PATH_LITERAL("crashpad")); | |
78 if (IsTestDataRoot(candidate)) { | |
79 return candidate; | |
80 } | |
81 | |
82 // If nothing else worked, use the current directory, issuing a warning if it | |
83 // doesn’t seem right. | |
84 if (!IsTestDataRoot(base::FilePath(base::FilePath::kCurrentDirectory))) { | |
85 LOG(WARNING) << "could not locate a valid test data root"; | |
86 } | |
87 | |
88 return base::FilePath(base::FilePath::kCurrentDirectory); | |
89 } | |
90 | |
91 } // namespace | |
92 | |
93 // static | |
94 base::FilePath Paths::TestDataRoot() { | |
95 static base::FilePath* test_data_root = | |
96 new base::FilePath(TestDataRootInternal()); | |
97 return *test_data_root; | |
98 } | |
99 | |
100 } // namespace test | |
101 } // namespace crashpad | |
OLD | NEW |