blob: 93ccd7c7295e7ffd29fd1da03626eabc4f01148a [file] [log] [blame]
[email protected]72e2e2422012-02-27 18:38:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
dcheng093de9b2016-04-04 21:25:515#include "base/command_line.h"
6
7#include <memory>
initial.commitd7cae122008-07-26 21:49:388#include <string>
9#include <vector>
10
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
avi9b6f42932015-12-26 22:15:1412#include "base/macros.h"
[email protected]a4ea1f12013-06-07 18:37:0713#include "base/strings/utf_string_conversions.h"
avi9b6f42932015-12-26 22:15:1414#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3815#include "testing/gtest/include/gtest/gtest.h"
16
pgal.u-szeged421dddb2014-11-25 12:55:0217namespace base {
[email protected]023ad6ab2013-02-17 05:07:2318
[email protected]98a1c2682010-08-10 18:14:1919// To test Windows quoting behavior, we use a string that has some backslashes
20// and quotes.
21// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
22// Here it is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2423static const CommandLine::StringType kTrickyQuoted =
24 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
[email protected]98a1c2682010-08-10 18:14:1925// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
26// Here that is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2427static const CommandLine::StringType kTricky =
28 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
[email protected]98a1c2682010-08-10 18:14:1929
initial.commitd7cae122008-07-26 21:49:3830TEST(CommandLineTest, CommandLineConstructor) {
[email protected]a40ca4302011-05-14 01:10:2431 const CommandLine::CharType* argv[] = {
32 FILE_PATH_LITERAL("program"),
33 FILE_PATH_LITERAL("--foo="),
34 FILE_PATH_LITERAL("-bAr"),
35 FILE_PATH_LITERAL("-spaetzel=pierogi"),
36 FILE_PATH_LITERAL("-baz"),
37 FILE_PATH_LITERAL("flim"),
38 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
39 FILE_PATH_LITERAL("-spaetzle=Crepe"),
40 FILE_PATH_LITERAL("-=loosevalue"),
[email protected]21e342f2012-10-19 06:19:5941 FILE_PATH_LITERAL("-"),
[email protected]a40ca4302011-05-14 01:10:2442 FILE_PATH_LITERAL("FLAN"),
[email protected]1fa39f02011-09-13 15:45:3443 FILE_PATH_LITERAL("a"),
[email protected]a40ca4302011-05-14 01:10:2444 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
45 FILE_PATH_LITERAL("--"),
46 FILE_PATH_LITERAL("--"),
47 FILE_PATH_LITERAL("--not-a-switch"),
48 FILE_PATH_LITERAL("\"in the time of submarines...\""),
49 FILE_PATH_LITERAL("unquoted arg-with-space")};
50 CommandLine cl(arraysize(argv), argv);
51
[email protected]61a4c6f2011-07-20 04:54:5252 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2453 EXPECT_FALSE(cl.HasSwitch("cruller"));
54 EXPECT_FALSE(cl.HasSwitch("flim"));
55 EXPECT_FALSE(cl.HasSwitch("program"));
56 EXPECT_FALSE(cl.HasSwitch("dog"));
57 EXPECT_FALSE(cl.HasSwitch("cat"));
58 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
59 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
60 EXPECT_FALSE(cl.HasSwitch("--"));
61
62 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
63 cl.GetProgram().value());
64
65 EXPECT_TRUE(cl.HasSwitch("foo"));
jackhoub20cbb42015-04-22 02:21:4466#if defined(OS_WIN)
67 EXPECT_TRUE(cl.HasSwitch("bar"));
68#else
69 EXPECT_FALSE(cl.HasSwitch("bar"));
70#endif
[email protected]a40ca4302011-05-14 01:10:2471 EXPECT_TRUE(cl.HasSwitch("baz"));
72 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]a40ca4302011-05-14 01:10:2473 EXPECT_TRUE(cl.HasSwitch("other-switches"));
74 EXPECT_TRUE(cl.HasSwitch("input-translation"));
75
76 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhou1bd9da92015-05-21 04:48:0077 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
[email protected]a40ca4302011-05-14 01:10:2478 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
79 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
80 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
81 "other-switches"));
82 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
83
[email protected]75f1c782011-07-13 23:41:2284 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]21e342f2012-10-19 06:19:5985 ASSERT_EQ(8U, args.size());
[email protected]a40ca4302011-05-14 01:10:2486
jdoerrie1c4b8ff2018-10-03 00:10:5787 auto iter = args.begin();
[email protected]a40ca4302011-05-14 01:10:2488 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
89 ++iter;
[email protected]21e342f2012-10-19 06:19:5990 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
91 ++iter;
[email protected]a40ca4302011-05-14 01:10:2492 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
93 ++iter;
[email protected]1fa39f02011-09-13 15:45:3494 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
95 ++iter;
[email protected]a40ca4302011-05-14 01:10:2496 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
97 ++iter;
98 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
99 ++iter;
100 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
101 ++iter;
102 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
103 ++iter;
104 EXPECT_TRUE(iter == args.end());
105}
106
107TEST(CommandLineTest, CommandLineFromString) {
[email protected]bb975362009-01-21 01:00:22108#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:33109 CommandLine cl = CommandLine::FromString(
[email protected]a40ca4302011-05-14 01:10:24110 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
111 L"--other-switches=\"--dog=canine --cat=feline\" "
112 L"-spaetzle=Crepe -=loosevalue FLAN "
113 L"--input-translation=\"45\"--output-rotation "
114 L"--quotes=" + kTrickyQuoted + L" "
115 L"-- -- --not-a-switch "
116 L"\"in the time of submarines...\"");
117
[email protected]61a4c6f2011-07-20 04:54:52118 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]b7e0a2a2009-10-13 02:07:25119 EXPECT_FALSE(cl.HasSwitch("cruller"));
120 EXPECT_FALSE(cl.HasSwitch("flim"));
121 EXPECT_FALSE(cl.HasSwitch("program"));
122 EXPECT_FALSE(cl.HasSwitch("dog"));
123 EXPECT_FALSE(cl.HasSwitch("cat"));
124 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
125 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
126 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commitd7cae122008-07-26 21:49:38127
[email protected]78c4c422010-10-08 00:06:31128 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
129 cl.GetProgram().value());
initial.commitd7cae122008-07-26 21:49:38130
[email protected]b7e0a2a2009-10-13 02:07:25131 EXPECT_TRUE(cl.HasSwitch("foo"));
132 EXPECT_TRUE(cl.HasSwitch("bar"));
133 EXPECT_TRUE(cl.HasSwitch("baz"));
134 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]b7e0a2a2009-10-13 02:07:25135 EXPECT_TRUE(cl.HasSwitch("other-switches"));
136 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:19137 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commitd7cae122008-07-26 21:49:38138
[email protected]c4e52f0d2009-11-06 19:55:16139 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhou1bd9da92015-05-21 04:48:00140 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
[email protected]c4e52f0d2009-11-06 19:55:16141 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
142 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
143 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
144 "other-switches"));
145 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
[email protected]a40ca4302011-05-14 01:10:24146 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commitd7cae122008-07-26 21:49:38147
[email protected]75f1c782011-07-13 23:41:22148 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]2e4c50c2010-07-21 15:57:23149 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:38150
[email protected]2e4c50c2010-07-21 15:57:23151 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
152 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:38153 ++iter;
[email protected]a40ca4302011-05-14 01:10:24154 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commitd7cae122008-07-26 21:49:38155 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23156 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:35157 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23158 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:35159 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23160 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:38161 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23162 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:08163
[email protected]a40ca4302011-05-14 01:10:24164 // Check that a generated string produces an equivalent command line.
[email protected]61a4c6f2011-07-20 04:54:52165 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
166 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
[email protected]10e42bf2008-10-15 21:59:08167#endif
initial.commitd7cae122008-07-26 21:49:38168}
169
initial.commitd7cae122008-07-26 21:49:38170// Tests behavior with an empty input string.
171TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32172#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24173 CommandLine cl_from_string = CommandLine::FromString(L"");
[email protected]61a4c6f2011-07-20 04:54:52174 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24175 EXPECT_TRUE(cl_from_string.GetProgram().empty());
176 EXPECT_EQ(1U, cl_from_string.argv().size());
[email protected]75f1c782011-07-13 23:41:22177 EXPECT_TRUE(cl_from_string.GetArgs().empty());
[email protected]f3adb5c2008-08-07 20:07:32178#endif
Ivan Kotenkova16212a52017-11-08 12:37:33179 CommandLine cl_from_argv(0, nullptr);
[email protected]61a4c6f2011-07-20 04:54:52180 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24181 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
182 EXPECT_EQ(1U, cl_from_argv.argv().size());
[email protected]75f1c782011-07-13 23:41:22183 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commitd7cae122008-07-26 21:49:38184}
185
[email protected]45f982e2012-10-29 21:31:31186TEST(CommandLineTest, GetArgumentsString) {
187 static const FilePath::CharType kPath1[] =
188 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
189 static const FilePath::CharType kPath2[] =
190 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
191
192 static const char kFirstArgName[] = "first-arg";
193 static const char kSecondArgName[] = "arg2";
194 static const char kThirdArgName[] = "arg with space";
195 static const char kFourthArgName[] = "nospace";
mgiucac974d5102014-10-01 09:24:51196 static const char kFifthArgName[] = "%1";
[email protected]45f982e2012-10-29 21:31:31197
198 CommandLine cl(CommandLine::NO_PROGRAM);
199 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
200 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
201 cl.AppendArg(kThirdArgName);
202 cl.AppendArg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51203 cl.AppendArg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31204
205#if defined(OS_WIN)
pgal.u-szeged421dddb2014-11-25 12:55:02206 CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName));
207 CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName));
208 CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName));
209 CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName));
210 CommandLine::StringType expected_fifth_arg(UTF8ToUTF16(kFifthArgName));
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39211#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]45f982e2012-10-29 21:31:31212 CommandLine::StringType expected_first_arg(kFirstArgName);
213 CommandLine::StringType expected_second_arg(kSecondArgName);
214 CommandLine::StringType expected_third_arg(kThirdArgName);
215 CommandLine::StringType expected_fourth_arg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51216 CommandLine::StringType expected_fifth_arg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31217#endif
218
219#if defined(OS_WIN)
220#define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
221#else
222#define QUOTE_ON_WIN FILE_PATH_LITERAL("")
223#endif // OS_WIN
224
225 CommandLine::StringType expected_str;
226 expected_str.append(FILE_PATH_LITERAL("--"))
227 .append(expected_first_arg)
228 .append(FILE_PATH_LITERAL("="))
229 .append(QUOTE_ON_WIN)
230 .append(kPath1)
231 .append(QUOTE_ON_WIN)
232 .append(FILE_PATH_LITERAL(" "))
233 .append(FILE_PATH_LITERAL("--"))
234 .append(expected_second_arg)
235 .append(FILE_PATH_LITERAL("="))
236 .append(QUOTE_ON_WIN)
237 .append(kPath2)
238 .append(QUOTE_ON_WIN)
239 .append(FILE_PATH_LITERAL(" "))
240 .append(QUOTE_ON_WIN)
241 .append(expected_third_arg)
242 .append(QUOTE_ON_WIN)
243 .append(FILE_PATH_LITERAL(" "))
mgiucac974d5102014-10-01 09:24:51244 .append(expected_fourth_arg)
245 .append(FILE_PATH_LITERAL(" "));
246
247 CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
248 expected_str_no_quote_placeholders.append(expected_fifth_arg);
249 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
250
251#if defined(OS_WIN)
252 CommandLine::StringType expected_str_quote_placeholders(expected_str);
253 expected_str_quote_placeholders.append(QUOTE_ON_WIN)
254 .append(expected_fifth_arg)
255 .append(QUOTE_ON_WIN);
256 EXPECT_EQ(expected_str_quote_placeholders,
257 cl.GetArgumentsStringWithPlaceholders());
258#endif
[email protected]45f982e2012-10-29 21:31:31259}
260
[email protected]bb975362009-01-21 01:00:22261// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38262TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25263 std::string switch1 = "switch1";
264 std::string switch2 = "switch2";
[email protected]a40ca4302011-05-14 01:10:24265 std::string value2 = "value";
[email protected]b7e0a2a2009-10-13 02:07:25266 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57267 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25268 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57269 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19270 std::string switch5 = "quotes";
[email protected]a40ca4302011-05-14 01:10:24271 CommandLine::StringType value5 = kTricky;
initial.commitd7cae122008-07-26 21:49:38272
[email protected]51343d5a2009-10-26 22:39:33273 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38274
[email protected]bb975362009-01-21 01:00:22275 cl.AppendSwitch(switch1);
[email protected]a40ca4302011-05-14 01:10:24276 cl.AppendSwitchASCII(switch2, value2);
[email protected]05076ba22010-07-30 05:59:57277 cl.AppendSwitchASCII(switch3, value3);
278 cl.AppendSwitchASCII(switch4, value4);
jackhou1bd9da92015-05-21 04:48:00279 cl.AppendSwitchASCII(switch5, value4);
[email protected]a40ca4302011-05-14 01:10:24280 cl.AppendSwitchNative(switch5, value5);
[email protected]bb975362009-01-21 01:00:22281
initial.commitd7cae122008-07-26 21:49:38282 EXPECT_TRUE(cl.HasSwitch(switch1));
283 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]a40ca4302011-05-14 01:10:24284 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38285 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57286 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20287 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57288 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19289 EXPECT_TRUE(cl.HasSwitch(switch5));
[email protected]a40ca4302011-05-14 01:10:24290 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
[email protected]98a1c2682010-08-10 18:14:19291
292#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24293 EXPECT_EQ(L"Program "
[email protected]98a1c2682010-08-10 18:14:19294 L"--switch1 "
295 L"--switch2=value "
296 L"--switch3=\"a value with spaces\" "
297 L"--switch4=\"\\\"a value with quotes\\\"\" "
jackhou1bd9da92015-05-21 04:48:00298 // Even though the switches are unique, appending can add repeat
299 // switches to argv.
300 L"--quotes=\"\\\"a value with quotes\\\"\" "
[email protected]a40ca4302011-05-14 01:10:24301 L"--quotes=\"" + kTrickyQuoted + L"\"",
[email protected]61a4c6f2011-07-20 04:54:52302 cl.GetCommandLineString());
[email protected]98a1c2682010-08-10 18:14:19303#endif
initial.commitd7cae122008-07-26 21:49:38304}
[email protected]e6124ad52010-11-15 04:17:52305
[email protected]a40ca4302011-05-14 01:10:24306TEST(CommandLineTest, AppendSwitchesDashDash) {
307 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
308 FILE_PATH_LITERAL("--"),
309 FILE_PATH_LITERAL("--arg1") };
310 CommandLine cl(arraysize(raw_argv), raw_argv);
311
312 cl.AppendSwitch("switch1");
313 cl.AppendSwitchASCII("switch2", "foo");
314
315 cl.AppendArg("--arg2");
316
317 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
[email protected]61a4c6f2011-07-20 04:54:52318 cl.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24319 CommandLine::StringVector cl_argv = cl.argv();
320 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
321 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
322 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
323 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
324 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
325 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
326}
327
[email protected]e6124ad52010-11-15 04:17:52328// Tests that when AppendArguments is called that the program is set correctly
329// on the target CommandLine object and the switches from the source
330// CommandLine are added to the target.
331TEST(CommandLineTest, AppendArguments) {
332 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
333 cl1.AppendSwitch("switch1");
334 cl1.AppendSwitchASCII("switch2", "foo");
335
336 CommandLine cl2(CommandLine::NO_PROGRAM);
337 cl2.AppendArguments(cl1, true);
338 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52339 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
[email protected]e6124ad52010-11-15 04:17:52340
341 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
342 c1.AppendSwitch("switch1");
343 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
344 c2.AppendSwitch("switch2");
345
346 c1.AppendArguments(c2, true);
347 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
348 EXPECT_TRUE(c1.HasSwitch("switch1"));
349 EXPECT_TRUE(c1.HasSwitch("switch2"));
350}
351
[email protected]450b34ec2010-11-29 21:12:22352#if defined(OS_WIN)
[email protected]61a4c6f2011-07-20 04:54:52353// Make sure that the command line string program paths are quoted as necessary.
[email protected]450b34ec2010-11-29 21:12:22354// This only makes sense on Windows and the test is basically here to guard
355// against regressions.
356TEST(CommandLineTest, ProgramQuotes) {
[email protected]a40ca4302011-05-14 01:10:24357 // Check that quotes are not added for paths without spaces.
[email protected]450b34ec2010-11-29 21:12:22358 const FilePath kProgram(L"Program");
[email protected]a40ca4302011-05-14 01:10:24359 CommandLine cl_program(kProgram);
360 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52361 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24362
363 const FilePath kProgramPath(L"Program Path");
[email protected]450b34ec2010-11-29 21:12:22364
365 // Check that quotes are not returned from GetProgram().
[email protected]a40ca4302011-05-14 01:10:24366 CommandLine cl_program_path(kProgramPath);
367 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
[email protected]450b34ec2010-11-29 21:12:22368
[email protected]a40ca4302011-05-14 01:10:24369 // Check that quotes are added to command line string paths containing spaces.
[email protected]61a4c6f2011-07-20 04:54:52370 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
mgiucab937b2422014-09-24 04:55:31371 EXPECT_EQ(L"\"Program Path\"", cmd_string);
mgiucac974d5102014-10-01 09:24:51372
373 // Check the optional quoting of placeholders in programs.
pgal.u-szeged421dddb2014-11-25 12:55:02374 CommandLine cl_quote_placeholder(FilePath(L"%1"));
mgiucac974d5102014-10-01 09:24:51375 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
376 EXPECT_EQ(L"\"%1\"",
377 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
[email protected]450b34ec2010-11-29 21:12:22378}
379#endif
[email protected]f96fe2c42011-07-13 18:03:34380
381// Calling Init multiple times should not modify the previous CommandLine.
382TEST(CommandLineTest, Init) {
arihce89963ac2015-08-12 23:45:48383 // Call Init without checking output once so we know it's been called
384 // whether or not the test runner does so.
Ivan Kotenkova16212a52017-11-08 12:37:33385 CommandLine::Init(0, nullptr);
[email protected]f96fe2c42011-07-13 18:03:34386 CommandLine* initial = CommandLine::ForCurrentProcess();
Ivan Kotenkova16212a52017-11-08 12:37:33387 EXPECT_FALSE(CommandLine::Init(0, nullptr));
[email protected]f96fe2c42011-07-13 18:03:34388 CommandLine* current = CommandLine::ForCurrentProcess();
389 EXPECT_EQ(initial, current);
390}
pgal.u-szeged421dddb2014-11-25 12:55:02391
jackhou1bd9da92015-05-21 04:48:00392// Test that copies of CommandLine have a valid StringPiece map.
393TEST(CommandLineTest, Copy) {
dcheng093de9b2016-04-04 21:25:51394 std::unique_ptr<CommandLine> initial(
395 new CommandLine(CommandLine::NO_PROGRAM));
jackhou1bd9da92015-05-21 04:48:00396 initial->AppendSwitch("a");
397 initial->AppendSwitch("bbbbbbbbbbbbbbb");
398 initial->AppendSwitch("c");
399 CommandLine copy_constructed(*initial);
400 CommandLine assigned = *initial;
401 CommandLine::SwitchMap switch_map = initial->GetSwitches();
402 initial.reset();
403 for (const auto& pair : switch_map)
404 EXPECT_TRUE(copy_constructed.HasSwitch(pair.first));
405 for (const auto& pair : switch_map)
406 EXPECT_TRUE(assigned.HasSwitch(pair.first));
407}
408
skyostild851aa12017-03-29 17:38:35409TEST(CommandLineTest, PrependSimpleWrapper) {
410 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
411 cl.AppendSwitch("a");
412 cl.AppendSwitch("b");
413 cl.PrependWrapper(FILE_PATH_LITERAL("wrapper --foo --bar"));
414
415 EXPECT_EQ(6u, cl.argv().size());
416 EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
417 EXPECT_EQ(FILE_PATH_LITERAL("--foo"), cl.argv()[1]);
418 EXPECT_EQ(FILE_PATH_LITERAL("--bar"), cl.argv()[2]);
419 EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
420 EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
421 EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
422}
423
424TEST(CommandLineTest, PrependComplexWrapper) {
425 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
426 cl.AppendSwitch("a");
427 cl.AppendSwitch("b");
428 cl.PrependWrapper(
429 FILE_PATH_LITERAL("wrapper --foo='hello world' --bar=\"let's go\""));
430
431 EXPECT_EQ(6u, cl.argv().size());
432 EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
433 EXPECT_EQ(FILE_PATH_LITERAL("--foo='hello world'"), cl.argv()[1]);
434 EXPECT_EQ(FILE_PATH_LITERAL("--bar=\"let's go\""), cl.argv()[2]);
435 EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
436 EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
437 EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
438}
439
pgal.u-szeged421dddb2014-11-25 12:55:02440} // namespace base