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
, false);
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
, false);
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
, false);
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
, ShellEscaping
) {
88 SourceDir
build_dir("//out/Debug/");
89 PathOutput
writer(build_dir
, ESCAPE_SHELL
, false);
91 // Spaces in filenames should get quoted.
92 std::ostringstream out
;
93 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
94 EXPECT_EQ("\"../../foo/foo bar.cc\"", out
.str());
97 // Quotes should get blackslash-escaped.
98 std::ostringstream out
;
99 writer
.WriteFile(out
, SourceFile("//foo/\"foobar\".cc"));
100 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out
.str());
103 // Backslashes should get escaped on non-Windows and preserved on Windows.
104 std::ostringstream out
;
105 writer
.WriteFile(out
, SourceFile("//foo\\bar.cc"));
107 EXPECT_EQ("../../foo\\bar.cc", out
.str());
109 EXPECT_EQ("../../foo\\\\bar.cc", out
.str());
114 TEST(PathOutput
, SlashConversion
) {
115 SourceDir
build_dir("//out/Debug/");
116 PathOutput
writer(build_dir
, ESCAPE_NINJA
, true);
118 std::ostringstream out
;
119 writer
.WriteFile(out
, SourceFile("//foo/bar.cc"));
121 EXPECT_EQ("..\\..\\foo\\bar.cc", out
.str());
123 EXPECT_EQ("../../foo/bar.cc", out
.str());
128 TEST(PathOutput
, InhibitQuoting
) {
129 SourceDir
build_dir("//out/Debug/");
130 PathOutput
writer(build_dir
, ESCAPE_SHELL
, false);
131 writer
.set_inhibit_quoting(true);
133 // We should get unescaped spaces in the output with no quotes.
134 std::ostringstream out
;
135 writer
.WriteFile(out
, SourceFile("//foo/foo bar.cc"));
136 EXPECT_EQ("../../foo/foo bar.cc", out
.str());
140 TEST(PathOutput
, WriteDir
) {
142 SourceDir
build_dir("//out/Debug/");
143 PathOutput
writer(build_dir
, ESCAPE_NINJA
, false);
145 std::ostringstream out
;
146 writer
.WriteDir(out
, SourceDir("//foo/bar/"),
147 PathOutput::DIR_INCLUDE_LAST_SLASH
);
148 EXPECT_EQ("../../foo/bar/", out
.str());
151 std::ostringstream out
;
152 writer
.WriteDir(out
, SourceDir("//foo/bar/"),
153 PathOutput::DIR_NO_LAST_SLASH
);
154 EXPECT_EQ("../../foo/bar", out
.str());
157 // Output source root dir.
159 std::ostringstream out
;
160 writer
.WriteDir(out
, SourceDir("//"),
161 PathOutput::DIR_INCLUDE_LAST_SLASH
);
162 EXPECT_EQ("../../", out
.str());
165 std::ostringstream out
;
166 writer
.WriteDir(out
, SourceDir("//"),
167 PathOutput::DIR_NO_LAST_SLASH
);
168 EXPECT_EQ("../..", out
.str());
171 // Output system root dir.
173 std::ostringstream out
;
174 writer
.WriteDir(out
, SourceDir("/"),
175 PathOutput::DIR_INCLUDE_LAST_SLASH
);
176 EXPECT_EQ("/", out
.str());
179 std::ostringstream out
;
180 writer
.WriteDir(out
, SourceDir("/"),
181 PathOutput::DIR_INCLUDE_LAST_SLASH
);
182 EXPECT_EQ("/", out
.str());
185 std::ostringstream out
;
186 writer
.WriteDir(out
, SourceDir("/"),
187 PathOutput::DIR_NO_LAST_SLASH
);
188 EXPECT_EQ("/.", out
.str());
191 // Output inside current dir.
193 std::ostringstream out
;
194 writer
.WriteDir(out
, SourceDir("//out/Debug/"),
195 PathOutput::DIR_INCLUDE_LAST_SLASH
);
196 EXPECT_EQ("./", out
.str());
199 std::ostringstream out
;
200 writer
.WriteDir(out
, SourceDir("//out/Debug/"),
201 PathOutput::DIR_NO_LAST_SLASH
);
202 EXPECT_EQ(".", out
.str());
205 std::ostringstream out
;
206 writer
.WriteDir(out
, SourceDir("//out/Debug/foo/"),
207 PathOutput::DIR_INCLUDE_LAST_SLASH
);
208 EXPECT_EQ("foo/", out
.str());
211 std::ostringstream out
;
212 writer
.WriteDir(out
, SourceDir("//out/Debug/foo/"),
213 PathOutput::DIR_NO_LAST_SLASH
);
214 EXPECT_EQ("foo", out
.str());
218 // Empty build dir writer.
219 PathOutput
root_writer(SourceDir("//"), ESCAPE_NINJA
, false);
221 std::ostringstream out
;
222 root_writer
.WriteDir(out
, SourceDir("//"),
223 PathOutput::DIR_INCLUDE_LAST_SLASH
);
224 EXPECT_EQ("./", out
.str());
227 std::ostringstream out
;
228 root_writer
.WriteDir(out
, SourceDir("//"),
229 PathOutput::DIR_NO_LAST_SLASH
);
230 EXPECT_EQ(".", out
.str());