Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Unified Diff: tools/gn/filesystem_utils_unittest.cc

Issue 1455203002: [GN] Add support to rebase_path to resolve paths above the source root. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Add tests, handle Windows sources. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gn/filesystem_utils.cc ('k') | tools/gn/function_rebase_path_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/filesystem_utils_unittest.cc
diff --git a/tools/gn/filesystem_utils_unittest.cc b/tools/gn/filesystem_utils_unittest.cc
index 9f89512cfa29f1260d20488e9a67a6feaed4e448..27ccab2ea4d0db453c217d1921db71729de58aac 100644
--- a/tools/gn/filesystem_utils_unittest.cc
+++ b/tools/gn/filesystem_utils_unittest.cc
@@ -204,7 +204,7 @@ TEST(FilesystemUtils, NormalizePath) {
NormalizePath(&input);
EXPECT_EQ("../bar", input);
- input = "/../foo"; // Don't go aboe the root dir.
+ input = "/../foo"; // Don't go above the root dir.
NormalizePath(&input);
EXPECT_EQ("/foo", input);
@@ -241,6 +241,134 @@ TEST(FilesystemUtils, NormalizePath) {
input = "//foo/bar/";
NormalizePath(&input);
EXPECT_EQ("//foo/bar/", input);
+
+#if defined(OS_WIN)
+ // Go above and outside of the source root.
+ input = "//../foo";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/C:/source/foo", input);
+
+ input = "//../foo";
+ NormalizePath(&input, "C:\\source\\root");
+ EXPECT_EQ("/C:/source/foo", input);
+
+ input = "//../";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/C:/source/", input);
+
+ input = "//../foo.txt";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/C:/source/foo.txt", input);
+
+ input = "//../foo/bar/";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/C:/source/foo/bar/", input);
+
+ // Go above and back into the source root. This should return a system-
+ // absolute path. We could arguably return this as a source-absolute path,
+ // but that would require additional handling to account for a rare edge
+ // case.
+ input = "//../root/foo";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/C:/source/root/foo", input);
+
+ input = "//../root/foo/bar/";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/C:/source/root/foo/bar/", input);
+
+ // Stay inside the source root
+ input = "//foo/bar";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("//foo/bar", input);
+
+ input = "//foo/bar/";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("//foo/bar/", input);
+
+ // The path should not go above the system root. Note that on Windows, this
+ // will consume the drive (C:).
+ input = "//../../../../../foo/bar";
+ NormalizePath(&input, "/C:/source/root");
+ EXPECT_EQ("/foo/bar", input);
+
+ // Test when the source root is the letter drive.
+ input = "//../foo";
+ NormalizePath(&input, "/C:");
+ EXPECT_EQ("/foo", input);
+
+ input = "//../foo";
+ NormalizePath(&input, "C:");
+ EXPECT_EQ("/foo", input);
+
+ input = "//../foo";
+ NormalizePath(&input, "/");
+ EXPECT_EQ("/foo", input);
+
+ input = "//../";
+ NormalizePath(&input, "\\C:");
+ EXPECT_EQ("/", input);
+
+ input = "//../foo.txt";
+ NormalizePath(&input, "/C:");
+ EXPECT_EQ("/foo.txt", input);
+#else
+ // Go above and outside of the source root.
+ input = "//../foo";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/source/foo", input);
+
+ input = "//../";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/source/", input);
+
+ input = "//../foo.txt";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/source/foo.txt", input);
+
+ input = "//../foo/bar/";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/source/foo/bar/", input);
+
+ // Go above and back into the source root. This should return a system-
+ // absolute path. We could arguably return this as a source-absolute path,
+ // but that would require additional handling to account for a rare edge
+ // case.
+ input = "//../root/foo";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/source/root/foo", input);
+
+ input = "//../root/foo/bar/";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/source/root/foo/bar/", input);
+
+ // Stay inside the source root
+ input = "//foo/bar";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("//foo/bar", input);
+
+ input = "//foo/bar/";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("//foo/bar/", input);
+
+ // The path should not go above the system root.
+ input = "//../../../../../foo/bar";
+ NormalizePath(&input, "/source/root");
+ EXPECT_EQ("/foo/bar", input);
+
+ // Test when the source root is the system root.
+ input = "//../foo/bar/";
+ NormalizePath(&input, "/");
+ EXPECT_EQ("/foo/bar/", input);
+
+ input = "//../";
+ NormalizePath(&input, "/");
+ EXPECT_EQ("/", input);
+
+ input = "//../foo.txt";
+ NormalizePath(&input, "/");
+ EXPECT_EQ("/foo.txt", input);
+
+#endif
}
TEST(FilesystemUtils, RebasePath) {
« no previous file with comments | « tools/gn/filesystem_utils.cc ('k') | tools/gn/function_rebase_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698