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

Side by Side Diff: base/command_line_unittest.cc

Issue 595803002: base::CommandLine: Added optional quoting of placeholder arguments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase off CL 602253006. Created 6 years, 2 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
« no previous file with comments | « base/command_line.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 TEST(CommandLineTest, GetArgumentsString) { 183 TEST(CommandLineTest, GetArgumentsString) {
184 static const FilePath::CharType kPath1[] = 184 static const FilePath::CharType kPath1[] =
185 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); 185 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
186 static const FilePath::CharType kPath2[] = 186 static const FilePath::CharType kPath2[] =
187 FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); 187 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
188 188
189 static const char kFirstArgName[] = "first-arg"; 189 static const char kFirstArgName[] = "first-arg";
190 static const char kSecondArgName[] = "arg2"; 190 static const char kSecondArgName[] = "arg2";
191 static const char kThirdArgName[] = "arg with space"; 191 static const char kThirdArgName[] = "arg with space";
192 static const char kFourthArgName[] = "nospace"; 192 static const char kFourthArgName[] = "nospace";
193 static const char kFifthArgName[] = "%1";
193 194
194 CommandLine cl(CommandLine::NO_PROGRAM); 195 CommandLine cl(CommandLine::NO_PROGRAM);
195 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); 196 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
196 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); 197 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
197 cl.AppendArg(kThirdArgName); 198 cl.AppendArg(kThirdArgName);
198 cl.AppendArg(kFourthArgName); 199 cl.AppendArg(kFourthArgName);
200 cl.AppendArg(kFifthArgName);
199 201
200 #if defined(OS_WIN) 202 #if defined(OS_WIN)
201 CommandLine::StringType expected_first_arg( 203 CommandLine::StringType expected_first_arg(
202 base::UTF8ToUTF16(kFirstArgName)); 204 base::UTF8ToUTF16(kFirstArgName));
203 CommandLine::StringType expected_second_arg( 205 CommandLine::StringType expected_second_arg(
204 base::UTF8ToUTF16(kSecondArgName)); 206 base::UTF8ToUTF16(kSecondArgName));
205 CommandLine::StringType expected_third_arg( 207 CommandLine::StringType expected_third_arg(
206 base::UTF8ToUTF16(kThirdArgName)); 208 base::UTF8ToUTF16(kThirdArgName));
207 CommandLine::StringType expected_fourth_arg( 209 CommandLine::StringType expected_fourth_arg(
208 base::UTF8ToUTF16(kFourthArgName)); 210 base::UTF8ToUTF16(kFourthArgName));
211 CommandLine::StringType expected_fifth_arg(base::UTF8ToUTF16(kFifthArgName));
209 #elif defined(OS_POSIX) 212 #elif defined(OS_POSIX)
210 CommandLine::StringType expected_first_arg(kFirstArgName); 213 CommandLine::StringType expected_first_arg(kFirstArgName);
211 CommandLine::StringType expected_second_arg(kSecondArgName); 214 CommandLine::StringType expected_second_arg(kSecondArgName);
212 CommandLine::StringType expected_third_arg(kThirdArgName); 215 CommandLine::StringType expected_third_arg(kThirdArgName);
213 CommandLine::StringType expected_fourth_arg(kFourthArgName); 216 CommandLine::StringType expected_fourth_arg(kFourthArgName);
217 CommandLine::StringType expected_fifth_arg(kFifthArgName);
214 #endif 218 #endif
215 219
216 #if defined(OS_WIN) 220 #if defined(OS_WIN)
217 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"") 221 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
218 #else 222 #else
219 #define QUOTE_ON_WIN FILE_PATH_LITERAL("") 223 #define QUOTE_ON_WIN FILE_PATH_LITERAL("")
220 #endif // OS_WIN 224 #endif // OS_WIN
221 225
222 CommandLine::StringType expected_str; 226 CommandLine::StringType expected_str;
223 expected_str.append(FILE_PATH_LITERAL("--")) 227 expected_str.append(FILE_PATH_LITERAL("--"))
224 .append(expected_first_arg) 228 .append(expected_first_arg)
225 .append(FILE_PATH_LITERAL("=")) 229 .append(FILE_PATH_LITERAL("="))
226 .append(QUOTE_ON_WIN) 230 .append(QUOTE_ON_WIN)
227 .append(kPath1) 231 .append(kPath1)
228 .append(QUOTE_ON_WIN) 232 .append(QUOTE_ON_WIN)
229 .append(FILE_PATH_LITERAL(" ")) 233 .append(FILE_PATH_LITERAL(" "))
230 .append(FILE_PATH_LITERAL("--")) 234 .append(FILE_PATH_LITERAL("--"))
231 .append(expected_second_arg) 235 .append(expected_second_arg)
232 .append(FILE_PATH_LITERAL("=")) 236 .append(FILE_PATH_LITERAL("="))
233 .append(QUOTE_ON_WIN) 237 .append(QUOTE_ON_WIN)
234 .append(kPath2) 238 .append(kPath2)
235 .append(QUOTE_ON_WIN) 239 .append(QUOTE_ON_WIN)
236 .append(FILE_PATH_LITERAL(" ")) 240 .append(FILE_PATH_LITERAL(" "))
237 .append(QUOTE_ON_WIN) 241 .append(QUOTE_ON_WIN)
238 .append(expected_third_arg) 242 .append(expected_third_arg)
239 .append(QUOTE_ON_WIN) 243 .append(QUOTE_ON_WIN)
240 .append(FILE_PATH_LITERAL(" ")) 244 .append(FILE_PATH_LITERAL(" "))
241 .append(expected_fourth_arg); 245 .append(expected_fourth_arg)
242 EXPECT_EQ(expected_str, cl.GetArgumentsString()); 246 .append(FILE_PATH_LITERAL(" "));
247
248 CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
249 expected_str_no_quote_placeholders.append(expected_fifth_arg);
250 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
251
252 #if defined(OS_WIN)
253 CommandLine::StringType expected_str_quote_placeholders(expected_str);
254 expected_str_quote_placeholders.append(QUOTE_ON_WIN)
255 .append(expected_fifth_arg)
256 .append(QUOTE_ON_WIN);
257 EXPECT_EQ(expected_str_quote_placeholders,
258 cl.GetArgumentsStringWithPlaceholders());
259 #endif
243 } 260 }
244 261
245 // Test methods for appending switches to a command line. 262 // Test methods for appending switches to a command line.
246 TEST(CommandLineTest, AppendSwitches) { 263 TEST(CommandLineTest, AppendSwitches) {
247 std::string switch1 = "switch1"; 264 std::string switch1 = "switch1";
248 std::string switch2 = "switch2"; 265 std::string switch2 = "switch2";
249 std::string value2 = "value"; 266 std::string value2 = "value";
250 std::string switch3 = "switch3"; 267 std::string switch3 = "switch3";
251 std::string value3 = "a value with spaces"; 268 std::string value3 = "a value with spaces";
252 std::string switch4 = "switch4"; 269 std::string switch4 = "switch4";
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 359
343 const FilePath kProgramPath(L"Program Path"); 360 const FilePath kProgramPath(L"Program Path");
344 361
345 // Check that quotes are not returned from GetProgram(). 362 // Check that quotes are not returned from GetProgram().
346 CommandLine cl_program_path(kProgramPath); 363 CommandLine cl_program_path(kProgramPath);
347 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); 364 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
348 365
349 // Check that quotes are added to command line string paths containing spaces. 366 // Check that quotes are added to command line string paths containing spaces.
350 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); 367 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
351 EXPECT_EQ(L"\"Program Path\"", cmd_string); 368 EXPECT_EQ(L"\"Program Path\"", cmd_string);
369
370 // Check the optional quoting of placeholders in programs.
371 CommandLine cl_quote_placeholder(base::FilePath(L"%1"));
372 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
373 EXPECT_EQ(L"\"%1\"",
374 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
352 } 375 }
353 #endif 376 #endif
354 377
355 // Calling Init multiple times should not modify the previous CommandLine. 378 // Calling Init multiple times should not modify the previous CommandLine.
356 TEST(CommandLineTest, Init) { 379 TEST(CommandLineTest, Init) {
357 CommandLine* initial = CommandLine::ForCurrentProcess(); 380 CommandLine* initial = CommandLine::ForCurrentProcess();
358 EXPECT_FALSE(CommandLine::Init(0, NULL)); 381 EXPECT_FALSE(CommandLine::Init(0, NULL));
359 CommandLine* current = CommandLine::ForCurrentProcess(); 382 CommandLine* current = CommandLine::ForCurrentProcess();
360 EXPECT_EQ(initial, current); 383 EXPECT_EQ(initial, current);
361 } 384 }
OLDNEW
« no previous file with comments | « base/command_line.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698