1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gn/source_dir.h"
6 #include "gn/err.h"
7 #include "gn/source_file.h"
8 #include "gn/value.h"
9 #include "util/build_config.h"
10 #include "util/test/test.h"
11
TEST(SourceDir,ResolveRelativeFile)12 TEST(SourceDir, ResolveRelativeFile) {
13 Err err;
14 SourceDir base("//base/");
15 #if defined(OS_WIN)
16 std::string_view source_root("C:/source/root");
17 #else
18 std::string_view source_root("/source/root");
19 #endif
20
21 // Empty input is an error.
22 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, std::string()), &err,
23 source_root) == SourceFile());
24 EXPECT_TRUE(err.has_error());
25
26 // These things are directories, so should be an error.
27 err = Err();
28 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "//foo/bar/"), &err,
29 source_root) == SourceFile());
30 EXPECT_TRUE(err.has_error());
31
32 err = Err();
33 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "bar/"), &err,
34 source_root) == SourceFile());
35 EXPECT_TRUE(err.has_error());
36
37 // Absolute paths should be passed unchanged.
38 err = Err();
39 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "//foo"), &err,
40 source_root) == SourceFile("//foo"));
41 EXPECT_FALSE(err.has_error());
42
43 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "/foo"), &err,
44 source_root) == SourceFile("/foo"));
45 EXPECT_FALSE(err.has_error());
46
47 // Basic relative stuff.
48 EXPECT_TRUE(
49 base.ResolveRelativeFile(Value(nullptr, "foo"), &err, source_root) ==
50 SourceFile("//base/foo"));
51 EXPECT_FALSE(err.has_error());
52 EXPECT_TRUE(
53 base.ResolveRelativeFile(Value(nullptr, "./foo"), &err, source_root) ==
54 SourceFile("//base/foo"));
55 EXPECT_FALSE(err.has_error());
56 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "../foo"), &err,
57 source_root) == SourceFile("//foo"));
58 EXPECT_FALSE(err.has_error());
59
60 // If the given relative path points outside the source root, we
61 // expect an absolute path.
62 #if defined(OS_WIN)
63 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "../../foo"), &err,
64 source_root) ==
65 SourceFile("/C:/source/foo"));
66 EXPECT_FALSE(err.has_error());
67
68 EXPECT_TRUE(
69 base.ResolveRelativeFile(Value(nullptr, "//../foo"), &err, source_root) ==
70 SourceFile("/C:/source/foo"));
71 EXPECT_FALSE(err.has_error());
72
73 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "//../root/foo"), &err,
74 source_root) ==
75 SourceFile("/C:/source/root/foo"));
76 EXPECT_FALSE(err.has_error());
77
78 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "//../../../foo/bar"),
79 &err,
80 source_root) == SourceFile("/foo/bar"));
81 EXPECT_FALSE(err.has_error());
82 #else
83 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "../../foo"), &err,
84 source_root) ==
85 SourceFile("/source/foo"));
86 EXPECT_FALSE(err.has_error());
87
88 EXPECT_TRUE(
89 base.ResolveRelativeFile(Value(nullptr, "//../foo"), &err, source_root) ==
90 SourceFile("/source/foo"));
91 EXPECT_FALSE(err.has_error());
92
93 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "//../root/foo"), &err,
94 source_root) ==
95 SourceFile("/source/root/foo"));
96 EXPECT_FALSE(err.has_error());
97
98 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "//../../../foo/bar"),
99 &err,
100 source_root) == SourceFile("/foo/bar"));
101 EXPECT_FALSE(err.has_error());
102 #endif
103
104 #if defined(OS_WIN)
105 // Note that we don't canonicalize the backslashes to forward slashes.
106 // This could potentially be changed in the future which would mean we should
107 // just change the expected result.
108 EXPECT_TRUE(base.ResolveRelativeFile(Value(nullptr, "C:\\foo\\bar.txt"), &err,
109 source_root) ==
110 SourceFile("/C:/foo/bar.txt"));
111 EXPECT_FALSE(err.has_error());
112 #endif
113 }
114
TEST(SourceDir,ResolveRelativeDir)115 TEST(SourceDir, ResolveRelativeDir) {
116 Err err;
117 SourceDir base("//base/");
118 #if defined(OS_WIN)
119 std::string_view source_root("C:/source/root");
120 #else
121 std::string_view source_root("/source/root");
122 #endif
123
124 // Empty input is an error.
125 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, std::string()), &err,
126 source_root) == SourceDir());
127 EXPECT_TRUE(err.has_error());
128
129 // Absolute paths should be passed unchanged.
130 err = Err();
131 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "//foo"), &err,
132 source_root) == SourceDir("//foo/"));
133 EXPECT_FALSE(err.has_error());
134 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "/foo"), &err,
135 source_root) == SourceDir("/foo/"));
136 EXPECT_FALSE(err.has_error());
137
138 // Basic relative stuff.
139 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "foo"), &err,
140 source_root) == SourceDir("//base/foo/"));
141 EXPECT_FALSE(err.has_error());
142 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "./foo"), &err,
143 source_root) == SourceDir("//base/foo/"));
144 EXPECT_FALSE(err.has_error());
145 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "../foo"), &err,
146 source_root) == SourceDir("//foo/"));
147 EXPECT_FALSE(err.has_error());
148
149 // If the given relative path points outside the source root, we
150 // expect an absolute path.
151 #if defined(OS_WIN)
152 EXPECT_TRUE(
153 base.ResolveRelativeDir(Value(nullptr, "../../foo"), &err, source_root) ==
154 SourceDir("/C:/source/foo/"));
155 EXPECT_FALSE(err.has_error());
156 EXPECT_TRUE(
157 base.ResolveRelativeDir(Value(nullptr, "//../foo"), &err, source_root) ==
158 SourceDir("/C:/source/foo/"));
159 EXPECT_FALSE(err.has_error());
160 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "//.."), &err,
161 source_root) == SourceDir("/C:/source/"));
162 EXPECT_FALSE(err.has_error());
163 #else
164 EXPECT_TRUE(
165 base.ResolveRelativeDir(Value(nullptr, "../../foo"), &err, source_root) ==
166 SourceDir("/source/foo/"));
167 EXPECT_FALSE(err.has_error());
168 EXPECT_TRUE(
169 base.ResolveRelativeDir(Value(nullptr, "//../foo"), &err, source_root) ==
170 SourceDir("/source/foo/"));
171 EXPECT_FALSE(err.has_error());
172 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "//.."), &err,
173 source_root) == SourceDir("/source/"));
174 EXPECT_FALSE(err.has_error());
175 #endif
176
177 #if defined(OS_WIN)
178 // Canonicalize the existing backslashes to forward slashes and add a
179 // leading slash if necessary.
180 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "\\C:\\foo"), &err) ==
181 SourceDir("/C:/foo/"));
182 EXPECT_FALSE(err.has_error());
183 EXPECT_TRUE(base.ResolveRelativeDir(Value(nullptr, "C:\\foo"), &err) ==
184 SourceDir("/C:/foo/"));
185 EXPECT_FALSE(err.has_error());
186 #endif
187 }
188
TEST(SourceDir,SourceWithNoTrailingSlash)189 TEST(SourceDir, SourceWithNoTrailingSlash) {
190 Err err;
191 SourceDir base("//base/");
192 SourceDir base_no_slash("//base/");
193 EXPECT_EQ(base.SourceWithNoTrailingSlash(), "//base");
194 EXPECT_EQ(base_no_slash.SourceWithNoTrailingSlash(), "//base");
195
196 SourceDir relative_root("//");
197 EXPECT_EQ(relative_root.SourceWithNoTrailingSlash(), "//");
198
199 #if defined(OS_WIN)
200 SourceDir root("C:/");
201 SourceDir root_no_slash("C:");
202 EXPECT_EQ(root.SourceWithNoTrailingSlash(), "C:");
203 EXPECT_EQ(root_no_slash.SourceWithNoTrailingSlash(), "C:");
204 #else
205 SourceDir root("/");
206 EXPECT_EQ(root.SourceWithNoTrailingSlash(), "/");
207 #endif
208 }
209