1 : // Copyright 2012 Google Inc. 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 "syzygy/core/unittest_util.h"
16 :
17 : #include "base/path_service.h"
18 : #include "syzygy/core/file_util.h"
19 :
20 : namespace testing {
21 :
22 E : base::FilePath GetSrcRelativePath(const wchar_t* rel_path) {
23 E : base::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 : base::FilePath GetExeRelativePath(const wchar_t* rel_path) {
29 E : base::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 : base::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 : base::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 : base::FilePath GetExeTestDataRelativePath(const wchar_t* rel_path) {
59 E : base::FilePath exe_dir;
60 E : PathService::Get(base::DIR_EXE, &exe_dir);
61 E : base::FilePath test_data = exe_dir.Append(L"test_data");
62 E : return test_data.Append(rel_path);
63 E : }
64 :
65 : base::FilePath GetRelativePath(const base::FilePath& abs_path,
66 E : const base::FilePath& root_path) {
67 E : DCHECK(abs_path.IsAbsolute());
68 E : DCHECK(!abs_path.empty());
69 E : DCHECK(root_path.IsAbsolute());
70 E : DCHECK(!root_path.empty());
71 :
72 : typedef std::vector<base::FilePath::StringType> PathComponents;
73 :
74 : // Get the components of the target path.
75 E : PathComponents abs_parts;
76 E : abs_path.GetComponents(&abs_parts);
77 :
78 : // Get the components of the current working directory.
79 E : PathComponents root_parts;
80 E : root_path.GetComponents(&root_parts);
81 :
82 : // Make sure they have a common root.
83 E : if (!base::FilePath::CompareEqualIgnoreCase(root_parts[0], abs_parts[0]))
84 E : return base::FilePath();
85 :
86 : // Figure out how much is shared.
87 E : size_t i = 1;
88 : while (i < std::min(root_parts.size(), abs_parts.size()) &&
89 E : base::FilePath::CompareEqualIgnoreCase(root_parts[i], abs_parts[i])) {
90 E : ++i;
91 E : }
92 :
93 E : base::FilePath rel_path;
94 :
95 : // Add parent directory traversal.
96 E : for (size_t j = i; j < root_parts.size(); ++j)
97 E : rel_path = rel_path.Append(base::FilePath::kParentDirectory);
98 :
99 : // Append the rest of the path.
100 E : for (size_t k = i; k < abs_parts.size(); ++k)
101 E : rel_path = rel_path.Append(abs_parts[k]);
102 :
103 E : if (rel_path.empty())
104 E : rel_path = base::FilePath(base::FilePath::kCurrentDirectory);
105 :
106 E : return rel_path;
107 E : }
108 :
109 E : base::FilePath GetRelativePath(const base::FilePath& abs_path) {
110 E : base::FilePath cur_dir;
111 E : file_util::GetCurrentDirectory(&cur_dir);
112 E : return GetRelativePath(abs_path, cur_dir);
113 E : }
114 :
115 : AssertionResult AssertAreSameFile(const char* path1_expr,
116 : const char* path2_expr,
117 : const base::FilePath& path1,
118 E : const base::FilePath& path2) {
119 E : core::FilePathCompareResult result = core::CompareFilePaths(path1, path2);
120 E : if (result == core::kEquivalentFilePaths)
121 E : return ::testing::AssertionSuccess();
122 :
123 : return ::testing::AssertionFailure() << "FilePathsReferToSameFile("
124 : << path1_expr << ", " << path2_expr << ") returned " << result
125 : << ", expected " << core::kEquivalentFilePaths << " (" << path1_expr
126 : << " = \"" << path1.value() << "\", " << path2_expr << " = \""
127 i : << path2.value() << "\").";
128 E : }
129 :
130 : } // namespace testing
|