1 : // Copyright 2012 Google Inc.
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 "syzygy/core/unittest_util.h"
16 :
17 : #include "base/file_util.h"
18 : #include "base/path_service.h"
19 :
20 : namespace testing {
21 :
22 E : FilePath GetSrcRelativePath(const wchar_t* rel_path) {
23 E : FilePath src_dir;
24 E : PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
25 E : return src_dir.Append(rel_path);
26 E : }
27 :
28 E : FilePath GetExeRelativePath(const wchar_t* rel_path) {
29 E : FilePath exe_dir;
30 E : PathService::Get(base::DIR_EXE, &exe_dir);
31 E : return exe_dir.Append(rel_path);
32 E : }
33 :
34 E : FilePath GetOutputRelativePath(const wchar_t* rel_path) {
35 : #if defined(_DEBUG)
36 : // TODO(chrisha): Expose $(ProjectDir) and $(OutputDir) via defines in the
37 : // project gyp file.
38 : #if defined(_COVERAGE_BUILD)
39 : static const wchar_t kOutputDir[] = L"Coverage";
40 : #else
41 : static const wchar_t kOutputDir[] = L"Debug";
42 : #endif
43 : #else
44 : #if defined(NDEBUG)
45 : static const wchar_t kOutputDir[] = L"Release";
46 : #else
47 : #error Unknown build profile.
48 : #endif
49 : #endif
50 :
51 E : FilePath src_dir;
52 E : PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
53 E : src_dir = src_dir.Append(L"build");
54 E : src_dir = src_dir.Append(kOutputDir);
55 E : return src_dir.Append(rel_path);
56 E : }
57 :
58 E : FilePath GetExeTestDataRelativePath(const wchar_t* rel_path) {
59 E : FilePath exe_dir;
60 E : PathService::Get(base::DIR_EXE, &exe_dir);
61 E : FilePath test_data = exe_dir.Append(L"test_data");
62 E : return test_data.Append(rel_path);
63 E : }
64 :
65 E : FilePath GetRelativePath(const FilePath& abs_path, const FilePath& root_path) {
66 E : DCHECK(abs_path.IsAbsolute());
67 E : DCHECK(!abs_path.empty());
68 E : DCHECK(root_path.IsAbsolute());
69 E : DCHECK(!root_path.empty());
70 :
71 : typedef std::vector<FilePath::StringType> PathComponents;
72 :
73 : // Get the components of the target path.
74 E : PathComponents abs_parts;
75 E : abs_path.GetComponents(&abs_parts);
76 :
77 : // Get the components of the current working directory.
78 E : PathComponents root_parts;
79 E : root_path.GetComponents(&root_parts);
80 :
81 : // Make sure they have a common root.
82 E : if (!FilePath::CompareEqualIgnoreCase(root_parts[0], abs_parts[0]))
83 E : return FilePath();
84 :
85 : // Figure out how much is shared.
86 E : size_t i = 1;
87 : while (i < std::min(root_parts.size(), abs_parts.size()) &&
88 E : FilePath::CompareEqualIgnoreCase(root_parts[i], abs_parts[i])) {
89 E : ++i;
90 E : }
91 :
92 E : FilePath rel_path;
93 :
94 : // Add parent directory traversal.
95 E : for (size_t j = i; j < root_parts.size(); ++j)
96 E : rel_path = rel_path.Append(FilePath::kParentDirectory);
97 :
98 : // Append the rest of the path.
99 E : for (size_t k = i; k < abs_parts.size(); ++k)
100 E : rel_path = rel_path.Append(abs_parts[k]);
101 :
102 E : if (rel_path.empty())
103 E : rel_path = FilePath(FilePath::kCurrentDirectory);
104 :
105 E : return rel_path;
106 E : }
107 :
108 E : FilePath GetRelativePath(const FilePath& abs_path) {
109 E : FilePath cur_dir;
110 E : file_util::GetCurrentDirectory(&cur_dir);
111 E : return GetRelativePath(abs_path, cur_dir);
112 E : }
113 :
114 :
115 : } // namespace testing
|