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/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
6 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
7 #include "build/build_config.h" | 8 #include "build/build_config.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "tools/gn/filesystem_utils.h" | 10 #include "tools/gn/filesystem_utils.h" |
10 #include "tools/gn/target.h" | 11 #include "tools/gn/target.h" |
11 | 12 |
12 TEST(FilesystemUtils, FileExtensionOffset) { | 13 TEST(FilesystemUtils, FileExtensionOffset) { |
13 EXPECT_EQ(std::string::npos, FindExtensionOffset("")); | 14 EXPECT_EQ(std::string::npos, FindExtensionOffset("")); |
14 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz")); | 15 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz")); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base/", "/base/foo/", | 163 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base/", "/base/foo/", |
163 &dest)); | 164 &dest)); |
164 EXPECT_EQ("//foo/", dest); | 165 EXPECT_EQ("//foo/", dest); |
165 | 166 |
166 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/ba", &dest)); | 167 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/ba", &dest)); |
167 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/notbase/foo", | 168 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/notbase/foo", |
168 &dest)); | 169 &dest)); |
169 #endif | 170 #endif |
170 } | 171 } |
171 | 172 |
172 TEST(FilesystemUtils, InvertDir) { | |
173 EXPECT_TRUE(InvertDir(SourceDir()) == ""); | |
174 EXPECT_TRUE(InvertDir(SourceDir("/")) == ""); | |
175 EXPECT_TRUE(InvertDir(SourceDir("//")) == ""); | |
176 | |
177 EXPECT_TRUE(InvertDir(SourceDir("//foo/bar")) == "../../"); | |
178 EXPECT_TRUE(InvertDir(SourceDir("//foo\\bar")) == "../../"); | |
179 EXPECT_TRUE(InvertDir(SourceDir("/foo/bar/")) == "../../"); | |
180 } | |
181 | |
182 TEST(FilesystemUtils, NormalizePath) { | 173 TEST(FilesystemUtils, NormalizePath) { |
183 std::string input; | 174 std::string input; |
184 | 175 |
185 NormalizePath(&input); | 176 NormalizePath(&input); |
186 EXPECT_EQ("", input); | 177 EXPECT_EQ("", input); |
187 | 178 |
188 input = "foo/bar.txt"; | 179 input = "foo/bar.txt"; |
189 NormalizePath(&input); | 180 NormalizePath(&input); |
190 EXPECT_EQ("foo/bar.txt", input); | 181 EXPECT_EQ("foo/bar.txt", input); |
191 | 182 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 NormalizePath(&input); | 232 NormalizePath(&input); |
242 EXPECT_EQ("../", input); | 233 EXPECT_EQ("../", input); |
243 | 234 |
244 // Backslash normalization. | 235 // Backslash normalization. |
245 input = "foo\\..\\..\\bar"; | 236 input = "foo\\..\\..\\bar"; |
246 NormalizePath(&input); | 237 NormalizePath(&input); |
247 EXPECT_EQ("../bar", input); | 238 EXPECT_EQ("../bar", input); |
248 } | 239 } |
249 | 240 |
250 TEST(FilesystemUtils, RebaseSourceAbsolutePath) { | 241 TEST(FilesystemUtils, RebaseSourceAbsolutePath) { |
| 242 base::StringPiece source_root("/source/root"); |
| 243 |
251 // Degenerate case. | 244 // Degenerate case. |
252 EXPECT_EQ(".", RebaseSourceAbsolutePath("//", SourceDir("//"))); | 245 EXPECT_EQ(".", RebaseSourceAbsolutePath("//", SourceDir("//"), source_root)); |
253 EXPECT_EQ(".", | 246 EXPECT_EQ(".", |
254 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//foo/bar/"))); | 247 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//foo/bar/"), |
| 248 source_root)); |
255 | 249 |
256 // Going up the tree. | 250 // Going up the tree. |
257 EXPECT_EQ("../foo", | 251 EXPECT_EQ("../foo", |
258 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/"))); | 252 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/"), |
| 253 source_root)); |
259 EXPECT_EQ("../foo/", | 254 EXPECT_EQ("../foo/", |
260 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/"))); | 255 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/"), |
| 256 source_root)); |
261 EXPECT_EQ("../../foo", | 257 EXPECT_EQ("../../foo", |
262 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/moo"))); | 258 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/moo"), |
| 259 source_root)); |
263 EXPECT_EQ("../../foo/", | 260 EXPECT_EQ("../../foo/", |
264 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/moo"))); | 261 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/moo"), |
| 262 source_root)); |
265 | 263 |
266 // Going down the tree. | 264 // Going down the tree. |
267 EXPECT_EQ("foo/bar", | 265 EXPECT_EQ("foo/bar", |
268 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//"))); | 266 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//"), |
| 267 source_root)); |
269 EXPECT_EQ("foo/bar/", | 268 EXPECT_EQ("foo/bar/", |
270 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//"))); | 269 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//"), |
| 270 source_root)); |
271 | 271 |
272 // Going up and down the tree. | 272 // Going up and down the tree. |
273 EXPECT_EQ("../../foo/bar", | 273 EXPECT_EQ("../../foo/bar", |
274 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//a/b/"))); | 274 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//a/b/"), |
| 275 source_root)); |
275 EXPECT_EQ("../../foo/bar/", | 276 EXPECT_EQ("../../foo/bar/", |
276 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//a/b/"))); | 277 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//a/b/"), |
| 278 source_root)); |
277 | 279 |
278 // Sharing prefix. | 280 // Sharing prefix. |
279 EXPECT_EQ("foo", | 281 EXPECT_EQ("foo", |
280 RebaseSourceAbsolutePath("//a/foo", SourceDir("//a/"))); | 282 RebaseSourceAbsolutePath("//a/foo", SourceDir("//a/"), |
| 283 source_root)); |
281 EXPECT_EQ("foo/", | 284 EXPECT_EQ("foo/", |
282 RebaseSourceAbsolutePath("//a/foo/", SourceDir("//a/"))); | 285 RebaseSourceAbsolutePath("//a/foo/", SourceDir("//a/"), |
| 286 source_root)); |
283 EXPECT_EQ("foo", | 287 EXPECT_EQ("foo", |
284 RebaseSourceAbsolutePath("//a/b/foo", SourceDir("//a/b/"))); | 288 RebaseSourceAbsolutePath("//a/b/foo", SourceDir("//a/b/"), |
| 289 source_root)); |
285 EXPECT_EQ("foo/", | 290 EXPECT_EQ("foo/", |
286 RebaseSourceAbsolutePath("//a/b/foo/", SourceDir("//a/b/"))); | 291 RebaseSourceAbsolutePath("//a/b/foo/", SourceDir("//a/b/"), |
| 292 source_root)); |
287 EXPECT_EQ("foo/bar", | 293 EXPECT_EQ("foo/bar", |
288 RebaseSourceAbsolutePath("//a/b/foo/bar", SourceDir("//a/b/"))); | 294 RebaseSourceAbsolutePath("//a/b/foo/bar", SourceDir("//a/b/"), |
| 295 source_root)); |
289 EXPECT_EQ("foo/bar/", | 296 EXPECT_EQ("foo/bar/", |
290 RebaseSourceAbsolutePath("//a/b/foo/bar/", SourceDir("//a/b/"))); | 297 RebaseSourceAbsolutePath("//a/b/foo/bar/", SourceDir("//a/b/"), |
| 298 source_root)); |
291 | 299 |
292 // One could argue about this case. Since the input doesn't have a slash it | 300 // One could argue about this case. Since the input doesn't have a slash it |
293 // would normally not be treated like a directory and we'd go up, which is | 301 // would normally not be treated like a directory and we'd go up, which is |
294 // simpler. However, since it matches the output directory's name, we could | 302 // simpler. However, since it matches the output directory's name, we could |
295 // potentially infer that it's the same and return "." for this. | 303 // potentially infer that it's the same and return "." for this. |
296 EXPECT_EQ("../bar", | 304 EXPECT_EQ("../bar", |
297 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//foo/bar/"))); | 305 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//foo/bar/"), |
| 306 source_root)); |
| 307 |
| 308 // Check when only |input| is system-absolute |
| 309 EXPECT_EQ("foo", |
| 310 RebaseSourceAbsolutePath("/source/root/foo", |
| 311 SourceDir("//"), |
| 312 base::StringPiece("/source/root"))); |
| 313 EXPECT_EQ("foo/", |
| 314 RebaseSourceAbsolutePath("/source/root/foo/", |
| 315 SourceDir("//"), |
| 316 base::StringPiece("/source/root"))); |
| 317 EXPECT_EQ("../../builddir/Out/Debug", |
| 318 RebaseSourceAbsolutePath("/builddir/Out/Debug", |
| 319 SourceDir("//"), |
| 320 base::StringPiece("/source/root"))); |
| 321 EXPECT_EQ("../../../builddir/Out/Debug", |
| 322 RebaseSourceAbsolutePath("/builddir/Out/Debug", |
| 323 SourceDir("//"), |
| 324 base::StringPiece("/source/root/foo"))); |
| 325 EXPECT_EQ("../../../builddir/Out/Debug/", |
| 326 RebaseSourceAbsolutePath("/builddir/Out/Debug/", |
| 327 SourceDir("//"), |
| 328 base::StringPiece("/source/root/foo"))); |
| 329 EXPECT_EQ("../../path/to/foo", |
| 330 RebaseSourceAbsolutePath("/path/to/foo", |
| 331 SourceDir("//"), |
| 332 base::StringPiece("/source/root"))); |
| 333 EXPECT_EQ("../../../path/to/foo", |
| 334 RebaseSourceAbsolutePath("/path/to/foo", |
| 335 SourceDir("//a"), |
| 336 base::StringPiece("/source/root"))); |
| 337 EXPECT_EQ("../../../../path/to/foo", |
| 338 RebaseSourceAbsolutePath("/path/to/foo", |
| 339 SourceDir("//a/b"), |
| 340 base::StringPiece("/source/root"))); |
| 341 |
| 342 // Check when only |dest_dir| is system-absolute. |
| 343 EXPECT_EQ(".", |
| 344 RebaseSourceAbsolutePath("//", |
| 345 SourceDir("/source/root"), |
| 346 base::StringPiece("/source/root"))); |
| 347 EXPECT_EQ("foo", |
| 348 RebaseSourceAbsolutePath("//foo", |
| 349 SourceDir("/source/root"), |
| 350 base::StringPiece("/source/root"))); |
| 351 EXPECT_EQ("../foo", |
| 352 RebaseSourceAbsolutePath("//foo", |
| 353 SourceDir("/source/root/bar"), |
| 354 base::StringPiece("/source/root"))); |
| 355 EXPECT_EQ("../../../source/root/foo", |
| 356 RebaseSourceAbsolutePath("//foo", |
| 357 SourceDir("/other/source/root"), |
| 358 base::StringPiece("/source/root"))); |
| 359 EXPECT_EQ("../../../../source/root/foo", |
| 360 RebaseSourceAbsolutePath("//foo", |
| 361 SourceDir("/other/source/root/bar"), |
| 362 base::StringPiece("/source/root"))); |
| 363 |
| 364 // Check when |input| and |dest_dir| are both system-absolute. Also, |
| 365 // in this case |source_root| is never used so set it to a dummy |
| 366 // value. |
| 367 EXPECT_EQ("foo", |
| 368 RebaseSourceAbsolutePath("/source/root/foo", |
| 369 SourceDir("/source/root"), |
| 370 base::StringPiece("/x/y/z"))); |
| 371 EXPECT_EQ("foo/", |
| 372 RebaseSourceAbsolutePath("/source/root/foo/", |
| 373 SourceDir("/source/root"), |
| 374 base::StringPiece("/x/y/z"))); |
| 375 EXPECT_EQ("../../builddir/Out/Debug", |
| 376 RebaseSourceAbsolutePath("/builddir/Out/Debug", |
| 377 SourceDir("/source/root"), |
| 378 base::StringPiece("/x/y/z"))); |
| 379 EXPECT_EQ("../../../builddir/Out/Debug", |
| 380 RebaseSourceAbsolutePath("/builddir/Out/Debug", |
| 381 SourceDir("/source/root/foo"), |
| 382 base::StringPiece("/source/root/foo"))); |
| 383 EXPECT_EQ("../../../builddir/Out/Debug/", |
| 384 RebaseSourceAbsolutePath("/builddir/Out/Debug/", |
| 385 SourceDir("/source/root/foo"), |
| 386 base::StringPiece("/source/root/foo"))); |
| 387 EXPECT_EQ("../../path/to/foo", |
| 388 RebaseSourceAbsolutePath("/path/to/foo", |
| 389 SourceDir("/source/root"), |
| 390 base::StringPiece("/x/y/z"))); |
| 391 EXPECT_EQ("../../../path/to/foo", |
| 392 RebaseSourceAbsolutePath("/path/to/foo", |
| 393 SourceDir("/source/root/a"), |
| 394 base::StringPiece("/x/y/z"))); |
| 395 EXPECT_EQ("../../../../path/to/foo", |
| 396 RebaseSourceAbsolutePath("/path/to/foo", |
| 397 SourceDir("/source/root/a/b"), |
| 398 base::StringPiece("/x/y/z"))); |
| 399 |
298 } | 400 } |
299 | 401 |
300 TEST(FilesystemUtils, DirectoryWithNoLastSlash) { | 402 TEST(FilesystemUtils, DirectoryWithNoLastSlash) { |
301 EXPECT_EQ("", DirectoryWithNoLastSlash(SourceDir())); | 403 EXPECT_EQ("", DirectoryWithNoLastSlash(SourceDir())); |
302 EXPECT_EQ("/.", DirectoryWithNoLastSlash(SourceDir("/"))); | 404 EXPECT_EQ("/.", DirectoryWithNoLastSlash(SourceDir("/"))); |
303 EXPECT_EQ("//.", DirectoryWithNoLastSlash(SourceDir("//"))); | 405 EXPECT_EQ("//.", DirectoryWithNoLastSlash(SourceDir("//"))); |
304 EXPECT_EQ("//foo", DirectoryWithNoLastSlash(SourceDir("//foo/"))); | 406 EXPECT_EQ("//foo", DirectoryWithNoLastSlash(SourceDir("//foo/"))); |
305 EXPECT_EQ("/bar", DirectoryWithNoLastSlash(SourceDir("/bar/"))); | 407 EXPECT_EQ("/bar", DirectoryWithNoLastSlash(SourceDir("/bar/"))); |
306 } | 408 } |
307 | 409 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); | 602 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); |
501 EXPECT_EQ("//obj/", | 603 EXPECT_EQ("//obj/", |
502 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); | 604 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); |
503 EXPECT_EQ("obj/", | 605 EXPECT_EQ("obj/", |
504 GetOutputDirForSourceDirAsOutputFile( | 606 GetOutputDirForSourceDirAsOutputFile( |
505 &settings, SourceDir("//")).value()); | 607 &settings, SourceDir("//")).value()); |
506 EXPECT_EQ("gen/", | 608 EXPECT_EQ("gen/", |
507 GetGenDirForSourceDirAsOutputFile( | 609 GetGenDirForSourceDirAsOutputFile( |
508 &settings, SourceDir("//")).value()); | 610 &settings, SourceDir("//")).value()); |
509 } | 611 } |
OLD | NEW |