Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: tools/gn/path_output_unittest.cc

Issue 311733002: Redo escaping in GN (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« tools/gn/escape_unittest.cc ('K') | « tools/gn/path_output.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sstream> 5 #include <sstream>
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/path_output.h" 8 #include "tools/gn/path_output.h"
9 #include "tools/gn/source_dir.h" 9 #include "tools/gn/source_dir.h"
10 #include "tools/gn/source_file.h" 10 #include "tools/gn/source_file.h"
11 11
12 TEST(PathOutput, Basic) { 12 TEST(PathOutput, Basic) {
13 SourceDir build_dir("//out/Debug/"); 13 SourceDir build_dir("//out/Debug/");
14 PathOutput writer(build_dir, ESCAPE_NONE, false); 14 PathOutput writer(build_dir, ESCAPE_NONE);
15 { 15 {
16 // Normal source-root path. 16 // Normal source-root path.
17 std::ostringstream out; 17 std::ostringstream out;
18 writer.WriteFile(out, SourceFile("//foo/bar.cc")); 18 writer.WriteFile(out, SourceFile("//foo/bar.cc"));
19 EXPECT_EQ("../../foo/bar.cc", out.str()); 19 EXPECT_EQ("../../foo/bar.cc", out.str());
20 } 20 }
21 { 21 {
22 // File in the root dir. 22 // File in the root dir.
23 std::ostringstream out; 23 std::ostringstream out;
24 writer.WriteFile(out, SourceFile("//foo.cc")); 24 writer.WriteFile(out, SourceFile("//foo.cc"));
(...skipping 20 matching lines...) Expand all
45 std::ostringstream out; 45 std::ostringstream out;
46 writer.WriteFile(out, SourceFile("/foo/bar.cc")); 46 writer.WriteFile(out, SourceFile("/foo/bar.cc"));
47 EXPECT_EQ("/foo/bar.cc", out.str()); 47 EXPECT_EQ("/foo/bar.cc", out.str());
48 } 48 }
49 #endif 49 #endif
50 } 50 }
51 51
52 // Same as basic but the output dir is the root. 52 // Same as basic but the output dir is the root.
53 TEST(PathOutput, BasicInRoot) { 53 TEST(PathOutput, BasicInRoot) {
54 SourceDir build_dir("//"); 54 SourceDir build_dir("//");
55 PathOutput writer(build_dir, ESCAPE_NONE, false); 55 PathOutput writer(build_dir, ESCAPE_NONE);
56 { 56 {
57 // Normal source-root path. 57 // Normal source-root path.
58 std::ostringstream out; 58 std::ostringstream out;
59 writer.WriteFile(out, SourceFile("//foo/bar.cc")); 59 writer.WriteFile(out, SourceFile("//foo/bar.cc"));
60 EXPECT_EQ("foo/bar.cc", out.str()); 60 EXPECT_EQ("foo/bar.cc", out.str());
61 } 61 }
62 { 62 {
63 // File in the root dir. 63 // File in the root dir.
64 std::ostringstream out; 64 std::ostringstream out;
65 writer.WriteFile(out, SourceFile("//foo.cc")); 65 writer.WriteFile(out, SourceFile("//foo.cc"));
66 EXPECT_EQ("foo.cc", out.str()); 66 EXPECT_EQ("foo.cc", out.str());
67 } 67 }
68 } 68 }
69 69
70 TEST(PathOutput, NinjaEscaping) { 70 TEST(PathOutput, NinjaEscaping) {
71 SourceDir build_dir("//out/Debug/"); 71 SourceDir build_dir("//out/Debug/");
72 PathOutput writer(build_dir, ESCAPE_NINJA, false); 72 PathOutput writer(build_dir, ESCAPE_NINJA);
73 { 73 {
74 // Spaces and $ in filenames. 74 // Spaces and $ in filenames.
75 std::ostringstream out; 75 std::ostringstream out;
76 writer.WriteFile(out, SourceFile("//foo/foo bar$.cc")); 76 writer.WriteFile(out, SourceFile("//foo/foo bar$.cc"));
77 EXPECT_EQ("../../foo/foo$ bar$$.cc", out.str()); 77 EXPECT_EQ("../../foo/foo$ bar$$.cc", out.str());
78 } 78 }
79 { 79 {
80 // Not other weird stuff 80 // Not other weird stuff
81 std::ostringstream out; 81 std::ostringstream out;
82 writer.WriteFile(out, SourceFile("//foo/\"foo\\bar\".cc")); 82 writer.WriteFile(out, SourceFile("//foo/\"foo\\bar\".cc"));
83 EXPECT_EQ("../../foo/\"foo\\bar\".cc", out.str()); 83 EXPECT_EQ("../../foo/\"foo\\bar\".cc", out.str());
84 } 84 }
85 } 85 }
86 86
87 TEST(PathOutput, ShellEscaping) { 87 TEST(PathOutput, NinjaForkEscaping) {
88 SourceDir build_dir("//out/Debug/"); 88 SourceDir build_dir("//out/Debug/");
89 PathOutput writer(build_dir, ESCAPE_SHELL, false); 89 PathOutput writer(build_dir, ESCAPE_NINJA_COMMAND);
90
91 // Spaces in filenames should get quoted on Windows.
92 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
90 { 93 {
91 // Spaces in filenames should get quoted.
92 std::ostringstream out; 94 std::ostringstream out;
93 writer.WriteFile(out, SourceFile("//foo/foo bar.cc")); 95 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
94 EXPECT_EQ("\"../../foo/foo bar.cc\"", out.str()); 96 EXPECT_EQ("\"../../foo/foo$ bar.cc\"", out.str());
95 } 97 }
98
99 // Spaces in filenames should get escaped on Posix.
100 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
96 { 101 {
97 // Quotes should get blackslash-escaped. 102 std::ostringstream out;
103 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
104 EXPECT_EQ("../../foo/foo\\$ bar.cc", out.str());
105 }
106
107 // Quotes should get blackslash-escaped on Windows and Posix.
108 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
109 {
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());
116 }
117 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
118 {
98 std::ostringstream out; 119 std::ostringstream out;
99 writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc")); 120 writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc"));
100 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str()); 121 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str());
101 } 122 }
123
124
125 // Backslashes should get escaped on non-Windows and preserved on Windows.
126 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
102 { 127 {
103 // Backslashes should get escaped on non-Windows and preserved on Windows.
104 std::ostringstream out; 128 std::ostringstream out;
105 writer.WriteFile(out, SourceFile("//foo\\bar.cc")); 129 writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
106 #if defined(OS_WIN)
107 EXPECT_EQ("../../foo\\bar.cc", out.str()); 130 EXPECT_EQ("../../foo\\bar.cc", out.str());
108 #else
109 EXPECT_EQ("../../foo\\\\bar.cc", out.str());
110 #endif
111 } 131 }
112 } 132 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
113
114 TEST(PathOutput, SlashConversion) {
115 SourceDir build_dir("//out/Debug/");
116 PathOutput writer(build_dir, ESCAPE_NINJA, true);
117 { 133 {
118 std::ostringstream out; 134 std::ostringstream out;
119 writer.WriteFile(out, SourceFile("//foo/bar.cc")); 135 writer.WriteFile(out, SourceFile("//foo\\bar.cc"));
120 #if defined(OS_WIN) 136 EXPECT_EQ("../../foo\\\\bar.cc", out.str());
121 EXPECT_EQ("..\\..\\foo\\bar.cc", out.str());
122 #else
123 EXPECT_EQ("../../foo/bar.cc", out.str());
124 #endif
125 } 137 }
126 } 138 }
127 139
128 TEST(PathOutput, InhibitQuoting) { 140 TEST(PathOutput, InhibitQuoting) {
129 SourceDir build_dir("//out/Debug/"); 141 SourceDir build_dir("//out/Debug/");
130 PathOutput writer(build_dir, ESCAPE_SHELL, false); 142 PathOutput writer(build_dir, ESCAPE_NINJA_COMMAND);
131 writer.set_inhibit_quoting(true); 143 writer.set_inhibit_quoting(true);
144
145 writer.set_escape_platform(ESCAPE_PLATFORM_WIN);
132 { 146 {
133 // We should get unescaped spaces in the output with no quotes. 147 // We should get unescaped spaces in the output with no quotes.
134 std::ostringstream out; 148 std::ostringstream out;
135 writer.WriteFile(out, SourceFile("//foo/foo bar.cc")); 149 writer.WriteFile(out, SourceFile("//foo/foo bar.cc"));
136 EXPECT_EQ("../../foo/foo bar.cc", out.str()); 150 EXPECT_EQ("../../foo/foo$ bar.cc", out.str());
151 }
152
153 writer.set_escape_platform(ESCAPE_PLATFORM_POSIX);
154 {
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());
137 } 159 }
138 } 160 }
139 161
140 TEST(PathOutput, WriteDir) { 162 TEST(PathOutput, WriteDir) {
141 { 163 {
142 SourceDir build_dir("//out/Debug/"); 164 SourceDir build_dir("//out/Debug/");
143 PathOutput writer(build_dir, ESCAPE_NINJA, false); 165 PathOutput writer(build_dir, ESCAPE_NINJA);
144 { 166 {
145 std::ostringstream out; 167 std::ostringstream out;
146 writer.WriteDir(out, SourceDir("//foo/bar/"), 168 writer.WriteDir(out, SourceDir("//foo/bar/"),
147 PathOutput::DIR_INCLUDE_LAST_SLASH); 169 PathOutput::DIR_INCLUDE_LAST_SLASH);
148 EXPECT_EQ("../../foo/bar/", out.str()); 170 EXPECT_EQ("../../foo/bar/", out.str());
149 } 171 }
150 { 172 {
151 std::ostringstream out; 173 std::ostringstream out;
152 writer.WriteDir(out, SourceDir("//foo/bar/"), 174 writer.WriteDir(out, SourceDir("//foo/bar/"),
153 PathOutput::DIR_NO_LAST_SLASH); 175 PathOutput::DIR_NO_LAST_SLASH);
(...skipping 30 matching lines...) Expand all
184 { 206 {
185 std::ostringstream out; 207 std::ostringstream out;
186 writer.WriteDir(out, SourceDir("/"), 208 writer.WriteDir(out, SourceDir("/"),
187 PathOutput::DIR_NO_LAST_SLASH); 209 PathOutput::DIR_NO_LAST_SLASH);
188 EXPECT_EQ("/.", out.str()); 210 EXPECT_EQ("/.", out.str());
189 } 211 }
190 212
191 // Output inside current dir. 213 // Output inside current dir.
192 { 214 {
193 std::ostringstream out; 215 std::ostringstream out;
216
217
194 writer.WriteDir(out, SourceDir("//out/Debug/"), 218 writer.WriteDir(out, SourceDir("//out/Debug/"),
195 PathOutput::DIR_INCLUDE_LAST_SLASH); 219 PathOutput::DIR_INCLUDE_LAST_SLASH);
196 EXPECT_EQ("./", out.str()); 220 EXPECT_EQ("./", out.str());
197 } 221 }
198 { 222 {
199 std::ostringstream out; 223 std::ostringstream out;
200 writer.WriteDir(out, SourceDir("//out/Debug/"), 224 writer.WriteDir(out, SourceDir("//out/Debug/"),
201 PathOutput::DIR_NO_LAST_SLASH); 225 PathOutput::DIR_NO_LAST_SLASH);
202 EXPECT_EQ(".", out.str()); 226 EXPECT_EQ(".", out.str());
203 } 227 }
204 { 228 {
205 std::ostringstream out; 229 std::ostringstream out;
206 writer.WriteDir(out, SourceDir("//out/Debug/foo/"), 230 writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
207 PathOutput::DIR_INCLUDE_LAST_SLASH); 231 PathOutput::DIR_INCLUDE_LAST_SLASH);
208 EXPECT_EQ("foo/", out.str()); 232 EXPECT_EQ("foo/", out.str());
209 } 233 }
210 { 234 {
211 std::ostringstream out; 235 std::ostringstream out;
212 writer.WriteDir(out, SourceDir("//out/Debug/foo/"), 236 writer.WriteDir(out, SourceDir("//out/Debug/foo/"),
213 PathOutput::DIR_NO_LAST_SLASH); 237 PathOutput::DIR_NO_LAST_SLASH);
214 EXPECT_EQ("foo", out.str()); 238 EXPECT_EQ("foo", out.str());
215 } 239 }
216 } 240 }
217 { 241 {
218 // Empty build dir writer. 242 // Empty build dir writer.
219 PathOutput root_writer(SourceDir("//"), ESCAPE_NINJA, false); 243 PathOutput root_writer(SourceDir("//"), ESCAPE_NINJA);
220 { 244 {
221 std::ostringstream out; 245 std::ostringstream out;
222 root_writer.WriteDir(out, SourceDir("//"), 246 root_writer.WriteDir(out, SourceDir("//"),
223 PathOutput::DIR_INCLUDE_LAST_SLASH); 247 PathOutput::DIR_INCLUDE_LAST_SLASH);
224 EXPECT_EQ("./", out.str()); 248 EXPECT_EQ("./", out.str());
225 } 249 }
226 { 250 {
227 std::ostringstream out; 251 std::ostringstream out;
228 root_writer.WriteDir(out, SourceDir("//"), 252 root_writer.WriteDir(out, SourceDir("//"),
229 PathOutput::DIR_NO_LAST_SLASH); 253 PathOutput::DIR_NO_LAST_SLASH);
230 EXPECT_EQ(".", out.str()); 254 EXPECT_EQ(".", out.str());
231 } 255 }
232 } 256 }
233 } 257 }
OLDNEW
« tools/gn/escape_unittest.cc ('K') | « tools/gn/path_output.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698