[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / gn / path_output_unittest.cc
blob131f55ec5faa2245caac72992709e121fbfbabbc
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 <sstream>
7 #include "base/files/file_path.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "tools/gn/output_file.h"
10 #include "tools/gn/path_output.h"
11 #include "tools/gn/source_dir.h"
12 #include "tools/gn/source_file.h"
14 TEST(PathOutput, Basic) {
15 SourceDir build_dir("//out/Debug/");
16 base::StringPiece source_root("/source/root");
17 PathOutput writer(build_dir, source_root, ESCAPE_NONE);
19 // Normal source-root path.
20 std::ostringstream out;
21 writer.WriteFile(out, SourceFile("//foo/bar.cc"));
22 EXPECT_EQ("../../foo/bar.cc", out.str());
25 // File in the root dir.
26 std::ostringstream out;
27 writer.WriteFile(out, SourceFile("//foo.cc"));
28 EXPECT_EQ("../../foo.cc", out.str());
31 // Files in the output dir.
32 std::ostringstream out;
33 writer.WriteFile(out, SourceFile("//out/Debug/foo.cc"));
34 out << " ";
35 writer.WriteFile(out, SourceFile("//out/Debug/bar/baz.cc"));
36 EXPECT_EQ("foo.cc bar/baz.cc", out.str());
38 #if defined(OS_WIN)
40 // System-absolute path.
41 std::ostringstream out;
42 writer.WriteFile(out, SourceFile("/C:/foo/bar.cc"));
43 EXPECT_EQ("C:/foo/bar.cc", out.str());
45 #else
47 // System-absolute path.
48 std::ostringstream out;
49 writer.WriteFile(out, SourceFile("/foo/bar.cc"));
50 EXPECT_EQ("/foo/bar.cc", out.str());
52 #endif
55 // Same as basic but the output dir is the root.
56 TEST(PathOutput, BasicInRoot) {
57 SourceDir build_dir("//");
58 base::StringPiece source_root("/source/root");
59 PathOutput writer(build_dir, source_root, ESCAPE_NONE);
61 // Normal source-root path.
62 std::ostringstream out;
63 writer.WriteFile(out, SourceFile("//foo/bar.cc"));
64 EXPECT_EQ("foo/bar.cc", out.str());
67 // File in the root dir.
68 std::ostringstream out;
69 writer.WriteFile(out, SourceFile("//foo.cc"));
70 EXPECT_EQ("foo.cc", out.str());
74 TEST(PathOutput, NinjaEscaping) {
75 SourceDir build_dir("//out/Debug/");
76 base::StringPiece source_root("/source/root");
77 PathOutput writer(build_dir, source_root, ESCAPE_NINJA);
79 // Spaces and $ in filenames.
80 std::ostringstream out;
81 writer.WriteFile(out, SourceFile("//foo/foo bar$.cc"));
82 EXPECT_EQ("../../foo/foo$ bar$$.cc", out.str());
85 // Not other weird stuff
86 std::ostringstream out;
87 writer.WriteFile(out, SourceFile("//foo/\"foo\\bar\".cc"));
88 EXPECT_EQ("../../foo/\"foo\\bar\".cc", out.str());
92 TEST(PathOutput, NinjaForkEscaping) {
93 SourceDir build_dir("//out/Debug/");
94 base::StringPiece source_root("/source/root");
95 PathOutput writer(build_dir, source_root, ESCAPE_NINJA_COMMAND);
97 // Spaces in filenames should get quoted on Windows.
98 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
100 std::ostringstream out;
101 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
102 EXPECT_EQ("\"../../foo/foo$ bar.cc\"", out.str());
105 // Spaces in filenames should get escaped on Posix.
106 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
108 std::ostringstream out;
109 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
110 EXPECT_EQ("../../foo/foo\\$ bar.cc", out.str());
113 // Quotes should get blackslash-escaped on Windows and Posix.
114 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
116 std::ostringstream out;
117 writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc"));
118 // Our Windows code currently quotes the whole thing in this case for
119 // code simplicity, even though it's strictly unnecessary. This might
120 // change in the future.
121 EXPECT_EQ("\"../../foo/\\\"foobar\\\".cc\"", out.str());
123 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
125 std::ostringstream out;
126 writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc"));
127 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str());
131 // Backslashes should get escaped on non-Windows and preserved on Windows.
132 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
134 std::ostringstream out;
135 writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
136 EXPECT_EQ("../../foo\\bar.cc", out.str());
138 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
140 std::ostringstream out;
141 writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
142 EXPECT_EQ("../../foo\\\\bar.cc", out.str());
146 TEST(PathOutput, InhibitQuoting) {
147 SourceDir build_dir("//out/Debug/");
148 base::StringPiece source_root("/source/root");
149 PathOutput writer(build_dir, source_root, ESCAPE_NINJA_COMMAND);
150 writer.set_inhibit_quoting(true);
152 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
154 // We should get unescaped spaces in the output with no quotes.
155 std::ostringstream out;
156 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
157 EXPECT_EQ("../../foo/foo$ bar.cc", out.str());
160 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
162 // Escapes the space.
163 std::ostringstream out;
164 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
165 EXPECT_EQ("../../foo/foo\\$ bar.cc", out.str());
169 TEST(PathOutput, WriteDir) {
171 SourceDir build_dir("//out/Debug/");
172 base::StringPiece source_root("/source/root");
173 PathOutput writer(build_dir, source_root, ESCAPE_NINJA);
175 std::ostringstream out;
176 writer.WriteDir(out, SourceDir("//foo/bar/"),
177 PathOutput::DIR_INCLUDE_LAST_SLASH);
178 EXPECT_EQ("../../foo/bar/", out.str());
181 std::ostringstream out;
182 writer.WriteDir(out, SourceDir("//foo/bar/"),
183 PathOutput::DIR_NO_LAST_SLASH);
184 EXPECT_EQ("../../foo/bar", out.str());
187 // Output source root dir.
189 std::ostringstream out;
190 writer.WriteDir(out, SourceDir("//"),
191 PathOutput::DIR_INCLUDE_LAST_SLASH);
192 EXPECT_EQ("../../", out.str());
195 std::ostringstream out;
196 writer.WriteDir(out, SourceDir("//"),
197 PathOutput::DIR_NO_LAST_SLASH);
198 EXPECT_EQ("../..", out.str());
201 // Output system root dir.
203 std::ostringstream out;
204 writer.WriteDir(out, SourceDir("/"),
205 PathOutput::DIR_INCLUDE_LAST_SLASH);
206 EXPECT_EQ("/", out.str());
209 std::ostringstream out;
210 writer.WriteDir(out, SourceDir("/"),
211 PathOutput::DIR_INCLUDE_LAST_SLASH);
212 EXPECT_EQ("/", out.str());
215 std::ostringstream out;
216 writer.WriteDir(out, SourceDir("/"),
217 PathOutput::DIR_NO_LAST_SLASH);
218 EXPECT_EQ("/.", out.str());
221 // Output inside current dir.
223 std::ostringstream out;
224 writer.WriteDir(out, SourceDir("//out/Debug/"),
225 PathOutput::DIR_INCLUDE_LAST_SLASH);
226 EXPECT_EQ("./", out.str());
229 std::ostringstream out;
230 writer.WriteDir(out, SourceDir("//out/Debug/"),
231 PathOutput::DIR_NO_LAST_SLASH);
232 EXPECT_EQ(".", out.str());
235 std::ostringstream out;
236 writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
237 PathOutput::DIR_INCLUDE_LAST_SLASH);
238 EXPECT_EQ("foo/", out.str());
241 std::ostringstream out;
242 writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
243 PathOutput::DIR_NO_LAST_SLASH);
244 EXPECT_EQ("foo", out.str());
247 // WriteDir using an OutputFile.
249 std::ostringstream out;
250 writer.WriteDir(out, OutputFile("foo/"),
251 PathOutput::DIR_INCLUDE_LAST_SLASH);
252 EXPECT_EQ("foo/", out.str());
255 std::ostringstream out;
256 writer.WriteDir(out, OutputFile("foo/"),
257 PathOutput::DIR_NO_LAST_SLASH);
258 EXPECT_EQ("foo", out.str());
261 std::ostringstream out;
262 writer.WriteDir(out, OutputFile(),
263 PathOutput::DIR_INCLUDE_LAST_SLASH);
264 EXPECT_EQ("", out.str());
268 // Empty build dir writer.
269 base::StringPiece source_root("/source/root");
270 PathOutput root_writer(SourceDir("//"), source_root, ESCAPE_NINJA);
272 std::ostringstream out;
273 root_writer.WriteDir(out, SourceDir("//"),
274 PathOutput::DIR_INCLUDE_LAST_SLASH);
275 EXPECT_EQ("./", out.str());
278 std::ostringstream out;
279 root_writer.WriteDir(out, SourceDir("//"),
280 PathOutput::DIR_NO_LAST_SLASH);
281 EXPECT_EQ(".", out.str());