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
) {
22 EXPECT_EQ("", FindExtension(&input
).as_string());
23 input
= "foo/bar/baz";
24 EXPECT_EQ("", FindExtension(&input
).as_string());
26 EXPECT_EQ("", FindExtension(&input
).as_string());
28 EXPECT_EQ("bar", FindExtension(&input
).as_string());
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
) {
46 EXPECT_STREQ("", s
.c_str());
50 EXPECT_STREQ("", s
.c_str());
54 EXPECT_STREQ("/", s
.c_str());
58 EXPECT_STREQ("foo/", s
.c_str());
62 EXPECT_STREQ("foo/bar/", s
.c_str());
65 TEST(FilesystemUtils
, FindDir
) {
67 EXPECT_EQ("", FindDir(&input
));
69 EXPECT_EQ("/", FindDir(&input
));
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"));
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"));
93 TEST(FilesystemUtils
, MakeAbsolutePathRelativeIfPossible
) {
97 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base\\foo",
99 EXPECT_EQ("//foo", dest
);
100 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base/foo",
102 EXPECT_EQ("//foo", dest
);
103 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("c:\\base", "C:\\base\\foo\\",
105 EXPECT_EQ("//foo\\", dest
);
107 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\ba", &dest
));
108 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base",
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/",
119 EXPECT_EQ("//foo/", dest
);
121 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/ba", &dest
));
122 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/notbase/foo",
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
) {
139 NormalizePath(&input
);
140 EXPECT_EQ("", input
);
142 input
= "foo/bar.txt";
143 NormalizePath(&input
);
144 EXPECT_EQ("foo/bar.txt", input
);
147 NormalizePath(&input
);
148 EXPECT_EQ("", input
);
151 NormalizePath(&input
);
152 EXPECT_EQ("..", input
);
155 NormalizePath(&input
);
156 EXPECT_EQ("foo/bar", input
);
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
);
179 NormalizePath(&input
);
180 EXPECT_EQ("../foo", input
);
183 NormalizePath(&input
);
184 EXPECT_EQ("..", input
);
187 NormalizePath(&input
);
188 EXPECT_EQ("", input
);
191 NormalizePath(&input
);
192 EXPECT_EQ("../../..", input
);
195 NormalizePath(&input
);
196 EXPECT_EQ("../", input
);
199 TEST(FilesystemUtils
, RebaseSourceAbsolutePath
) {
201 EXPECT_EQ(".", RebaseSourceAbsolutePath("//", SourceDir("//")));
203 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//foo/bar/")));
205 // Going up the tree.
207 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/")));
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.
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/")));
229 RebaseSourceAbsolutePath("//a/foo", SourceDir("//a/")));
231 RebaseSourceAbsolutePath("//a/foo/", SourceDir("//a/")));
233 RebaseSourceAbsolutePath("//a/b/foo", SourceDir("//a/b/")));
235 RebaseSourceAbsolutePath("//a/b/foo/", SourceDir("//a/b/")));
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.
246 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//foo/bar/")));