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 #if defined(OS_WIN) |
| 246 // Go above and outside of the source root. |
| 247 input = "//../foo"; |
| 248 NormalizePath(&input, "/C:/source/root"); |
| 249 EXPECT_EQ("/C:/source/foo", input); |
| 250 |
| 251 input = "//../foo"; |
| 252 NormalizePath(&input, "C:\\source\\root"); |
| 253 EXPECT_EQ("/C:/source/foo", input); |
| 254 |
| 255 input = "//../"; |
| 256 NormalizePath(&input, "/C:/source/root"); |
| 257 EXPECT_EQ("/C:/source/", input); |
| 258 |
| 259 input = "//../foo.txt"; |
| 260 NormalizePath(&input, "/C:/source/root"); |
| 261 EXPECT_EQ("/C:/source/foo.txt", input); |
| 262 |
| 263 input = "//../foo/bar/"; |
| 264 NormalizePath(&input, "/C:/source/root"); |
| 265 EXPECT_EQ("/C:/source/foo/bar/", input); |
| 266 |
| 267 // Go above and back into the source root. This should return a system- |
| 268 // absolute path. We could arguably return this as a source-absolute path, |
| 269 // but that would require additional handling to account for a rare edge |
| 270 // case. |
| 271 input = "//../root/foo"; |
| 272 NormalizePath(&input, "/C:/source/root"); |
| 273 EXPECT_EQ("/C:/source/root/foo", input); |
| 274 |
| 275 input = "//../root/foo/bar/"; |
| 276 NormalizePath(&input, "/C:/source/root"); |
| 277 EXPECT_EQ("/C:/source/root/foo/bar/", input); |
| 278 |
| 279 // Stay inside the source root |
| 280 input = "//foo/bar"; |
| 281 NormalizePath(&input, "/C:/source/root"); |
| 282 EXPECT_EQ("//foo/bar", input); |
| 283 |
| 284 input = "//foo/bar/"; |
| 285 NormalizePath(&input, "/C:/source/root"); |
| 286 EXPECT_EQ("//foo/bar/", input); |
| 287 |
| 288 // The path should not go above the system root. Note that on Windows, this |
| 289 // will consume the drive (C:). |
| 290 input = "//../../../../../foo/bar"; |
| 291 NormalizePath(&input, "/C:/source/root"); |
| 292 EXPECT_EQ("/foo/bar", input); |
| 293 |
| 294 // Test when the source root is the letter drive. |
| 295 input = "//../foo"; |
| 296 NormalizePath(&input, "/C:"); |
| 297 EXPECT_EQ("/foo", input); |
| 298 |
| 299 input = "//../foo"; |
| 300 NormalizePath(&input, "C:"); |
| 301 EXPECT_EQ("/foo", input); |
| 302 |
| 303 input = "//../foo"; |
| 304 NormalizePath(&input, "/"); |
| 305 EXPECT_EQ("/foo", input); |
| 306 |
| 307 input = "//../"; |
| 308 NormalizePath(&input, "\\C:"); |
| 309 EXPECT_EQ("/", input); |
| 310 |
| 311 input = "//../foo.txt"; |
| 312 NormalizePath(&input, "/C:"); |
| 313 EXPECT_EQ("/foo.txt", input); |
| 314 #else |
| 315 // Go above and outside of the source root. |
| 316 input = "//../foo"; |
| 317 NormalizePath(&input, "/source/root"); |
| 318 EXPECT_EQ("/source/foo", input); |
| 319 |
| 320 input = "//../"; |
| 321 NormalizePath(&input, "/source/root"); |
| 322 EXPECT_EQ("/source/", input); |
| 323 |
| 324 input = "//../foo.txt"; |
| 325 NormalizePath(&input, "/source/root"); |
| 326 EXPECT_EQ("/source/foo.txt", input); |
| 327 |
| 328 input = "//../foo/bar/"; |
| 329 NormalizePath(&input, "/source/root"); |
| 330 EXPECT_EQ("/source/foo/bar/", input); |
| 331 |
| 332 // Go above and back into the source root. This should return a system- |
| 333 // absolute path. We could arguably return this as a source-absolute path, |
| 334 // but that would require additional handling to account for a rare edge |
| 335 // case. |
| 336 input = "//../root/foo"; |
| 337 NormalizePath(&input, "/source/root"); |
| 338 EXPECT_EQ("/source/root/foo", input); |
| 339 |
| 340 input = "//../root/foo/bar/"; |
| 341 NormalizePath(&input, "/source/root"); |
| 342 EXPECT_EQ("/source/root/foo/bar/", input); |
| 343 |
| 344 // Stay inside the source root |
| 345 input = "//foo/bar"; |
| 346 NormalizePath(&input, "/source/root"); |
| 347 EXPECT_EQ("//foo/bar", input); |
| 348 |
| 349 input = "//foo/bar/"; |
| 350 NormalizePath(&input, "/source/root"); |
| 351 EXPECT_EQ("//foo/bar/", input); |
| 352 |
| 353 // The path should not go above the system root. |
| 354 input = "//../../../../../foo/bar"; |
| 355 NormalizePath(&input, "/source/root"); |
| 356 EXPECT_EQ("/foo/bar", input); |
| 357 |
| 358 // Test when the source root is the system root. |
| 359 input = "//../foo/bar/"; |
| 360 NormalizePath(&input, "/"); |
| 361 EXPECT_EQ("/foo/bar/", input); |
| 362 |
| 363 input = "//../"; |
| 364 NormalizePath(&input, "/"); |
| 365 EXPECT_EQ("/", input); |
| 366 |
| 367 input = "//../foo.txt"; |
| 368 NormalizePath(&input, "/"); |
| 369 EXPECT_EQ("/foo.txt", input); |
| 370 |
| 371 #endif |
244 } | 372 } |
245 | 373 |
246 TEST(FilesystemUtils, RebasePath) { | 374 TEST(FilesystemUtils, RebasePath) { |
247 base::StringPiece source_root("/source/root"); | 375 base::StringPiece source_root("/source/root"); |
248 | 376 |
249 // Degenerate case. | 377 // Degenerate case. |
250 EXPECT_EQ(".", RebasePath("//", SourceDir("//"), source_root)); | 378 EXPECT_EQ(".", RebasePath("//", SourceDir("//"), source_root)); |
251 EXPECT_EQ(".", RebasePath("//foo/bar/", SourceDir("//foo/bar/"), | 379 EXPECT_EQ(".", RebasePath("//foo/bar/", SourceDir("//foo/bar/"), |
252 source_root)); | 380 source_root)); |
253 | 381 |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); | 721 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); |
594 EXPECT_EQ("//obj/", | 722 EXPECT_EQ("//obj/", |
595 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); | 723 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); |
596 EXPECT_EQ("obj/", | 724 EXPECT_EQ("obj/", |
597 GetOutputDirForSourceDirAsOutputFile( | 725 GetOutputDirForSourceDirAsOutputFile( |
598 &settings, SourceDir("//")).value()); | 726 &settings, SourceDir("//")).value()); |
599 EXPECT_EQ("gen/", | 727 EXPECT_EQ("gen/", |
600 GetGenDirForSourceDirAsOutputFile( | 728 GetGenDirForSourceDirAsOutputFile( |
601 &settings, SourceDir("//")).value()); | 729 &settings, SourceDir("//")).value()); |
602 } | 730 } |
OLD | NEW |