Correct blacklist entry message
[chromium-blink-merge.git] / tools / gn / filesystem_utils_unittest.cc
blobaeb09a4f95dd485a55b0662745e957f80da869be
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.
5 #include "base/strings/string_util.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "tools/gn/filesystem_utils.h"
11 TEST(FilesystemUtils, FileExtensionOffset) {
12 EXPECT_EQ(std::string::npos, FindExtensionOffset(""));
13 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz"));
14 EXPECT_EQ(4u, FindExtensionOffset("foo."));
15 EXPECT_EQ(4u, FindExtensionOffset("f.o.bar"));
16 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo.bar/"));
17 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo.bar/baz"));
20 TEST(FilesystemUtils, FindExtension) {
21 std::string input;
22 EXPECT_EQ("", FindExtension(&input).as_string());
23 input = "foo/bar/baz";
24 EXPECT_EQ("", FindExtension(&input).as_string());
25 input = "foo.";
26 EXPECT_EQ("", FindExtension(&input).as_string());
27 input = "f.o.bar";
28 EXPECT_EQ("bar", FindExtension(&input).as_string());
29 input = "foo.bar/";
30 EXPECT_EQ("", FindExtension(&input).as_string());
31 input = "foo.bar/baz";
32 EXPECT_EQ("", FindExtension(&input).as_string());
35 TEST(FilesystemUtils, FindFilenameOffset) {
36 EXPECT_EQ(0u, FindFilenameOffset(""));
37 EXPECT_EQ(0u, FindFilenameOffset("foo"));
38 EXPECT_EQ(4u, FindFilenameOffset("foo/"));
39 EXPECT_EQ(4u, FindFilenameOffset("foo/bar"));
42 TEST(FilesystemUtils, RemoveFilename) {
43 std::string s;
45 RemoveFilename(&s);
46 EXPECT_STREQ("", s.c_str());
48 s = "foo";
49 RemoveFilename(&s);
50 EXPECT_STREQ("", s.c_str());
52 s = "/";
53 RemoveFilename(&s);
54 EXPECT_STREQ("/", s.c_str());
56 s = "foo/bar";
57 RemoveFilename(&s);
58 EXPECT_STREQ("foo/", s.c_str());
60 s = "foo/bar/baz.cc";
61 RemoveFilename(&s);
62 EXPECT_STREQ("foo/bar/", s.c_str());
65 TEST(FilesystemUtils, FindDir) {
66 std::string input;
67 EXPECT_EQ("", FindDir(&input));
68 input = "/";
69 EXPECT_EQ("/", FindDir(&input));
70 input = "foo/";
71 EXPECT_EQ("foo/", FindDir(&input));
72 input = "foo/bar/baz";
73 EXPECT_EQ("foo/bar/", FindDir(&input));
76 TEST(FilesystemUtils, IsPathAbsolute) {
77 EXPECT_TRUE(IsPathAbsolute("/foo/bar"));
78 EXPECT_TRUE(IsPathAbsolute("/"));
79 EXPECT_FALSE(IsPathAbsolute(""));
80 EXPECT_FALSE(IsPathAbsolute("//"));
81 EXPECT_FALSE(IsPathAbsolute("//foo/bar"));
83 #if defined(OS_WIN)
84 EXPECT_TRUE(IsPathAbsolute("C:/foo"));
85 EXPECT_TRUE(IsPathAbsolute("C:/"));
86 EXPECT_TRUE(IsPathAbsolute("C:\\foo"));
87 EXPECT_TRUE(IsPathAbsolute("C:\\"));
88 EXPECT_TRUE(IsPathAbsolute("/C:/foo"));
89 EXPECT_TRUE(IsPathAbsolute("/C:\\foo"));
90 #endif
93 TEST(FilesystemUtils, MakeAbsolutePathRelativeIfPossible) {
94 std::string dest;
96 #if defined(OS_WIN)
97 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base\\foo",
98 &dest));
99 EXPECT_EQ("//foo", dest);
100 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base/foo",
101 &dest));
102 EXPECT_EQ("//foo", dest);
103 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("c:\\base", "C:\\base\\foo\\",
104 &dest));
105 EXPECT_EQ("//foo\\", dest);
107 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\ba", &dest));
108 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base",
109 "C:\\/notbase/foo",
110 &dest));
111 #else
113 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base/foo/", &dest));
114 EXPECT_EQ("//foo/", dest);
115 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base/foo", &dest));
116 EXPECT_EQ("//foo", dest);
117 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base/", "/base/foo/",
118 &dest));
119 EXPECT_EQ("//foo/", dest);
121 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/ba", &dest));
122 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/notbase/foo",
123 &dest));
124 #endif
127 TEST(FilesystemUtils, InvertDir) {
128 EXPECT_TRUE(InvertDir(SourceDir()) == "");
129 EXPECT_TRUE(InvertDir(SourceDir("/")) == "");
130 EXPECT_TRUE(InvertDir(SourceDir("//")) == "");
132 EXPECT_TRUE(InvertDir(SourceDir("//foo/bar")) == "../../");
133 EXPECT_TRUE(InvertDir(SourceDir("/foo/bar/")) == "../../");
136 TEST(FilesystemUtils, NormalizePath) {
137 std::string input;
139 NormalizePath(&input);
140 EXPECT_EQ("", input);
142 input = "foo/bar.txt";
143 NormalizePath(&input);
144 EXPECT_EQ("foo/bar.txt", input);
146 input = ".";
147 NormalizePath(&input);
148 EXPECT_EQ("", input);
150 input = "..";
151 NormalizePath(&input);
152 EXPECT_EQ("..", input);
154 input = "foo//bar";
155 NormalizePath(&input);
156 EXPECT_EQ("foo/bar", input);
158 input = "//foo";
159 NormalizePath(&input);
160 EXPECT_EQ("//foo", input);
162 input = "foo/..//bar";
163 NormalizePath(&input);
164 EXPECT_EQ("bar", input);
166 input = "foo/../../bar";
167 NormalizePath(&input);
168 EXPECT_EQ("../bar", input);
170 input = "/../foo"; // Don't go aboe the root dir.
171 NormalizePath(&input);
172 EXPECT_EQ("/foo", input);
174 input = "//../foo"; // Don't go aboe the root dir.
175 NormalizePath(&input);
176 EXPECT_EQ("//foo", input);
178 input = "../foo";
179 NormalizePath(&input);
180 EXPECT_EQ("../foo", input);
182 input = "..";
183 NormalizePath(&input);
184 EXPECT_EQ("..", input);
186 input = "./././.";
187 NormalizePath(&input);
188 EXPECT_EQ("", input);
190 input = "../../..";
191 NormalizePath(&input);
192 EXPECT_EQ("../../..", input);
194 input = "../";
195 NormalizePath(&input);
196 EXPECT_EQ("../", input);
199 TEST(FilesystemUtils, RebaseSourceAbsolutePath) {
200 // Degenerate case.
201 EXPECT_EQ(".", RebaseSourceAbsolutePath("//", SourceDir("//")));
202 EXPECT_EQ(".",
203 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//foo/bar/")));
205 // Going up the tree.
206 EXPECT_EQ("../foo",
207 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/")));
208 EXPECT_EQ("../foo/",
209 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/")));
210 EXPECT_EQ("../../foo",
211 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/moo")));
212 EXPECT_EQ("../../foo/",
213 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/moo")));
215 // Going down the tree.
216 EXPECT_EQ("foo/bar",
217 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//")));
218 EXPECT_EQ("foo/bar/",
219 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//")));
221 // Going up and down the tree.
222 EXPECT_EQ("../../foo/bar",
223 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//a/b/")));
224 EXPECT_EQ("../../foo/bar/",
225 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//a/b/")));
227 // Sharing prefix.
228 EXPECT_EQ("foo",
229 RebaseSourceAbsolutePath("//a/foo", SourceDir("//a/")));
230 EXPECT_EQ("foo/",
231 RebaseSourceAbsolutePath("//a/foo/", SourceDir("//a/")));
232 EXPECT_EQ("foo",
233 RebaseSourceAbsolutePath("//a/b/foo", SourceDir("//a/b/")));
234 EXPECT_EQ("foo/",
235 RebaseSourceAbsolutePath("//a/b/foo/", SourceDir("//a/b/")));
236 EXPECT_EQ("foo/bar",
237 RebaseSourceAbsolutePath("//a/b/foo/bar", SourceDir("//a/b/")));
238 EXPECT_EQ("foo/bar/",
239 RebaseSourceAbsolutePath("//a/b/foo/bar/", SourceDir("//a/b/")));
241 // One could argue about this case. Since the input doesn't have a slash it
242 // would normally not be treated like a directory and we'd go up, which is
243 // simpler. However, since it matches the output directory's name, we could
244 // potentially infer that it's the same and return "." for this.
245 EXPECT_EQ("../bar",
246 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//foo/bar/")));