Add the ability to code generated prepopulated static nested structs
[chromium-blink-merge.git] / tools / gn / filesystem_utils_unittest.cc
blob14bc9515307e3767a108ff2f358a7789b68eb7a5
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/files/file_path.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "build/build_config.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "tools/gn/filesystem_utils.h"
11 #include "tools/gn/target.h"
13 TEST(FilesystemUtils, FileExtensionOffset) {
14 EXPECT_EQ(std::string::npos, FindExtensionOffset(""));
15 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo/bar/baz"));
16 EXPECT_EQ(4u, FindExtensionOffset("foo."));
17 EXPECT_EQ(4u, FindExtensionOffset("f.o.bar"));
18 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo.bar/"));
19 EXPECT_EQ(std::string::npos, FindExtensionOffset("foo.bar/baz"));
22 TEST(FilesystemUtils, FindExtension) {
23 std::string input;
24 EXPECT_EQ("", FindExtension(&input).as_string());
25 input = "foo/bar/baz";
26 EXPECT_EQ("", FindExtension(&input).as_string());
27 input = "foo.";
28 EXPECT_EQ("", FindExtension(&input).as_string());
29 input = "f.o.bar";
30 EXPECT_EQ("bar", FindExtension(&input).as_string());
31 input = "foo.bar/";
32 EXPECT_EQ("", FindExtension(&input).as_string());
33 input = "foo.bar/baz";
34 EXPECT_EQ("", FindExtension(&input).as_string());
37 TEST(FilesystemUtils, FindFilenameOffset) {
38 EXPECT_EQ(0u, FindFilenameOffset(""));
39 EXPECT_EQ(0u, FindFilenameOffset("foo"));
40 EXPECT_EQ(4u, FindFilenameOffset("foo/"));
41 EXPECT_EQ(4u, FindFilenameOffset("foo/bar"));
44 TEST(FilesystemUtils, RemoveFilename) {
45 std::string s;
47 RemoveFilename(&s);
48 EXPECT_STREQ("", s.c_str());
50 s = "foo";
51 RemoveFilename(&s);
52 EXPECT_STREQ("", s.c_str());
54 s = "/";
55 RemoveFilename(&s);
56 EXPECT_STREQ("/", s.c_str());
58 s = "foo/bar";
59 RemoveFilename(&s);
60 EXPECT_STREQ("foo/", s.c_str());
62 s = "foo/bar/baz.cc";
63 RemoveFilename(&s);
64 EXPECT_STREQ("foo/bar/", s.c_str());
67 TEST(FilesystemUtils, FindDir) {
68 std::string input;
69 EXPECT_EQ("", FindDir(&input));
70 input = "/";
71 EXPECT_EQ("/", FindDir(&input));
72 input = "foo/";
73 EXPECT_EQ("foo/", FindDir(&input));
74 input = "foo/bar/baz";
75 EXPECT_EQ("foo/bar/", FindDir(&input));
78 TEST(FilesystemUtils, FindLastDirComponent) {
79 SourceDir empty;
80 EXPECT_EQ("", FindLastDirComponent(empty));
82 SourceDir root("/");
83 EXPECT_EQ("", FindLastDirComponent(root));
85 SourceDir srcroot("//");
86 EXPECT_EQ("", FindLastDirComponent(srcroot));
88 SourceDir regular1("//foo/");
89 EXPECT_EQ("foo", FindLastDirComponent(regular1));
91 SourceDir regular2("//foo/bar/");
92 EXPECT_EQ("bar", FindLastDirComponent(regular2));
95 TEST(FilesystemUtils, EnsureStringIsInOutputDir) {
96 SourceDir output_dir("//out/Debug/");
98 // Some outside.
99 Err err;
100 EXPECT_FALSE(EnsureStringIsInOutputDir(output_dir, "//foo", nullptr, &err));
101 EXPECT_TRUE(err.has_error());
102 err = Err();
103 EXPECT_FALSE(
104 EnsureStringIsInOutputDir(output_dir, "//out/Debugit", nullptr, &err));
105 EXPECT_TRUE(err.has_error());
107 // Some inside.
108 err = Err();
109 EXPECT_TRUE(
110 EnsureStringIsInOutputDir(output_dir, "//out/Debug/", nullptr, &err));
111 EXPECT_FALSE(err.has_error());
112 EXPECT_TRUE(
113 EnsureStringIsInOutputDir(output_dir, "//out/Debug/foo", nullptr, &err));
114 EXPECT_FALSE(err.has_error());
116 // Pattern but no template expansions are allowed.
117 EXPECT_FALSE(EnsureStringIsInOutputDir(output_dir, "{{source_gen_dir}}",
118 nullptr, &err));
119 EXPECT_TRUE(err.has_error());
122 TEST(FilesystemUtils, IsPathAbsolute) {
123 EXPECT_TRUE(IsPathAbsolute("/foo/bar"));
124 EXPECT_TRUE(IsPathAbsolute("/"));
125 EXPECT_FALSE(IsPathAbsolute(""));
126 EXPECT_FALSE(IsPathAbsolute("//"));
127 EXPECT_FALSE(IsPathAbsolute("//foo/bar"));
129 #if defined(OS_WIN)
130 EXPECT_TRUE(IsPathAbsolute("C:/foo"));
131 EXPECT_TRUE(IsPathAbsolute("C:/"));
132 EXPECT_TRUE(IsPathAbsolute("C:\\foo"));
133 EXPECT_TRUE(IsPathAbsolute("C:\\"));
134 EXPECT_TRUE(IsPathAbsolute("/C:/foo"));
135 EXPECT_TRUE(IsPathAbsolute("/C:\\foo"));
136 #endif
139 TEST(FilesystemUtils, MakeAbsolutePathRelativeIfPossible) {
140 std::string dest;
142 #if defined(OS_WIN)
143 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\base\\foo",
144 &dest));
145 EXPECT_EQ("//foo", dest);
146 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("C:\\base", "/C:/base/foo",
147 &dest));
148 EXPECT_EQ("//foo", dest);
149 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("c:\\base", "C:\\base\\foo\\",
150 &dest));
151 EXPECT_EQ("//foo\\", dest);
153 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base", "C:\\ba", &dest));
154 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("C:\\base",
155 "C:\\/notbase/foo",
156 &dest));
157 #else
159 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base/foo/", &dest));
160 EXPECT_EQ("//foo/", dest);
161 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base", "/base/foo", &dest));
162 EXPECT_EQ("//foo", dest);
163 EXPECT_TRUE(MakeAbsolutePathRelativeIfPossible("/base/", "/base/foo/",
164 &dest));
165 EXPECT_EQ("//foo/", dest);
167 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/ba", &dest));
168 EXPECT_FALSE(MakeAbsolutePathRelativeIfPossible("/base", "/notbase/foo",
169 &dest));
170 #endif
173 TEST(FilesystemUtils, NormalizePath) {
174 std::string input;
176 NormalizePath(&input);
177 EXPECT_EQ("", input);
179 input = "foo/bar.txt";
180 NormalizePath(&input);
181 EXPECT_EQ("foo/bar.txt", input);
183 input = ".";
184 NormalizePath(&input);
185 EXPECT_EQ("", input);
187 input = "..";
188 NormalizePath(&input);
189 EXPECT_EQ("..", input);
191 input = "foo//bar";
192 NormalizePath(&input);
193 EXPECT_EQ("foo/bar", input);
195 input = "//foo";
196 NormalizePath(&input);
197 EXPECT_EQ("//foo", input);
199 input = "foo/..//bar";
200 NormalizePath(&input);
201 EXPECT_EQ("bar", input);
203 input = "foo/../../bar";
204 NormalizePath(&input);
205 EXPECT_EQ("../bar", input);
207 input = "/../foo"; // Don't go aboe the root dir.
208 NormalizePath(&input);
209 EXPECT_EQ("/foo", input);
211 input = "//../foo"; // Don't go above the root dir.
212 NormalizePath(&input);
213 EXPECT_EQ("//foo", input);
215 input = "../foo";
216 NormalizePath(&input);
217 EXPECT_EQ("../foo", input);
219 input = "..";
220 NormalizePath(&input);
221 EXPECT_EQ("..", input);
223 input = "./././.";
224 NormalizePath(&input);
225 EXPECT_EQ("", input);
227 input = "../../..";
228 NormalizePath(&input);
229 EXPECT_EQ("../../..", input);
231 input = "../";
232 NormalizePath(&input);
233 EXPECT_EQ("../", input);
235 // Backslash normalization.
236 input = "foo\\..\\..\\bar";
237 NormalizePath(&input);
238 EXPECT_EQ("../bar", input);
240 // Trailing slashes should get preserved.
241 input = "//foo/bar/";
242 NormalizePath(&input);
243 EXPECT_EQ("//foo/bar/", input);
246 TEST(FilesystemUtils, RebasePath) {
247 base::StringPiece source_root("/source/root");
249 // Degenerate case.
250 EXPECT_EQ(".", RebasePath("//", SourceDir("//"), source_root));
251 EXPECT_EQ(".", RebasePath("//foo/bar/", SourceDir("//foo/bar/"),
252 source_root));
254 // Going up the tree.
255 EXPECT_EQ("../foo", RebasePath("//foo", SourceDir("//bar/"), source_root));
256 EXPECT_EQ("../foo/", RebasePath("//foo/", SourceDir("//bar/"), source_root));
257 EXPECT_EQ("../../foo", RebasePath("//foo", SourceDir("//bar/moo"),
258 source_root));
259 EXPECT_EQ("../../foo/", RebasePath("//foo/", SourceDir("//bar/moo"),
260 source_root));
262 // Going down the tree.
263 EXPECT_EQ("foo/bar", RebasePath("//foo/bar", SourceDir("//"), source_root));
264 EXPECT_EQ("foo/bar/", RebasePath("//foo/bar/", SourceDir("//"),
265 source_root));
267 // Going up and down the tree.
268 EXPECT_EQ("../../foo/bar", RebasePath("//foo/bar", SourceDir("//a/b/"),
269 source_root));
270 EXPECT_EQ("../../foo/bar/", RebasePath("//foo/bar/", SourceDir("//a/b/"),
271 source_root));
273 // Sharing prefix.
274 EXPECT_EQ("foo", RebasePath("//a/foo", SourceDir("//a/"), source_root));
275 EXPECT_EQ("foo/", RebasePath("//a/foo/", SourceDir("//a/"), source_root));
276 EXPECT_EQ("foo", RebasePath("//a/b/foo", SourceDir("//a/b/"), source_root));
277 EXPECT_EQ("foo/", RebasePath("//a/b/foo/", SourceDir("//a/b/"),
278 source_root));
279 EXPECT_EQ("foo/bar", RebasePath("//a/b/foo/bar", SourceDir("//a/b/"),
280 source_root));
281 EXPECT_EQ("foo/bar/", RebasePath("//a/b/foo/bar/", SourceDir("//a/b/"),
282 source_root));
284 // One could argue about this case. Since the input doesn't have a slash it
285 // would normally not be treated like a directory and we'd go up, which is
286 // simpler. However, since it matches the output directory's name, we could
287 // potentially infer that it's the same and return "." for this.
288 EXPECT_EQ("../bar", RebasePath("//foo/bar", SourceDir("//foo/bar/"),
289 source_root));
291 // Check when only |input| is system-absolute
292 EXPECT_EQ("foo", RebasePath("/source/root/foo", SourceDir("//"),
293 base::StringPiece("/source/root")));
294 EXPECT_EQ("foo/", RebasePath("/source/root/foo/", SourceDir("//"),
295 base::StringPiece("/source/root")));
296 EXPECT_EQ("../../builddir/Out/Debug",
297 RebasePath("/builddir/Out/Debug", SourceDir("//"),
298 base::StringPiece("/source/root")));
299 EXPECT_EQ("../../../builddir/Out/Debug",
300 RebasePath("/builddir/Out/Debug", SourceDir("//"),
301 base::StringPiece("/source/root/foo")));
302 EXPECT_EQ("../../../builddir/Out/Debug/",
303 RebasePath("/builddir/Out/Debug/", SourceDir("//"),
304 base::StringPiece("/source/root/foo")));
305 EXPECT_EQ("../../path/to/foo",
306 RebasePath("/path/to/foo", SourceDir("//"),
307 base::StringPiece("/source/root")));
308 EXPECT_EQ("../../../path/to/foo",
309 RebasePath("/path/to/foo", SourceDir("//a"),
310 base::StringPiece("/source/root")));
311 EXPECT_EQ("../../../../path/to/foo",
312 RebasePath("/path/to/foo", SourceDir("//a/b"),
313 base::StringPiece("/source/root")));
315 // Check when only |dest_dir| is system-absolute.
316 EXPECT_EQ(".",
317 RebasePath("//", SourceDir("/source/root"),
318 base::StringPiece("/source/root")));
319 EXPECT_EQ("foo",
320 RebasePath("//foo", SourceDir("/source/root"),
321 base::StringPiece("/source/root")));
322 EXPECT_EQ("../foo",
323 RebasePath("//foo", SourceDir("/source/root/bar"),
324 base::StringPiece("/source/root")));
325 EXPECT_EQ("../../../source/root/foo",
326 RebasePath("//foo", SourceDir("/other/source/root"),
327 base::StringPiece("/source/root")));
328 EXPECT_EQ("../../../../source/root/foo",
329 RebasePath("//foo", SourceDir("/other/source/root/bar"),
330 base::StringPiece("/source/root")));
332 // Check when |input| and |dest_dir| are both system-absolute. Also,
333 // in this case |source_root| is never used so set it to a dummy
334 // value.
335 EXPECT_EQ("foo",
336 RebasePath("/source/root/foo", SourceDir("/source/root"),
337 base::StringPiece("/x/y/z")));
338 EXPECT_EQ("foo/",
339 RebasePath("/source/root/foo/", SourceDir("/source/root"),
340 base::StringPiece("/x/y/z")));
341 EXPECT_EQ("../../builddir/Out/Debug",
342 RebasePath("/builddir/Out/Debug",SourceDir("/source/root"),
343 base::StringPiece("/x/y/z")));
344 EXPECT_EQ("../../../builddir/Out/Debug",
345 RebasePath("/builddir/Out/Debug", SourceDir("/source/root/foo"),
346 base::StringPiece("/source/root/foo")));
347 EXPECT_EQ("../../../builddir/Out/Debug/",
348 RebasePath("/builddir/Out/Debug/", SourceDir("/source/root/foo"),
349 base::StringPiece("/source/root/foo")));
350 EXPECT_EQ("../../path/to/foo",
351 RebasePath("/path/to/foo", SourceDir("/source/root"),
352 base::StringPiece("/x/y/z")));
353 EXPECT_EQ("../../../path/to/foo",
354 RebasePath("/path/to/foo", SourceDir("/source/root/a"),
355 base::StringPiece("/x/y/z")));
356 EXPECT_EQ("../../../../path/to/foo",
357 RebasePath("/path/to/foo", SourceDir("/source/root/a/b"),
358 base::StringPiece("/x/y/z")));
362 TEST(FilesystemUtils, DirectoryWithNoLastSlash) {
363 EXPECT_EQ("", DirectoryWithNoLastSlash(SourceDir()));
364 EXPECT_EQ("/.", DirectoryWithNoLastSlash(SourceDir("/")));
365 EXPECT_EQ("//.", DirectoryWithNoLastSlash(SourceDir("//")));
366 EXPECT_EQ("//foo", DirectoryWithNoLastSlash(SourceDir("//foo/")));
367 EXPECT_EQ("/bar", DirectoryWithNoLastSlash(SourceDir("/bar/")));
370 TEST(FilesystemUtils, SourceDirForPath) {
371 #if defined(OS_WIN)
372 base::FilePath root(L"C:\\source\\foo\\");
373 EXPECT_EQ("/C:/foo/bar/", SourceDirForPath(root,
374 base::FilePath(L"C:\\foo\\bar")).value());
375 EXPECT_EQ("/", SourceDirForPath(root,
376 base::FilePath(L"/")).value());
377 EXPECT_EQ("//", SourceDirForPath(root,
378 base::FilePath(L"C:\\source\\foo")).value());
379 EXPECT_EQ("//bar/", SourceDirForPath(root,
380 base::FilePath(L"C:\\source\\foo\\bar\\")). value());
381 EXPECT_EQ("//bar/baz/", SourceDirForPath(root,
382 base::FilePath(L"C:\\source\\foo\\bar\\baz")).value());
384 // Should be case-and-slash-insensitive.
385 EXPECT_EQ("//baR/", SourceDirForPath(root,
386 base::FilePath(L"c:/SOURCE\\Foo/baR/")).value());
388 // Some "weird" Windows paths.
389 EXPECT_EQ("/foo/bar/", SourceDirForPath(root,
390 base::FilePath(L"/foo/bar/")).value());
391 EXPECT_EQ("/C:/foo/bar/", SourceDirForPath(root,
392 base::FilePath(L"C:foo/bar/")).value());
394 // Also allow absolute GN-style Windows paths.
395 EXPECT_EQ("/C:/foo/bar/", SourceDirForPath(root,
396 base::FilePath(L"/C:/foo/bar")).value());
397 EXPECT_EQ("//bar/", SourceDirForPath(root,
398 base::FilePath(L"/C:/source/foo/bar")).value());
400 // Empty source dir.
401 base::FilePath empty;
402 EXPECT_EQ(
403 "/C:/source/foo/",
404 SourceDirForPath(empty, base::FilePath(L"C:\\source\\foo")).value());
405 #else
406 base::FilePath root("/source/foo/");
407 EXPECT_EQ("/foo/bar/", SourceDirForPath(root,
408 base::FilePath("/foo/bar/")).value());
409 EXPECT_EQ("/", SourceDirForPath(root,
410 base::FilePath("/")).value());
411 EXPECT_EQ("//", SourceDirForPath(root,
412 base::FilePath("/source/foo")).value());
413 EXPECT_EQ("//bar/", SourceDirForPath(root,
414 base::FilePath("/source/foo/bar/")).value());
415 EXPECT_EQ("//bar/baz/", SourceDirForPath(root,
416 base::FilePath("/source/foo/bar/baz/")).value());
418 // Should be case-sensitive.
419 EXPECT_EQ("/SOURCE/foo/bar/", SourceDirForPath(root,
420 base::FilePath("/SOURCE/foo/bar/")).value());
422 // Empty source dir.
423 base::FilePath empty;
424 EXPECT_EQ("/source/foo/",
425 SourceDirForPath(empty, base::FilePath("/source/foo")).value());
426 #endif
429 TEST(FilesystemUtils, GetToolchainDirs) {
430 BuildSettings build_settings;
431 build_settings.SetBuildDir(SourceDir("//out/Debug/"));
433 // The default toolchain.
434 Settings default_settings(&build_settings, "");
435 Label default_toolchain_label(SourceDir("//toolchain/"), "default");
436 default_settings.set_toolchain_label(default_toolchain_label);
437 default_settings.set_default_toolchain_label(default_toolchain_label);
439 // Default toolchain out dir.
440 EXPECT_EQ("//out/Debug/",
441 GetToolchainOutputDir(&default_settings).value());
442 EXPECT_EQ("//out/Debug/",
443 GetToolchainOutputDir(&build_settings, default_toolchain_label,
444 true).value());
446 // Default toolchain gen dir.
447 EXPECT_EQ("//out/Debug/gen/",
448 GetToolchainGenDir(&default_settings).value());
449 EXPECT_EQ("gen/",
450 GetToolchainGenDirAsOutputFile(&default_settings).value());
451 EXPECT_EQ("//out/Debug/gen/",
452 GetToolchainGenDir(&build_settings, default_toolchain_label,
453 true).value());
455 // Check a secondary toolchain.
456 Settings other_settings(&build_settings, "two/");
457 Label other_toolchain_label(SourceDir("//toolchain/"), "two");
458 default_settings.set_toolchain_label(other_toolchain_label);
459 default_settings.set_default_toolchain_label(default_toolchain_label);
461 // Secondary toolchain out dir.
462 EXPECT_EQ("//out/Debug/two/",
463 GetToolchainOutputDir(&other_settings).value());
464 EXPECT_EQ("//out/Debug/two/",
465 GetToolchainOutputDir(&build_settings, other_toolchain_label,
466 false).value());
468 // Secondary toolchain gen dir.
469 EXPECT_EQ("//out/Debug/two/gen/",
470 GetToolchainGenDir(&other_settings).value());
471 EXPECT_EQ("two/gen/",
472 GetToolchainGenDirAsOutputFile(&other_settings).value());
473 EXPECT_EQ("//out/Debug/two/gen/",
474 GetToolchainGenDir(&build_settings, other_toolchain_label,
475 false).value());
478 TEST(FilesystemUtils, GetOutDirForSourceDir) {
479 BuildSettings build_settings;
480 build_settings.SetBuildDir(SourceDir("//out/Debug/"));
482 // Test the default toolchain.
483 Settings default_settings(&build_settings, "");
484 EXPECT_EQ("//out/Debug/obj/",
485 GetOutputDirForSourceDir(
486 &default_settings, SourceDir("//")).value());
487 EXPECT_EQ("obj/",
488 GetOutputDirForSourceDirAsOutputFile(
489 &default_settings, SourceDir("//")).value());
491 EXPECT_EQ("//out/Debug/obj/foo/bar/",
492 GetOutputDirForSourceDir(
493 &default_settings, SourceDir("//foo/bar/")).value());
494 EXPECT_EQ("obj/foo/bar/",
495 GetOutputDirForSourceDirAsOutputFile(
496 &default_settings, SourceDir("//foo/bar/")).value());
498 // Secondary toolchain.
499 Settings other_settings(&build_settings, "two/");
500 EXPECT_EQ("//out/Debug/two/obj/",
501 GetOutputDirForSourceDir(
502 &other_settings, SourceDir("//")).value());
503 EXPECT_EQ("two/obj/",
504 GetOutputDirForSourceDirAsOutputFile(
505 &other_settings, SourceDir("//")).value());
507 EXPECT_EQ("//out/Debug/two/obj/foo/bar/",
508 GetOutputDirForSourceDir(&other_settings,
509 SourceDir("//foo/bar/")).value());
510 EXPECT_EQ("two/obj/foo/bar/",
511 GetOutputDirForSourceDirAsOutputFile(
512 &other_settings, SourceDir("//foo/bar/")).value());
514 // Absolute source path
515 EXPECT_EQ("//out/Debug/obj/ABS_PATH/abs/",
516 GetOutputDirForSourceDir(
517 &default_settings, SourceDir("/abs")).value());
518 EXPECT_EQ("obj/ABS_PATH/abs/",
519 GetOutputDirForSourceDirAsOutputFile(
520 &default_settings, SourceDir("/abs")).value());
521 #if defined(OS_WIN)
522 EXPECT_EQ("//out/Debug/obj/ABS_PATH/C/abs/",
523 GetOutputDirForSourceDir(
524 &default_settings, SourceDir("/C:/abs")).value());
525 EXPECT_EQ("obj/ABS_PATH/C/abs/",
526 GetOutputDirForSourceDirAsOutputFile(
527 &default_settings, SourceDir("/C:/abs")).value());
528 #endif
531 TEST(FilesystemUtils, GetGenDirForSourceDir) {
532 BuildSettings build_settings;
533 build_settings.SetBuildDir(SourceDir("//out/Debug/"));
535 // Test the default toolchain.
536 Settings default_settings(&build_settings, "");
537 EXPECT_EQ("//out/Debug/gen/",
538 GetGenDirForSourceDir(
539 &default_settings, SourceDir("//")).value());
540 EXPECT_EQ("gen/",
541 GetGenDirForSourceDirAsOutputFile(
542 &default_settings, SourceDir("//")).value());
544 EXPECT_EQ("//out/Debug/gen/foo/bar/",
545 GetGenDirForSourceDir(
546 &default_settings, SourceDir("//foo/bar/")).value());
547 EXPECT_EQ("gen/foo/bar/",
548 GetGenDirForSourceDirAsOutputFile(
549 &default_settings, SourceDir("//foo/bar/")).value());
551 // Secondary toolchain.
552 Settings other_settings(&build_settings, "two/");
553 EXPECT_EQ("//out/Debug/two/gen/",
554 GetGenDirForSourceDir(
555 &other_settings, SourceDir("//")).value());
556 EXPECT_EQ("two/gen/",
557 GetGenDirForSourceDirAsOutputFile(
558 &other_settings, SourceDir("//")).value());
560 EXPECT_EQ("//out/Debug/two/gen/foo/bar/",
561 GetGenDirForSourceDir(
562 &other_settings, SourceDir("//foo/bar/")).value());
563 EXPECT_EQ("two/gen/foo/bar/",
564 GetGenDirForSourceDirAsOutputFile(
565 &other_settings, SourceDir("//foo/bar/")).value());
568 TEST(FilesystemUtils, GetTargetDirs) {
569 BuildSettings build_settings;
570 build_settings.SetBuildDir(SourceDir("//out/Debug/"));
571 Settings settings(&build_settings, "");
573 Target a(&settings, Label(SourceDir("//foo/bar/"), "baz"));
574 EXPECT_EQ("//out/Debug/obj/foo/bar/", GetTargetOutputDir(&a).value());
575 EXPECT_EQ("obj/foo/bar/", GetTargetOutputDirAsOutputFile(&a).value());
576 EXPECT_EQ("//out/Debug/gen/foo/bar/", GetTargetGenDir(&a).value());
577 EXPECT_EQ("gen/foo/bar/", GetTargetGenDirAsOutputFile(&a).value());
580 // Tests handling of output dirs when build dir is the same as the root.
581 TEST(FilesystemUtils, GetDirForEmptyBuildDir) {
582 BuildSettings build_settings;
583 build_settings.SetBuildDir(SourceDir("//"));
584 Settings settings(&build_settings, "");
586 EXPECT_EQ("//", GetToolchainOutputDir(&settings).value());
587 EXPECT_EQ("//gen/", GetToolchainGenDir(&settings).value());
588 EXPECT_EQ("gen/", GetToolchainGenDirAsOutputFile(&settings).value());
589 EXPECT_EQ("//obj/",
590 GetOutputDirForSourceDir(&settings, SourceDir("//")).value());
591 EXPECT_EQ("obj/",
592 GetOutputDirForSourceDirAsOutputFile(
593 &settings, SourceDir("//")).value());
594 EXPECT_EQ("gen/",
595 GetGenDirForSourceDirAsOutputFile(
596 &settings, SourceDir("//")).value());