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.
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"));
35 writer
.WriteFile(out
, SourceFile("//out/Debug/bar/baz.cc"));
36 EXPECT_EQ("foo.cc bar/baz.cc", out
.str());
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());
47 // System-absolute path.
48 std::ostringstream out
;
49 writer
.WriteFile(out
, SourceFile("/foo/bar.cc"));
50 EXPECT_EQ("/foo/bar.cc", out
.str());
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\".cc"));
88 EXPECT_EQ("../../foo/\"foo\".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());
130 // Backslashes should get escaped on non-Windows and preserved on Windows.
131 writer
.set_escape_platform(ESCAPE_PLATFORM_WIN
);
133 std::ostringstream out
;
134 writer
.WriteFile(out
, OutputFile("foo\\bar.cc"));
135 EXPECT_EQ("foo\\bar.cc", out
.str());
137 writer
.set_escape_platform(ESCAPE_PLATFORM_POSIX
);
139 std::ostringstream out
;
140 writer
.WriteFile(out
, OutputFile("foo\\bar.cc"));
141 EXPECT_EQ("foo\\\\bar.cc", out
.str());
145 TEST(PathOutput
, InhibitQuoting
) {
146 SourceDir
build_dir("//out/Debug/");
147 base::StringPiece
source_root("/source/root");
148 PathOutput
writer(build_dir
, source_root
, ESCAPE_NINJA_COMMAND
);
149 writer
.set_inhibit_quoting(true);
151 writer
.set_escape_platform(ESCAPE_PLATFORM_WIN
);
153 // We should get unescaped spaces in the output with no quotes.
154 std::ostringstream out
;
155 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
156 EXPECT_EQ("../../foo/foo$ bar.cc", out
.str());
159 writer
.set_escape_platform(ESCAPE_PLATFORM_POSIX
);
161 // Escapes the space.
162 std::ostringstream out
;
163 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
164 EXPECT_EQ("../../foo/foo\\$ bar.cc", out
.str());
168 TEST(PathOutput
, WriteDir
) {
170 SourceDir
build_dir("//out/Debug/");
171 base::StringPiece
source_root("/source/root");
172 PathOutput
writer(build_dir
, source_root
, ESCAPE_NINJA
);
174 std::ostringstream out
;
175 writer
.WriteDir(out
, SourceDir("//foo/bar/"),
176 PathOutput::DIR_INCLUDE_LAST_SLASH
);
177 EXPECT_EQ("../../foo/bar/", out
.str());
180 std::ostringstream out
;
181 writer
.WriteDir(out
, SourceDir("//foo/bar/"),
182 PathOutput::DIR_NO_LAST_SLASH
);
183 EXPECT_EQ("../../foo/bar", out
.str());
186 // Output source root dir.
188 std::ostringstream out
;
189 writer
.WriteDir(out
, SourceDir("//"),
190 PathOutput::DIR_INCLUDE_LAST_SLASH
);
191 EXPECT_EQ("../../", out
.str());
194 std::ostringstream out
;
195 writer
.WriteDir(out
, SourceDir("//"),
196 PathOutput::DIR_NO_LAST_SLASH
);
197 EXPECT_EQ("../..", out
.str());
200 // Output system root dir.
202 std::ostringstream out
;
203 writer
.WriteDir(out
, SourceDir("/"),
204 PathOutput::DIR_INCLUDE_LAST_SLASH
);
205 EXPECT_EQ("/", out
.str());
208 std::ostringstream out
;
209 writer
.WriteDir(out
, SourceDir("/"),
210 PathOutput::DIR_INCLUDE_LAST_SLASH
);
211 EXPECT_EQ("/", out
.str());
214 std::ostringstream out
;
215 writer
.WriteDir(out
, SourceDir("/"),
216 PathOutput::DIR_NO_LAST_SLASH
);
217 EXPECT_EQ("/.", out
.str());
220 // Output inside current dir.
222 std::ostringstream out
;
223 writer
.WriteDir(out
, SourceDir("//out/Debug/"),
224 PathOutput::DIR_INCLUDE_LAST_SLASH
);
225 EXPECT_EQ("./", out
.str());
228 std::ostringstream out
;
229 writer
.WriteDir(out
, SourceDir("//out/Debug/"),
230 PathOutput::DIR_NO_LAST_SLASH
);
231 EXPECT_EQ(".", out
.str());
234 std::ostringstream out
;
235 writer
.WriteDir(out
, SourceDir("//out/Debug/foo/"),
236 PathOutput::DIR_INCLUDE_LAST_SLASH
);
237 EXPECT_EQ("foo/", out
.str());
240 std::ostringstream out
;
241 writer
.WriteDir(out
, SourceDir("//out/Debug/foo/"),
242 PathOutput::DIR_NO_LAST_SLASH
);
243 EXPECT_EQ("foo", out
.str());
246 // WriteDir using an OutputFile.
248 std::ostringstream out
;
249 writer
.WriteDir(out
, OutputFile("foo/"),
250 PathOutput::DIR_INCLUDE_LAST_SLASH
);
251 EXPECT_EQ("foo/", out
.str());
254 std::ostringstream out
;
255 writer
.WriteDir(out
, OutputFile("foo/"),
256 PathOutput::DIR_NO_LAST_SLASH
);
257 EXPECT_EQ("foo", out
.str());
260 std::ostringstream out
;
261 writer
.WriteDir(out
, OutputFile(),
262 PathOutput::DIR_INCLUDE_LAST_SLASH
);
263 EXPECT_EQ("", out
.str());
267 // Empty build dir writer.
268 base::StringPiece
source_root("/source/root");
269 PathOutput
root_writer(SourceDir("//"), source_root
, ESCAPE_NINJA
);
271 std::ostringstream out
;
272 root_writer
.WriteDir(out
, SourceDir("//"),
273 PathOutput::DIR_INCLUDE_LAST_SLASH
);
274 EXPECT_EQ("./", out
.str());
277 std::ostringstream out
;
278 root_writer
.WriteDir(out
, SourceDir("//"),
279 PathOutput::DIR_NO_LAST_SLASH
);
280 EXPECT_EQ(".", out
.str());