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 "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/path_output.h"
9 #include "tools/gn/source_dir.h"
10 #include "tools/gn/source_file.h"
12 TEST(PathOutput
, Basic
) {
13 SourceDir
build_dir("//out/Debug/");
14 PathOutput
writer(build_dir
, ESCAPE_NONE
);
16 // Normal source-root path.
17 std::ostringstream out
;
18 writer
.WriteFile(out
, SourceFile("//foo/bar.cc"));
19 EXPECT_EQ("../../foo/bar.cc", out
.str());
22 // File in the root dir.
23 std::ostringstream out
;
24 writer
.WriteFile(out
, SourceFile("//foo.cc"));
25 EXPECT_EQ("../../foo.cc", out
.str());
28 // Files in the output dir.
29 std::ostringstream out
;
30 writer
.WriteFile(out
, SourceFile("//out/Debug/foo.cc"));
32 writer
.WriteFile(out
, SourceFile("//out/Debug/bar/baz.cc"));
33 EXPECT_EQ("foo.cc bar/baz.cc", out
.str());
37 // System-absolute path.
38 std::ostringstream out
;
39 writer
.WriteFile(out
, SourceFile("/C:/foo/bar.cc"));
40 EXPECT_EQ("C:/foo/bar.cc", out
.str());
44 // System-absolute path.
45 std::ostringstream out
;
46 writer
.WriteFile(out
, SourceFile("/foo/bar.cc"));
47 EXPECT_EQ("/foo/bar.cc", out
.str());
52 // Same as basic but the output dir is the root.
53 TEST(PathOutput
, BasicInRoot
) {
54 SourceDir
build_dir("//");
55 PathOutput
writer(build_dir
, ESCAPE_NONE
);
57 // Normal source-root path.
58 std::ostringstream out
;
59 writer
.WriteFile(out
, SourceFile("//foo/bar.cc"));
60 EXPECT_EQ("foo/bar.cc", out
.str());
63 // File in the root dir.
64 std::ostringstream out
;
65 writer
.WriteFile(out
, SourceFile("//foo.cc"));
66 EXPECT_EQ("foo.cc", out
.str());
70 TEST(PathOutput
, NinjaEscaping
) {
71 SourceDir
build_dir("//out/Debug/");
72 PathOutput
writer(build_dir
, ESCAPE_NINJA
);
74 // Spaces and $ in filenames.
75 std::ostringstream out
;
76 writer
.WriteFile(out
, SourceFile("//foo/foo bar$.cc"));
77 EXPECT_EQ("../../foo/foo$ bar$$.cc", out
.str());
80 // Not other weird stuff
81 std::ostringstream out
;
82 writer
.WriteFile(out
, SourceFile("//foo/\"foo\\bar\".cc"));
83 EXPECT_EQ("../../foo/\"foo\\bar\".cc", out
.str());
87 TEST(PathOutput
, NinjaForkEscaping
) {
88 SourceDir
build_dir("//out/Debug/");
89 PathOutput
writer(build_dir
, ESCAPE_NINJA_COMMAND
);
91 // Spaces in filenames should get quoted on Windows.
92 writer
.set_escape_platform(ESCAPE_PLATFORM_WIN
);
94 std::ostringstream out
;
95 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
96 EXPECT_EQ("\"../../foo/foo$ bar.cc\"", out
.str());
99 // Spaces in filenames should get escaped on Posix.
100 writer
.set_escape_platform(ESCAPE_PLATFORM_POSIX
);
102 std::ostringstream out
;
103 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
104 EXPECT_EQ("../../foo/foo\\$ bar.cc", out
.str());
107 // Quotes should get blackslash-escaped on Windows and Posix.
108 writer
.set_escape_platform(ESCAPE_PLATFORM_WIN
);
110 std::ostringstream out
;
111 writer
.WriteFile(out
, SourceFile("//foo/\"foobar\".cc"));
112 // Our Windows code currently quotes the whole thing in this case for
113 // code simplicity, even though it's strictly unnecessary. This might
114 // change in the future.
115 EXPECT_EQ("\"../../foo/\\\"foobar\\\".cc\"", out
.str());
117 writer
.set_escape_platform(ESCAPE_PLATFORM_POSIX
);
119 std::ostringstream out
;
120 writer
.WriteFile(out
, SourceFile("//foo/\"foobar\".cc"));
121 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out
.str());
125 // Backslashes should get escaped on non-Windows and preserved on Windows.
126 writer
.set_escape_platform(ESCAPE_PLATFORM_WIN
);
128 std::ostringstream out
;
129 writer
.WriteFile(out
, SourceFile("//foo\\bar.cc"));
130 EXPECT_EQ("../../foo\\bar.cc", out
.str());
132 writer
.set_escape_platform(ESCAPE_PLATFORM_POSIX
);
134 std::ostringstream out
;
135 writer
.WriteFile(out
, SourceFile("//foo\\bar.cc"));
136 EXPECT_EQ("../../foo\\\\bar.cc", out
.str());
140 TEST(PathOutput
, InhibitQuoting
) {
141 SourceDir
build_dir("//out/Debug/");
142 PathOutput
writer(build_dir
, ESCAPE_NINJA_COMMAND
);
143 writer
.set_inhibit_quoting(true);
145 writer
.set_escape_platform(ESCAPE_PLATFORM_WIN
);
147 // We should get unescaped spaces in the output with no quotes.
148 std::ostringstream out
;
149 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
150 EXPECT_EQ("../../foo/foo$ bar.cc", out
.str());
153 writer
.set_escape_platform(ESCAPE_PLATFORM_POSIX
);
155 // Escapes the space.
156 std::ostringstream out
;
157 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
158 EXPECT_EQ("../../foo/foo\\$ bar.cc", out
.str());
162 TEST(PathOutput
, WriteDir
) {
164 SourceDir
build_dir("//out/Debug/");
165 PathOutput
writer(build_dir
, ESCAPE_NINJA
);
167 std::ostringstream out
;
168 writer
.WriteDir(out
, SourceDir("//foo/bar/"),
169 PathOutput::DIR_INCLUDE_LAST_SLASH
);
170 EXPECT_EQ("../../foo/bar/", out
.str());
173 std::ostringstream out
;
174 writer
.WriteDir(out
, SourceDir("//foo/bar/"),
175 PathOutput::DIR_NO_LAST_SLASH
);
176 EXPECT_EQ("../../foo/bar", out
.str());
179 // Output source root dir.
181 std::ostringstream out
;
182 writer
.WriteDir(out
, SourceDir("//"),
183 PathOutput::DIR_INCLUDE_LAST_SLASH
);
184 EXPECT_EQ("../../", out
.str());
187 std::ostringstream out
;
188 writer
.WriteDir(out
, SourceDir("//"),
189 PathOutput::DIR_NO_LAST_SLASH
);
190 EXPECT_EQ("../..", out
.str());
193 // Output system root dir.
195 std::ostringstream out
;
196 writer
.WriteDir(out
, SourceDir("/"),
197 PathOutput::DIR_INCLUDE_LAST_SLASH
);
198 EXPECT_EQ("/", out
.str());
201 std::ostringstream out
;
202 writer
.WriteDir(out
, SourceDir("/"),
203 PathOutput::DIR_INCLUDE_LAST_SLASH
);
204 EXPECT_EQ("/", out
.str());
207 std::ostringstream out
;
208 writer
.WriteDir(out
, SourceDir("/"),
209 PathOutput::DIR_NO_LAST_SLASH
);
210 EXPECT_EQ("/.", out
.str());
213 // Output inside current dir.
215 std::ostringstream out
;
218 writer
.WriteDir(out
, SourceDir("//out/Debug/"),
219 PathOutput::DIR_INCLUDE_LAST_SLASH
);
220 EXPECT_EQ("./", out
.str());
223 std::ostringstream out
;
224 writer
.WriteDir(out
, SourceDir("//out/Debug/"),
225 PathOutput::DIR_NO_LAST_SLASH
);
226 EXPECT_EQ(".", out
.str());
229 std::ostringstream out
;
230 writer
.WriteDir(out
, SourceDir("//out/Debug/foo/"),
231 PathOutput::DIR_INCLUDE_LAST_SLASH
);
232 EXPECT_EQ("foo/", out
.str());
235 std::ostringstream out
;
236 writer
.WriteDir(out
, SourceDir("//out/Debug/foo/"),
237 PathOutput::DIR_NO_LAST_SLASH
);
238 EXPECT_EQ("foo", out
.str());
242 // Empty build dir writer.
243 PathOutput
root_writer(SourceDir("//"), ESCAPE_NINJA
);
245 std::ostringstream out
;
246 root_writer
.WriteDir(out
, SourceDir("//"),
247 PathOutput::DIR_INCLUDE_LAST_SLASH
);
248 EXPECT_EQ("./", out
.str());
251 std::ostringstream out
;
252 root_writer
.WriteDir(out
, SourceDir("//"),
253 PathOutput::DIR_NO_LAST_SLASH
);
254 EXPECT_EQ(".", out
.str());