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

Side by Side Diff: tools/gn/filesystem_utils_unittest.cc

Issue 711113004: original (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: win compile fix Created 6 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « tools/gn/filesystem_utils.cc ('k') | tools/gn/function_exec_script.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 input = "../"; 231 input = "../";
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, RebasePath) {
242 base::StringPiece source_root("/source/root");
243
251 // Degenerate case. 244 // Degenerate case.
252 EXPECT_EQ(".", RebaseSourceAbsolutePath("//", SourceDir("//"))); 245 EXPECT_EQ(".", RebasePath("//", SourceDir("//"), source_root));
253 EXPECT_EQ(".", 246 EXPECT_EQ(".", RebasePath("//foo/bar/", SourceDir("//foo/bar/"),
254 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//foo/bar/"))); 247 source_root));
255 248
256 // Going up the tree. 249 // Going up the tree.
257 EXPECT_EQ("../foo", 250 EXPECT_EQ("../foo", RebasePath("//foo", SourceDir("//bar/"), source_root));
258 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/"))); 251 EXPECT_EQ("../foo/", RebasePath("//foo/", SourceDir("//bar/"), source_root));
259 EXPECT_EQ("../foo/", 252 EXPECT_EQ("../../foo", RebasePath("//foo", SourceDir("//bar/moo"),
260 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/"))); 253 source_root));
261 EXPECT_EQ("../../foo", 254 EXPECT_EQ("../../foo/", RebasePath("//foo/", SourceDir("//bar/moo"),
262 RebaseSourceAbsolutePath("//foo", SourceDir("//bar/moo"))); 255 source_root));
263 EXPECT_EQ("../../foo/",
264 RebaseSourceAbsolutePath("//foo/", SourceDir("//bar/moo")));
265 256
266 // Going down the tree. 257 // Going down the tree.
267 EXPECT_EQ("foo/bar", 258 EXPECT_EQ("foo/bar", RebasePath("//foo/bar", SourceDir("//"), source_root));
268 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//"))); 259 EXPECT_EQ("foo/bar/", RebasePath("//foo/bar/", SourceDir("//"),
269 EXPECT_EQ("foo/bar/", 260 source_root));
270 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//")));
271 261
272 // Going up and down the tree. 262 // Going up and down the tree.
273 EXPECT_EQ("../../foo/bar", 263 EXPECT_EQ("../../foo/bar", RebasePath("//foo/bar", SourceDir("//a/b/"),
274 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//a/b/"))); 264 source_root));
275 EXPECT_EQ("../../foo/bar/", 265 EXPECT_EQ("../../foo/bar/", RebasePath("//foo/bar/", SourceDir("//a/b/"),
276 RebaseSourceAbsolutePath("//foo/bar/", SourceDir("//a/b/"))); 266 source_root));
277 267
278 // Sharing prefix. 268 // Sharing prefix.
279 EXPECT_EQ("foo", 269 EXPECT_EQ("foo", RebasePath("//a/foo", SourceDir("//a/"), source_root));
280 RebaseSourceAbsolutePath("//a/foo", SourceDir("//a/"))); 270 EXPECT_EQ("foo/", RebasePath("//a/foo/", SourceDir("//a/"), source_root));
281 EXPECT_EQ("foo/", 271 EXPECT_EQ("foo", RebasePath("//a/b/foo", SourceDir("//a/b/"), source_root));
282 RebaseSourceAbsolutePath("//a/foo/", SourceDir("//a/"))); 272 EXPECT_EQ("foo/", RebasePath("//a/b/foo/", SourceDir("//a/b/"),
283 EXPECT_EQ("foo", 273 source_root));
284 RebaseSourceAbsolutePath("//a/b/foo", SourceDir("//a/b/"))); 274 EXPECT_EQ("foo/bar", RebasePath("//a/b/foo/bar", SourceDir("//a/b/"),
285 EXPECT_EQ("foo/", 275 source_root));
286 RebaseSourceAbsolutePath("//a/b/foo/", SourceDir("//a/b/"))); 276 EXPECT_EQ("foo/bar/", RebasePath("//a/b/foo/bar/", SourceDir("//a/b/"),
287 EXPECT_EQ("foo/bar", 277 source_root));
288 RebaseSourceAbsolutePath("//a/b/foo/bar", SourceDir("//a/b/")));
289 EXPECT_EQ("foo/bar/",
290 RebaseSourceAbsolutePath("//a/b/foo/bar/", SourceDir("//a/b/")));
291 278
292 // One could argue about this case. Since the input doesn't have a slash it 279 // 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 280 // 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 281 // simpler. However, since it matches the output directory's name, we could
295 // potentially infer that it's the same and return "." for this. 282 // potentially infer that it's the same and return "." for this.
296 EXPECT_EQ("../bar", 283 EXPECT_EQ("../bar", RebasePath("//foo/bar", SourceDir("//foo/bar/"),
297 RebaseSourceAbsolutePath("//foo/bar", SourceDir("//foo/bar/"))); 284 source_root));
285
286 // Check when only |input| is system-absolute
287 EXPECT_EQ("foo", RebasePath("/source/root/foo", SourceDir("//"),
288 base::StringPiece("/source/root")));
289 EXPECT_EQ("foo/", RebasePath("/source/root/foo/", SourceDir("//"),
290 base::StringPiece("/source/root")));
291 EXPECT_EQ("../../builddir/Out/Debug",
292 RebasePath("/builddir/Out/Debug", SourceDir("//"),
293 base::StringPiece("/source/root")));
294 EXPECT_EQ("../../../builddir/Out/Debug",
295 RebasePath("/builddir/Out/Debug", SourceDir("//"),
296 base::StringPiece("/source/root/foo")));
297 EXPECT_EQ("../../../builddir/Out/Debug/",
298 RebasePath("/builddir/Out/Debug/", SourceDir("//"),
299 base::StringPiece("/source/root/foo")));
300 EXPECT_EQ("../../path/to/foo",
301 RebasePath("/path/to/foo", SourceDir("//"),
302 base::StringPiece("/source/root")));
303 EXPECT_EQ("../../../path/to/foo",
304 RebasePath("/path/to/foo", SourceDir("//a"),
305 base::StringPiece("/source/root")));
306 EXPECT_EQ("../../../../path/to/foo",
307 RebasePath("/path/to/foo", SourceDir("//a/b"),
308 base::StringPiece("/source/root")));
309
310 // Check when only |dest_dir| is system-absolute.
311 EXPECT_EQ(".",
312 RebasePath("//", SourceDir("/source/root"),
313 base::StringPiece("/source/root")));
314 EXPECT_EQ("foo",
315 RebasePath("//foo", SourceDir("/source/root"),
316 base::StringPiece("/source/root")));
317 EXPECT_EQ("../foo",
318 RebasePath("//foo", SourceDir("/source/root/bar"),
319 base::StringPiece("/source/root")));
320 EXPECT_EQ("../../../source/root/foo",
321 RebasePath("//foo", SourceDir("/other/source/root"),
322 base::StringPiece("/source/root")));
323 EXPECT_EQ("../../../../source/root/foo",
324 RebasePath("//foo", SourceDir("/other/source/root/bar"),
325 base::StringPiece("/source/root")));
326
327 // Check when |input| and |dest_dir| are both system-absolute. Also,
328 // in this case |source_root| is never used so set it to a dummy
329 // value.
330 EXPECT_EQ("foo",
331 RebasePath("/source/root/foo", SourceDir("/source/root"),
332 base::StringPiece("/x/y/z")));
333 EXPECT_EQ("foo/",
334 RebasePath("/source/root/foo/", SourceDir("/source/root"),
335 base::StringPiece("/x/y/z")));
336 EXPECT_EQ("../../builddir/Out/Debug",
337 RebasePath("/builddir/Out/Debug",SourceDir("/source/root"),
338 base::StringPiece("/x/y/z")));
339 EXPECT_EQ("../../../builddir/Out/Debug",
340 RebasePath("/builddir/Out/Debug", SourceDir("/source/root/foo"),
341 base::StringPiece("/source/root/foo")));
342 EXPECT_EQ("../../../builddir/Out/Debug/",
343 RebasePath("/builddir/Out/Debug/", SourceDir("/source/root/foo"),
344 base::StringPiece("/source/root/foo")));
345 EXPECT_EQ("../../path/to/foo",
346 RebasePath("/path/to/foo", SourceDir("/source/root"),
347 base::StringPiece("/x/y/z")));
348 EXPECT_EQ("../../../path/to/foo",
349 RebasePath("/path/to/foo", SourceDir("/source/root/a"),
350 base::StringPiece("/x/y/z")));
351 EXPECT_EQ("../../../../path/to/foo",
352 RebasePath("/path/to/foo", SourceDir("/source/root/a/b"),
353 base::StringPiece("/x/y/z")));
354
298 } 355 }
299 356
300 TEST(FilesystemUtils, DirectoryWithNoLastSlash) { 357 TEST(FilesystemUtils, DirectoryWithNoLastSlash) {
301 EXPECT_EQ("", DirectoryWithNoLastSlash(SourceDir())); 358 EXPECT_EQ("", DirectoryWithNoLastSlash(SourceDir()));
302 EXPECT_EQ("/.", DirectoryWithNoLastSlash(SourceDir("/"))); 359 EXPECT_EQ("/.", DirectoryWithNoLastSlash(SourceDir("/")));
303 EXPECT_EQ("//.", DirectoryWithNoLastSlash(SourceDir("//"))); 360 EXPECT_EQ("//.", DirectoryWithNoLastSlash(SourceDir("//")));
304 EXPECT_EQ("//foo", DirectoryWithNoLastSlash(SourceDir("//foo/"))); 361 EXPECT_EQ("//foo", DirectoryWithNoLastSlash(SourceDir("//foo/")));
305 EXPECT_EQ("/bar", DirectoryWithNoLastSlash(SourceDir("/bar/"))); 362 EXPECT_EQ("/bar", DirectoryWithNoLastSlash(SourceDir("/bar/")));
306 } 363 }
307 364
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value()); 557 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value());
501 EXPECT_EQ("//obj/", 558 EXPECT_EQ("//obj/",
502 GetOutputDirForSourceDir(&settings, SourceDir("//")).value()); 559 GetOutputDirForSourceDir(&settings, SourceDir("//")).value());
503 EXPECT_EQ("obj/", 560 EXPECT_EQ("obj/",
504 GetOutputDirForSourceDirAsOutputFile( 561 GetOutputDirForSourceDirAsOutputFile(
505 &settings, SourceDir("//")).value()); 562 &settings, SourceDir("//")).value());
506 EXPECT_EQ("gen/", 563 EXPECT_EQ("gen/",
507 GetGenDirForSourceDirAsOutputFile( 564 GetGenDirForSourceDirAsOutputFile(
508 &settings, SourceDir("//")).value()); 565 &settings, SourceDir("//")).value());
509 } 566 }
OLDNEW
« no previous file with comments | « tools/gn/filesystem_utils.cc ('k') | tools/gn/function_exec_script.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698