OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "tools/gn/filesystem_utils.h" | 10 #include "tools/gn/filesystem_utils.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 EXPECT_EQ("//foo", input); | 197 EXPECT_EQ("//foo", input); |
198 | 198 |
199 input = "foo/..//bar"; | 199 input = "foo/..//bar"; |
200 NormalizePath(&input); | 200 NormalizePath(&input); |
201 EXPECT_EQ("bar", input); | 201 EXPECT_EQ("bar", input); |
202 | 202 |
203 input = "foo/../../bar"; | 203 input = "foo/../../bar"; |
204 NormalizePath(&input); | 204 NormalizePath(&input); |
205 EXPECT_EQ("../bar", input); | 205 EXPECT_EQ("../bar", input); |
206 | 206 |
207 input = "/../foo"; // Don't go aboe the root dir. | 207 input = "/../foo"; // Don't go above the root dir. |
208 NormalizePath(&input); | 208 NormalizePath(&input); |
209 EXPECT_EQ("/foo", input); | 209 EXPECT_EQ("/foo", input); |
210 | 210 |
211 input = "//../foo"; // Don't go above the root dir. | 211 input = "//../foo"; // Don't go above the root dir. |
212 NormalizePath(&input); | 212 NormalizePath(&input); |
213 EXPECT_EQ("//foo", input); | 213 EXPECT_EQ("//foo", input); |
214 | 214 |
215 input = "../foo"; | 215 input = "../foo"; |
216 NormalizePath(&input); | 216 NormalizePath(&input); |
217 EXPECT_EQ("../foo", input); | 217 EXPECT_EQ("../foo", input); |
(...skipping 16 matching lines...) Expand all Loading... | |
234 | 234 |
235 // Backslash normalization. | 235 // Backslash normalization. |
236 input = "foo\\..\\..\\bar"; | 236 input = "foo\\..\\..\\bar"; |
237 NormalizePath(&input); | 237 NormalizePath(&input); |
238 EXPECT_EQ("../bar", input); | 238 EXPECT_EQ("../bar", input); |
239 | 239 |
240 // Trailing slashes should get preserved. | 240 // Trailing slashes should get preserved. |
241 input = "//foo/bar/"; | 241 input = "//foo/bar/"; |
242 NormalizePath(&input); | 242 NormalizePath(&input); |
243 EXPECT_EQ("//foo/bar/", input); | 243 EXPECT_EQ("//foo/bar/", input); |
244 | |
245 // Go above and outside of the source root. | |
246 input = "//../foo"; | |
247 NormalizePath(&input, "/source/root"); | |
248 EXPECT_EQ("/source/foo", input); | |
249 | |
250 input = "//../foo"; | |
251 NormalizePath(&input, "/source/root/"); | |
252 EXPECT_EQ("/source/foo", input); | |
253 | |
254 input = "//../"; | |
255 NormalizePath(&input, "/source/root/"); | |
256 EXPECT_EQ("/source/", input); | |
257 | |
258 input = "//../foo.txt"; | |
259 NormalizePath(&input, "/source/root"); | |
260 EXPECT_EQ("/source/foo.txt", input); | |
261 | |
262 input = "//../foo/bar/"; | |
263 NormalizePath(&input, "/source/root"); | |
264 EXPECT_EQ("/source/foo/bar/", input); | |
265 | |
266 // Go above and back into the source root. This should return a system- | |
267 // absolute path. We could arguably return this as a source-absolute path, | |
268 // but that would require additional handling to account for a rare edge | |
269 // case. | |
270 input = "//../root/foo"; | |
271 NormalizePath(&input, "/source/root"); | |
272 EXPECT_EQ("/source/root/foo", input); | |
273 | |
274 input = "//../root/foo/bar/"; | |
275 NormalizePath(&input, "/source/root"); | |
276 EXPECT_EQ("/source/root/foo/bar/", input); | |
277 | |
278 // Stay inside the source root | |
279 input = "//foo/bar"; | |
280 NormalizePath(&input, "/source/root"); | |
281 EXPECT_EQ("//foo/bar", input); | |
282 | |
283 input = "//foo/bar/"; | |
284 NormalizePath(&input, "/source/root"); | |
285 EXPECT_EQ("//foo/bar/", input); | |
286 | |
287 // The path should not go above the system root. | |
288 input = "//../../../../../foo/bar"; | |
289 NormalizePath(&input, "/source/root"); | |
290 EXPECT_EQ("/foo/bar", input); | |
244 } | 291 } |
brettw
2015/12/01 01:21:22
Can you add a test to SourceDir that checks this i
slan
2015/12/01 22:15:03
Done.
| |
245 | 292 |
246 TEST(FilesystemUtils, RebasePath) { | 293 TEST(FilesystemUtils, RebasePath) { |
247 base::StringPiece source_root("/source/root"); | 294 base::StringPiece source_root("/source/root"); |
248 | 295 |
249 // Degenerate case. | 296 // Degenerate case. |
250 EXPECT_EQ(".", RebasePath("//", SourceDir("//"), source_root)); | 297 EXPECT_EQ(".", RebasePath("//", SourceDir("//"), source_root)); |
251 EXPECT_EQ(".", RebasePath("//foo/bar/", SourceDir("//foo/bar/"), | 298 EXPECT_EQ(".", RebasePath("//foo/bar/", SourceDir("//foo/bar/"), |
252 source_root)); | 299 source_root)); |
253 | 300 |
254 // Going up the tree. | 301 // Going up the tree. |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); | 640 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); |
594 EXPECT_EQ("//obj/", | 641 EXPECT_EQ("//obj/", |
595 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); | 642 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); |
596 EXPECT_EQ("obj/", | 643 EXPECT_EQ("obj/", |
597 GetOutputDirForSourceDirAsOutputFile( | 644 GetOutputDirForSourceDirAsOutputFile( |
598 &settings, SourceDir("//")).value()); | 645 &settings, SourceDir("//")).value()); |
599 EXPECT_EQ("gen/", | 646 EXPECT_EQ("gen/", |
600 GetGenDirForSourceDirAsOutputFile( | 647 GetGenDirForSourceDirAsOutputFile( |
601 &settings, SourceDir("//")).value()); | 648 &settings, SourceDir("//")).value()); |
602 } | 649 } |
OLD | NEW |