blob: 26a045b212ecfbaf68be7aad500b5cd7107a878f [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"
Avi Drissmane3b70bf2019-01-04 19:50:2212#include "base/stl_util.h"
jdoerrie5c4dc4e2019-02-01 18:02:3313#include "base/strings/strcat.h"
[email protected]a4ea1f12013-06-07 18:37:0714#include "base/strings/utf_string_conversions.h"
avi9b6f42932015-12-26 22:15:1415#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3816#include "testing/gtest/include/gtest/gtest.h"
17
pgal.u-szeged421dddb2014-11-25 12:55:0218namespace base {
[email protected]023ad6ab2013-02-17 05:07:2319
[email protected]98a1c2682010-08-10 18:14:1920// To test Windows quoting behavior, we use a string that has some backslashes
21// and quotes.
22// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
23// Here it is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2424static const CommandLine::StringType kTrickyQuoted =
25 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
[email protected]98a1c2682010-08-10 18:14:1926// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
27// Here that is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2428static const CommandLine::StringType kTricky =
29 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
[email protected]98a1c2682010-08-10 18:14:1930
initial.commitd7cae122008-07-26 21:49:3831TEST(CommandLineTest, CommandLineConstructor) {
[email protected]a40ca4302011-05-14 01:10:2432 const CommandLine::CharType* argv[] = {
33 FILE_PATH_LITERAL("program"),
34 FILE_PATH_LITERAL("--foo="),
35 FILE_PATH_LITERAL("-bAr"),
36 FILE_PATH_LITERAL("-spaetzel=pierogi"),
37 FILE_PATH_LITERAL("-baz"),
38 FILE_PATH_LITERAL("flim"),
39 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
40 FILE_PATH_LITERAL("-spaetzle=Crepe"),
41 FILE_PATH_LITERAL("-=loosevalue"),
[email protected]21e342f2012-10-19 06:19:5942 FILE_PATH_LITERAL("-"),
[email protected]a40ca4302011-05-14 01:10:2443 FILE_PATH_LITERAL("FLAN"),
[email protected]1fa39f02011-09-13 15:45:3444 FILE_PATH_LITERAL("a"),
[email protected]a40ca4302011-05-14 01:10:2445 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
46 FILE_PATH_LITERAL("--"),
47 FILE_PATH_LITERAL("--"),
48 FILE_PATH_LITERAL("--not-a-switch"),
49 FILE_PATH_LITERAL("\"in the time of submarines...\""),
50 FILE_PATH_LITERAL("unquoted arg-with-space")};
jdoerrie5c4dc4e2019-02-01 18:02:3351 CommandLine cl(size(argv), argv);
[email protected]a40ca4302011-05-14 01:10:2452
[email protected]61a4c6f2011-07-20 04:54:5253 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2454 EXPECT_FALSE(cl.HasSwitch("cruller"));
55 EXPECT_FALSE(cl.HasSwitch("flim"));
56 EXPECT_FALSE(cl.HasSwitch("program"));
57 EXPECT_FALSE(cl.HasSwitch("dog"));
58 EXPECT_FALSE(cl.HasSwitch("cat"));
59 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
60 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
61 EXPECT_FALSE(cl.HasSwitch("--"));
62
63 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
64 cl.GetProgram().value());
65
66 EXPECT_TRUE(cl.HasSwitch("foo"));
jackhoub20cbb42015-04-22 02:21:4467#if defined(OS_WIN)
68 EXPECT_TRUE(cl.HasSwitch("bar"));
69#else
70 EXPECT_FALSE(cl.HasSwitch("bar"));
71#endif
[email protected]a40ca4302011-05-14 01:10:2472 EXPECT_TRUE(cl.HasSwitch("baz"));
73 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]a40ca4302011-05-14 01:10:2474 EXPECT_TRUE(cl.HasSwitch("other-switches"));
75 EXPECT_TRUE(cl.HasSwitch("input-translation"));
76
77 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhou1bd9da92015-05-21 04:48:0078 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
[email protected]a40ca4302011-05-14 01:10:2479 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
80 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
81 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
82 "other-switches"));
83 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
84
[email protected]75f1c782011-07-13 23:41:2285 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]21e342f2012-10-19 06:19:5986 ASSERT_EQ(8U, args.size());
[email protected]a40ca4302011-05-14 01:10:2487
jdoerrie1c4b8ff2018-10-03 00:10:5788 auto iter = args.begin();
[email protected]a40ca4302011-05-14 01:10:2489 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
90 ++iter;
[email protected]21e342f2012-10-19 06:19:5991 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
92 ++iter;
[email protected]a40ca4302011-05-14 01:10:2493 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
94 ++iter;
[email protected]1fa39f02011-09-13 15:45:3495 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
96 ++iter;
[email protected]a40ca4302011-05-14 01:10:2497 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
98 ++iter;
99 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
100 ++iter;
101 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
102 ++iter;
103 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
104 ++iter;
105 EXPECT_TRUE(iter == args.end());
106}
107
108TEST(CommandLineTest, CommandLineFromString) {
[email protected]bb975362009-01-21 01:00:22109#if defined(OS_WIN)
jdoerrie5c4dc4e2019-02-01 18:02:33110 CommandLine cl = CommandLine::FromString(StrCat(
111 {STRING16_LITERAL("program --foo= -bAr /Spaetzel=pierogi /Baz flim "),
112 STRING16_LITERAL("--other-switches=\"--dog=canine --cat=feline\" "),
113 STRING16_LITERAL("-spaetzle=Crepe -=loosevalue FLAN "),
114 STRING16_LITERAL("--input-translation=\"45\"--output-rotation "),
115 STRING16_LITERAL("--quotes="), kTrickyQuoted, STRING16_LITERAL(" "),
116 STRING16_LITERAL("-- -- --not-a-switch "),
117 STRING16_LITERAL("\"in the time of submarines...\"")}));
[email protected]a40ca4302011-05-14 01:10:24118
[email protected]61a4c6f2011-07-20 04:54:52119 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]b7e0a2a2009-10-13 02:07:25120 EXPECT_FALSE(cl.HasSwitch("cruller"));
121 EXPECT_FALSE(cl.HasSwitch("flim"));
122 EXPECT_FALSE(cl.HasSwitch("program"));
123 EXPECT_FALSE(cl.HasSwitch("dog"));
124 EXPECT_FALSE(cl.HasSwitch("cat"));
125 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
126 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
127 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commitd7cae122008-07-26 21:49:38128
[email protected]78c4c422010-10-08 00:06:31129 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
130 cl.GetProgram().value());
initial.commitd7cae122008-07-26 21:49:38131
[email protected]b7e0a2a2009-10-13 02:07:25132 EXPECT_TRUE(cl.HasSwitch("foo"));
133 EXPECT_TRUE(cl.HasSwitch("bar"));
134 EXPECT_TRUE(cl.HasSwitch("baz"));
135 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]b7e0a2a2009-10-13 02:07:25136 EXPECT_TRUE(cl.HasSwitch("other-switches"));
137 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:19138 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commitd7cae122008-07-26 21:49:38139
[email protected]c4e52f0d2009-11-06 19:55:16140 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhou1bd9da92015-05-21 04:48:00141 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
[email protected]c4e52f0d2009-11-06 19:55:16142 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
143 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
144 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
145 "other-switches"));
146 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
[email protected]a40ca4302011-05-14 01:10:24147 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commitd7cae122008-07-26 21:49:38148
[email protected]75f1c782011-07-13 23:41:22149 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]2e4c50c2010-07-21 15:57:23150 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:38151
[email protected]2e4c50c2010-07-21 15:57:23152 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
153 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:38154 ++iter;
[email protected]a40ca4302011-05-14 01:10:24155 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commitd7cae122008-07-26 21:49:38156 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23157 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:35158 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23159 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:35160 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23161 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:38162 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23163 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:08164
[email protected]a40ca4302011-05-14 01:10:24165 // Check that a generated string produces an equivalent command line.
[email protected]61a4c6f2011-07-20 04:54:52166 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
167 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
[email protected]10e42bf2008-10-15 21:59:08168#endif
initial.commitd7cae122008-07-26 21:49:38169}
170
initial.commitd7cae122008-07-26 21:49:38171// Tests behavior with an empty input string.
172TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32173#if defined(OS_WIN)
jdoerrie5c4dc4e2019-02-01 18:02:33174 CommandLine cl_from_string = CommandLine::FromString(string16());
[email protected]61a4c6f2011-07-20 04:54:52175 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24176 EXPECT_TRUE(cl_from_string.GetProgram().empty());
177 EXPECT_EQ(1U, cl_from_string.argv().size());
[email protected]75f1c782011-07-13 23:41:22178 EXPECT_TRUE(cl_from_string.GetArgs().empty());
[email protected]f3adb5c2008-08-07 20:07:32179#endif
Ivan Kotenkova16212a52017-11-08 12:37:33180 CommandLine cl_from_argv(0, nullptr);
[email protected]61a4c6f2011-07-20 04:54:52181 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24182 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
183 EXPECT_EQ(1U, cl_from_argv.argv().size());
[email protected]75f1c782011-07-13 23:41:22184 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commitd7cae122008-07-26 21:49:38185}
186
[email protected]45f982e2012-10-29 21:31:31187TEST(CommandLineTest, GetArgumentsString) {
188 static const FilePath::CharType kPath1[] =
189 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
190 static const FilePath::CharType kPath2[] =
191 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
192
193 static const char kFirstArgName[] = "first-arg";
194 static const char kSecondArgName[] = "arg2";
195 static const char kThirdArgName[] = "arg with space";
196 static const char kFourthArgName[] = "nospace";
mgiucac974d5102014-10-01 09:24:51197 static const char kFifthArgName[] = "%1";
[email protected]45f982e2012-10-29 21:31:31198
199 CommandLine cl(CommandLine::NO_PROGRAM);
200 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
201 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
202 cl.AppendArg(kThirdArgName);
203 cl.AppendArg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51204 cl.AppendArg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31205
206#if defined(OS_WIN)
pgal.u-szeged421dddb2014-11-25 12:55:02207 CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName));
208 CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName));
209 CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName));
210 CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName));
211 CommandLine::StringType expected_fifth_arg(UTF8ToUTF16(kFifthArgName));
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39212#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]45f982e2012-10-29 21:31:31213 CommandLine::StringType expected_first_arg(kFirstArgName);
214 CommandLine::StringType expected_second_arg(kSecondArgName);
215 CommandLine::StringType expected_third_arg(kThirdArgName);
216 CommandLine::StringType expected_fourth_arg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51217 CommandLine::StringType expected_fifth_arg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31218#endif
219
220#if defined(OS_WIN)
221#define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
222#else
223#define QUOTE_ON_WIN FILE_PATH_LITERAL("")
224#endif // OS_WIN
225
226 CommandLine::StringType expected_str;
227 expected_str.append(FILE_PATH_LITERAL("--"))
228 .append(expected_first_arg)
229 .append(FILE_PATH_LITERAL("="))
230 .append(QUOTE_ON_WIN)
231 .append(kPath1)
232 .append(QUOTE_ON_WIN)
233 .append(FILE_PATH_LITERAL(" "))
234 .append(FILE_PATH_LITERAL("--"))
235 .append(expected_second_arg)
236 .append(FILE_PATH_LITERAL("="))
237 .append(QUOTE_ON_WIN)
238 .append(kPath2)
239 .append(QUOTE_ON_WIN)
240 .append(FILE_PATH_LITERAL(" "))
241 .append(QUOTE_ON_WIN)
242 .append(expected_third_arg)
243 .append(QUOTE_ON_WIN)
244 .append(FILE_PATH_LITERAL(" "))
mgiucac974d5102014-10-01 09:24:51245 .append(expected_fourth_arg)
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
[email protected]45f982e2012-10-29 21:31:31260}
261
[email protected]bb975362009-01-21 01:00:22262// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38263TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25264 std::string switch1 = "switch1";
265 std::string switch2 = "switch2";
[email protected]a40ca4302011-05-14 01:10:24266 std::string value2 = "value";
[email protected]b7e0a2a2009-10-13 02:07:25267 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57268 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25269 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57270 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19271 std::string switch5 = "quotes";
[email protected]a40ca4302011-05-14 01:10:24272 CommandLine::StringType value5 = kTricky;
initial.commitd7cae122008-07-26 21:49:38273
[email protected]51343d5a2009-10-26 22:39:33274 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38275
[email protected]bb975362009-01-21 01:00:22276 cl.AppendSwitch(switch1);
[email protected]a40ca4302011-05-14 01:10:24277 cl.AppendSwitchASCII(switch2, value2);
[email protected]05076ba22010-07-30 05:59:57278 cl.AppendSwitchASCII(switch3, value3);
279 cl.AppendSwitchASCII(switch4, value4);
jackhou1bd9da92015-05-21 04:48:00280 cl.AppendSwitchASCII(switch5, value4);
[email protected]a40ca4302011-05-14 01:10:24281 cl.AppendSwitchNative(switch5, value5);
[email protected]bb975362009-01-21 01:00:22282
initial.commitd7cae122008-07-26 21:49:38283 EXPECT_TRUE(cl.HasSwitch(switch1));
284 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]a40ca4302011-05-14 01:10:24285 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38286 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57287 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20288 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57289 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19290 EXPECT_TRUE(cl.HasSwitch(switch5));
[email protected]a40ca4302011-05-14 01:10:24291 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
[email protected]98a1c2682010-08-10 18:14:19292
293#if defined(OS_WIN)
jdoerrie5c4dc4e2019-02-01 18:02:33294 EXPECT_EQ(
295 StrCat({STRING16_LITERAL("Program "), STRING16_LITERAL("--switch1 "),
296 STRING16_LITERAL("--switch2=value "),
297 STRING16_LITERAL("--switch3=\"a value with spaces\" "),
298 STRING16_LITERAL("--switch4=\"\\\"a value with quotes\\\"\" "),
299 // Even though the switches are unique, appending can add
300 // repeat switches to argv.
301 STRING16_LITERAL("--quotes=\"\\\"a value with quotes\\\"\" "),
302 STRING16_LITERAL("--quotes=\""), kTrickyQuoted,
303 STRING16_LITERAL("\"")}),
304 cl.GetCommandLineString());
[email protected]98a1c2682010-08-10 18:14:19305#endif
initial.commitd7cae122008-07-26 21:49:38306}
[email protected]e6124ad52010-11-15 04:17:52307
[email protected]a40ca4302011-05-14 01:10:24308TEST(CommandLineTest, AppendSwitchesDashDash) {
309 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
310 FILE_PATH_LITERAL("--"),
311 FILE_PATH_LITERAL("--arg1") };
jdoerrie5c4dc4e2019-02-01 18:02:33312 CommandLine cl(size(raw_argv), raw_argv);
[email protected]a40ca4302011-05-14 01:10:24313
Avi Drissmane3b70bf2019-01-04 19:50:22314 cl.AppendSwitch("switch1");
315 cl.AppendSwitchASCII("switch2", "foo");
[email protected]a40ca4302011-05-14 01:10:24316
Avi Drissmane3b70bf2019-01-04 19:50:22317 cl.AppendArg("--arg2");
[email protected]a40ca4302011-05-14 01:10:24318
Avi Drissmane3b70bf2019-01-04 19:50:22319 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
320 cl.GetCommandLineString());
321 CommandLine::StringVector cl_argv = cl.argv();
322 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
323 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
324 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
325 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
326 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
327 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
[email protected]a40ca4302011-05-14 01:10:24328}
329
[email protected]e6124ad52010-11-15 04:17:52330// Tests that when AppendArguments is called that the program is set correctly
331// on the target CommandLine object and the switches from the source
332// CommandLine are added to the target.
333TEST(CommandLineTest, AppendArguments) {
334 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
335 cl1.AppendSwitch("switch1");
336 cl1.AppendSwitchASCII("switch2", "foo");
337
338 CommandLine cl2(CommandLine::NO_PROGRAM);
339 cl2.AppendArguments(cl1, true);
340 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52341 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
[email protected]e6124ad52010-11-15 04:17:52342
343 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
344 c1.AppendSwitch("switch1");
345 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
346 c2.AppendSwitch("switch2");
347
348 c1.AppendArguments(c2, true);
349 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
350 EXPECT_TRUE(c1.HasSwitch("switch1"));
351 EXPECT_TRUE(c1.HasSwitch("switch2"));
352}
353
[email protected]450b34ec2010-11-29 21:12:22354#if defined(OS_WIN)
[email protected]61a4c6f2011-07-20 04:54:52355// Make sure that the command line string program paths are quoted as necessary.
[email protected]450b34ec2010-11-29 21:12:22356// This only makes sense on Windows and the test is basically here to guard
357// against regressions.
358TEST(CommandLineTest, ProgramQuotes) {
[email protected]a40ca4302011-05-14 01:10:24359 // Check that quotes are not added for paths without spaces.
jdoerrie5c4dc4e2019-02-01 18:02:33360 const FilePath kProgram(STRING16_LITERAL("Program"));
[email protected]a40ca4302011-05-14 01:10:24361 CommandLine cl_program(kProgram);
362 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52363 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24364
jdoerrie5c4dc4e2019-02-01 18:02:33365 const FilePath kProgramPath(STRING16_LITERAL("Program Path"));
[email protected]450b34ec2010-11-29 21:12:22366
367 // Check that quotes are not returned from GetProgram().
[email protected]a40ca4302011-05-14 01:10:24368 CommandLine cl_program_path(kProgramPath);
369 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
[email protected]450b34ec2010-11-29 21:12:22370
[email protected]a40ca4302011-05-14 01:10:24371 // Check that quotes are added to command line string paths containing spaces.
[email protected]61a4c6f2011-07-20 04:54:52372 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
jdoerrie5c4dc4e2019-02-01 18:02:33373 EXPECT_EQ(STRING16_LITERAL("\"Program Path\""), cmd_string);
mgiucac974d5102014-10-01 09:24:51374
375 // Check the optional quoting of placeholders in programs.
jdoerrie5c4dc4e2019-02-01 18:02:33376 CommandLine cl_quote_placeholder(FilePath(STRING16_LITERAL("%1")));
377 EXPECT_EQ(STRING16_LITERAL("%1"),
378 cl_quote_placeholder.GetCommandLineString());
379 EXPECT_EQ(STRING16_LITERAL("\"%1\""),
mgiucac974d5102014-10-01 09:24:51380 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
[email protected]450b34ec2010-11-29 21:12:22381}
382#endif
[email protected]f96fe2c42011-07-13 18:03:34383
384// Calling Init multiple times should not modify the previous CommandLine.
385TEST(CommandLineTest, Init) {
arihce89963ac2015-08-12 23:45:48386 // Call Init without checking output once so we know it's been called
387 // whether or not the test runner does so.
Ivan Kotenkova16212a52017-11-08 12:37:33388 CommandLine::Init(0, nullptr);
[email protected]f96fe2c42011-07-13 18:03:34389 CommandLine* initial = CommandLine::ForCurrentProcess();
Ivan Kotenkova16212a52017-11-08 12:37:33390 EXPECT_FALSE(CommandLine::Init(0, nullptr));
[email protected]f96fe2c42011-07-13 18:03:34391 CommandLine* current = CommandLine::ForCurrentProcess();
392 EXPECT_EQ(initial, current);
393}
pgal.u-szeged421dddb2014-11-25 12:55:02394
jackhou1bd9da92015-05-21 04:48:00395// Test that copies of CommandLine have a valid StringPiece map.
396TEST(CommandLineTest, Copy) {
dcheng093de9b2016-04-04 21:25:51397 std::unique_ptr<CommandLine> initial(
398 new CommandLine(CommandLine::NO_PROGRAM));
jackhou1bd9da92015-05-21 04:48:00399 initial->AppendSwitch("a");
400 initial->AppendSwitch("bbbbbbbbbbbbbbb");
401 initial->AppendSwitch("c");
402 CommandLine copy_constructed(*initial);
403 CommandLine assigned = *initial;
404 CommandLine::SwitchMap switch_map = initial->GetSwitches();
405 initial.reset();
406 for (const auto& pair : switch_map)
407 EXPECT_TRUE(copy_constructed.HasSwitch(pair.first));
408 for (const auto& pair : switch_map)
409 EXPECT_TRUE(assigned.HasSwitch(pair.first));
410}
411
skyostild851aa12017-03-29 17:38:35412TEST(CommandLineTest, PrependSimpleWrapper) {
413 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
414 cl.AppendSwitch("a");
415 cl.AppendSwitch("b");
416 cl.PrependWrapper(FILE_PATH_LITERAL("wrapper --foo --bar"));
417
418 EXPECT_EQ(6u, cl.argv().size());
419 EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
420 EXPECT_EQ(FILE_PATH_LITERAL("--foo"), cl.argv()[1]);
421 EXPECT_EQ(FILE_PATH_LITERAL("--bar"), cl.argv()[2]);
422 EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
423 EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
424 EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
425}
426
427TEST(CommandLineTest, PrependComplexWrapper) {
428 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
429 cl.AppendSwitch("a");
430 cl.AppendSwitch("b");
431 cl.PrependWrapper(
432 FILE_PATH_LITERAL("wrapper --foo='hello world' --bar=\"let's go\""));
433
434 EXPECT_EQ(6u, cl.argv().size());
435 EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
436 EXPECT_EQ(FILE_PATH_LITERAL("--foo='hello world'"), cl.argv()[1]);
437 EXPECT_EQ(FILE_PATH_LITERAL("--bar=\"let's go\""), cl.argv()[2]);
438 EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
439 EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
440 EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
441}
442
Avi Drissman1aa6cb92019-01-23 15:58:38443TEST(CommandLineTest, RemoveSwitch) {
444 std::string switch1 = "switch1";
445 std::string switch2 = "switch2";
446 std::string value2 = "value";
447
448 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
449
450 cl.AppendSwitch(switch1);
451 cl.AppendSwitchASCII(switch2, value2);
452
453 EXPECT_TRUE(cl.HasSwitch(switch1));
454 EXPECT_TRUE(cl.HasSwitch(switch2));
455 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
456
457 cl.RemoveSwitch(switch1);
458
459 EXPECT_FALSE(cl.HasSwitch(switch1));
460 EXPECT_TRUE(cl.HasSwitch(switch2));
461 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
462}
463
John Rummellb1d5fcb2019-04-27 01:13:33464TEST(CommandLineTest, MultipleSameSwitch) {
465 const CommandLine::CharType* argv[] = {
466 FILE_PATH_LITERAL("program"),
467 FILE_PATH_LITERAL("--foo=one"), // --foo first time
468 FILE_PATH_LITERAL("-baz"),
469 FILE_PATH_LITERAL("--foo=two") // --foo second time
470 };
471 CommandLine cl(size(argv), argv);
472
473 EXPECT_TRUE(cl.HasSwitch("foo"));
474 EXPECT_TRUE(cl.HasSwitch("baz"));
475
476 EXPECT_EQ("two", cl.GetSwitchValueASCII("foo"));
477}
478
pgal.u-szeged421dddb2014-11-25 12:55:02479} // namespace base