blob: a4107b59ba34fda3972971b6551c9ab005df35de [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"
Pavol Markobf16b812019-06-14 00:53:1216#include "testing/gmock/include/gmock/gmock.h"
initial.commitd7cae122008-07-26 21:49:3817#include "testing/gtest/include/gtest/gtest.h"
18
pgal.u-szeged421dddb2014-11-25 12:55:0219namespace base {
[email protected]023ad6ab2013-02-17 05:07:2320
[email protected]98a1c2682010-08-10 18:14:1921// To test Windows quoting behavior, we use a string that has some backslashes
22// and quotes.
23// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
24// Here it is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2425static const CommandLine::StringType kTrickyQuoted =
26 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
[email protected]98a1c2682010-08-10 18:14:1927// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
28// Here that is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2429static const CommandLine::StringType kTricky =
30 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
[email protected]98a1c2682010-08-10 18:14:1931
initial.commitd7cae122008-07-26 21:49:3832TEST(CommandLineTest, CommandLineConstructor) {
[email protected]a40ca4302011-05-14 01:10:2433 const CommandLine::CharType* argv[] = {
34 FILE_PATH_LITERAL("program"),
35 FILE_PATH_LITERAL("--foo="),
36 FILE_PATH_LITERAL("-bAr"),
37 FILE_PATH_LITERAL("-spaetzel=pierogi"),
38 FILE_PATH_LITERAL("-baz"),
39 FILE_PATH_LITERAL("flim"),
40 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
41 FILE_PATH_LITERAL("-spaetzle=Crepe"),
42 FILE_PATH_LITERAL("-=loosevalue"),
[email protected]21e342f2012-10-19 06:19:5943 FILE_PATH_LITERAL("-"),
[email protected]a40ca4302011-05-14 01:10:2444 FILE_PATH_LITERAL("FLAN"),
[email protected]1fa39f02011-09-13 15:45:3445 FILE_PATH_LITERAL("a"),
[email protected]a40ca4302011-05-14 01:10:2446 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
47 FILE_PATH_LITERAL("--"),
48 FILE_PATH_LITERAL("--"),
49 FILE_PATH_LITERAL("--not-a-switch"),
50 FILE_PATH_LITERAL("\"in the time of submarines...\""),
51 FILE_PATH_LITERAL("unquoted arg-with-space")};
jdoerrie5c4dc4e2019-02-01 18:02:3352 CommandLine cl(size(argv), argv);
[email protected]a40ca4302011-05-14 01:10:2453
[email protected]61a4c6f2011-07-20 04:54:5254 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2455 EXPECT_FALSE(cl.HasSwitch("cruller"));
56 EXPECT_FALSE(cl.HasSwitch("flim"));
57 EXPECT_FALSE(cl.HasSwitch("program"));
58 EXPECT_FALSE(cl.HasSwitch("dog"));
59 EXPECT_FALSE(cl.HasSwitch("cat"));
60 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
61 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
62 EXPECT_FALSE(cl.HasSwitch("--"));
63
64 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
65 cl.GetProgram().value());
66
67 EXPECT_TRUE(cl.HasSwitch("foo"));
jackhoub20cbb42015-04-22 02:21:4468#if defined(OS_WIN)
69 EXPECT_TRUE(cl.HasSwitch("bar"));
70#else
71 EXPECT_FALSE(cl.HasSwitch("bar"));
72#endif
[email protected]a40ca4302011-05-14 01:10:2473 EXPECT_TRUE(cl.HasSwitch("baz"));
74 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]a40ca4302011-05-14 01:10:2475 EXPECT_TRUE(cl.HasSwitch("other-switches"));
76 EXPECT_TRUE(cl.HasSwitch("input-translation"));
77
78 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhou1bd9da92015-05-21 04:48:0079 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
[email protected]a40ca4302011-05-14 01:10:2480 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
81 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
82 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
83 "other-switches"));
84 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
85
[email protected]75f1c782011-07-13 23:41:2286 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]21e342f2012-10-19 06:19:5987 ASSERT_EQ(8U, args.size());
[email protected]a40ca4302011-05-14 01:10:2488
jdoerrie1c4b8ff2018-10-03 00:10:5789 auto iter = args.begin();
[email protected]a40ca4302011-05-14 01:10:2490 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
91 ++iter;
[email protected]21e342f2012-10-19 06:19:5992 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
93 ++iter;
[email protected]a40ca4302011-05-14 01:10:2494 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
95 ++iter;
[email protected]1fa39f02011-09-13 15:45:3496 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
97 ++iter;
[email protected]a40ca4302011-05-14 01:10:2498 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
99 ++iter;
100 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
101 ++iter;
102 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
103 ++iter;
104 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
105 ++iter;
106 EXPECT_TRUE(iter == args.end());
107}
108
109TEST(CommandLineTest, CommandLineFromString) {
[email protected]bb975362009-01-21 01:00:22110#if defined(OS_WIN)
Jan Wilken Dörrieda77fd432019-10-24 21:40:34111 CommandLine cl = CommandLine::FromString(
112 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
113 L"--other-switches=\"--dog=canine --cat=feline\" "
114 L"-spaetzle=Crepe -=loosevalue FLAN "
115 L"--input-translation=\"45\"--output-rotation "
116 L"--quotes=" +
117 kTrickyQuoted +
118 L" -- -- --not-a-switch \"in the time of submarines...\"");
[email protected]a40ca4302011-05-14 01:10:24119
[email protected]61a4c6f2011-07-20 04:54:52120 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]b7e0a2a2009-10-13 02:07:25121 EXPECT_FALSE(cl.HasSwitch("cruller"));
122 EXPECT_FALSE(cl.HasSwitch("flim"));
123 EXPECT_FALSE(cl.HasSwitch("program"));
124 EXPECT_FALSE(cl.HasSwitch("dog"));
125 EXPECT_FALSE(cl.HasSwitch("cat"));
126 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
127 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
128 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commitd7cae122008-07-26 21:49:38129
[email protected]78c4c422010-10-08 00:06:31130 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
131 cl.GetProgram().value());
initial.commitd7cae122008-07-26 21:49:38132
[email protected]b7e0a2a2009-10-13 02:07:25133 EXPECT_TRUE(cl.HasSwitch("foo"));
134 EXPECT_TRUE(cl.HasSwitch("bar"));
135 EXPECT_TRUE(cl.HasSwitch("baz"));
136 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]b7e0a2a2009-10-13 02:07:25137 EXPECT_TRUE(cl.HasSwitch("other-switches"));
138 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:19139 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commitd7cae122008-07-26 21:49:38140
[email protected]c4e52f0d2009-11-06 19:55:16141 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhou1bd9da92015-05-21 04:48:00142 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
[email protected]c4e52f0d2009-11-06 19:55:16143 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
144 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
145 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
146 "other-switches"));
147 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
[email protected]a40ca4302011-05-14 01:10:24148 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commitd7cae122008-07-26 21:49:38149
[email protected]75f1c782011-07-13 23:41:22150 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]2e4c50c2010-07-21 15:57:23151 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:38152
[email protected]2e4c50c2010-07-21 15:57:23153 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
154 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:38155 ++iter;
[email protected]a40ca4302011-05-14 01:10:24156 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commitd7cae122008-07-26 21:49:38157 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23158 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:35159 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23160 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:35161 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23162 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:38163 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23164 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:08165
[email protected]a40ca4302011-05-14 01:10:24166 // Check that a generated string produces an equivalent command line.
[email protected]61a4c6f2011-07-20 04:54:52167 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
168 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
[email protected]10e42bf2008-10-15 21:59:08169#endif
initial.commitd7cae122008-07-26 21:49:38170}
171
initial.commitd7cae122008-07-26 21:49:38172// Tests behavior with an empty input string.
173TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32174#if defined(OS_WIN)
Jan Wilken Dörrieda77fd432019-10-24 21:40:34175 CommandLine cl_from_string = CommandLine::FromString(std::wstring());
[email protected]61a4c6f2011-07-20 04:54:52176 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24177 EXPECT_TRUE(cl_from_string.GetProgram().empty());
178 EXPECT_EQ(1U, cl_from_string.argv().size());
[email protected]75f1c782011-07-13 23:41:22179 EXPECT_TRUE(cl_from_string.GetArgs().empty());
[email protected]f3adb5c2008-08-07 20:07:32180#endif
Ivan Kotenkova16212a52017-11-08 12:37:33181 CommandLine cl_from_argv(0, nullptr);
[email protected]61a4c6f2011-07-20 04:54:52182 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24183 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
184 EXPECT_EQ(1U, cl_from_argv.argv().size());
[email protected]75f1c782011-07-13 23:41:22185 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commitd7cae122008-07-26 21:49:38186}
187
[email protected]45f982e2012-10-29 21:31:31188TEST(CommandLineTest, GetArgumentsString) {
189 static const FilePath::CharType kPath1[] =
190 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
191 static const FilePath::CharType kPath2[] =
192 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
193
194 static const char kFirstArgName[] = "first-arg";
195 static const char kSecondArgName[] = "arg2";
196 static const char kThirdArgName[] = "arg with space";
197 static const char kFourthArgName[] = "nospace";
mgiucac974d5102014-10-01 09:24:51198 static const char kFifthArgName[] = "%1";
[email protected]45f982e2012-10-29 21:31:31199
200 CommandLine cl(CommandLine::NO_PROGRAM);
201 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
202 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
203 cl.AppendArg(kThirdArgName);
204 cl.AppendArg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51205 cl.AppendArg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31206
207#if defined(OS_WIN)
Jan Wilken Dörrieda77fd432019-10-24 21:40:34208 CommandLine::StringType expected_first_arg(UTF8ToWide(kFirstArgName));
209 CommandLine::StringType expected_second_arg(UTF8ToWide(kSecondArgName));
210 CommandLine::StringType expected_third_arg(UTF8ToWide(kThirdArgName));
211 CommandLine::StringType expected_fourth_arg(UTF8ToWide(kFourthArgName));
212 CommandLine::StringType expected_fifth_arg(UTF8ToWide(kFifthArgName));
Fabrice de Gans-Riberi306871de2018-05-16 19:38:39213#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]45f982e2012-10-29 21:31:31214 CommandLine::StringType expected_first_arg(kFirstArgName);
215 CommandLine::StringType expected_second_arg(kSecondArgName);
216 CommandLine::StringType expected_third_arg(kThirdArgName);
217 CommandLine::StringType expected_fourth_arg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51218 CommandLine::StringType expected_fifth_arg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31219#endif
220
221#if defined(OS_WIN)
222#define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
223#else
224#define QUOTE_ON_WIN FILE_PATH_LITERAL("")
225#endif // OS_WIN
226
227 CommandLine::StringType expected_str;
228 expected_str.append(FILE_PATH_LITERAL("--"))
229 .append(expected_first_arg)
230 .append(FILE_PATH_LITERAL("="))
231 .append(QUOTE_ON_WIN)
232 .append(kPath1)
233 .append(QUOTE_ON_WIN)
234 .append(FILE_PATH_LITERAL(" "))
235 .append(FILE_PATH_LITERAL("--"))
236 .append(expected_second_arg)
237 .append(FILE_PATH_LITERAL("="))
238 .append(QUOTE_ON_WIN)
239 .append(kPath2)
240 .append(QUOTE_ON_WIN)
241 .append(FILE_PATH_LITERAL(" "))
242 .append(QUOTE_ON_WIN)
243 .append(expected_third_arg)
244 .append(QUOTE_ON_WIN)
245 .append(FILE_PATH_LITERAL(" "))
mgiucac974d5102014-10-01 09:24:51246 .append(expected_fourth_arg)
247 .append(FILE_PATH_LITERAL(" "));
248
249 CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
250 expected_str_no_quote_placeholders.append(expected_fifth_arg);
251 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
252
253#if defined(OS_WIN)
254 CommandLine::StringType expected_str_quote_placeholders(expected_str);
255 expected_str_quote_placeholders.append(QUOTE_ON_WIN)
256 .append(expected_fifth_arg)
257 .append(QUOTE_ON_WIN);
258 EXPECT_EQ(expected_str_quote_placeholders,
259 cl.GetArgumentsStringWithPlaceholders());
260#endif
[email protected]45f982e2012-10-29 21:31:31261}
262
[email protected]bb975362009-01-21 01:00:22263// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38264TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25265 std::string switch1 = "switch1";
266 std::string switch2 = "switch2";
[email protected]a40ca4302011-05-14 01:10:24267 std::string value2 = "value";
[email protected]b7e0a2a2009-10-13 02:07:25268 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57269 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25270 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57271 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19272 std::string switch5 = "quotes";
[email protected]a40ca4302011-05-14 01:10:24273 CommandLine::StringType value5 = kTricky;
initial.commitd7cae122008-07-26 21:49:38274
[email protected]51343d5a2009-10-26 22:39:33275 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38276
[email protected]bb975362009-01-21 01:00:22277 cl.AppendSwitch(switch1);
[email protected]a40ca4302011-05-14 01:10:24278 cl.AppendSwitchASCII(switch2, value2);
[email protected]05076ba22010-07-30 05:59:57279 cl.AppendSwitchASCII(switch3, value3);
280 cl.AppendSwitchASCII(switch4, value4);
jackhou1bd9da92015-05-21 04:48:00281 cl.AppendSwitchASCII(switch5, value4);
[email protected]a40ca4302011-05-14 01:10:24282 cl.AppendSwitchNative(switch5, value5);
[email protected]bb975362009-01-21 01:00:22283
initial.commitd7cae122008-07-26 21:49:38284 EXPECT_TRUE(cl.HasSwitch(switch1));
285 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]a40ca4302011-05-14 01:10:24286 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38287 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57288 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20289 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57290 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19291 EXPECT_TRUE(cl.HasSwitch(switch5));
[email protected]a40ca4302011-05-14 01:10:24292 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
[email protected]98a1c2682010-08-10 18:14:19293
294#if defined(OS_WIN)
jdoerrie5c4dc4e2019-02-01 18:02:33295 EXPECT_EQ(
Jan Wilken Dörrieda77fd432019-10-24 21:40:34296 L"Program "
297 L"--switch1 "
298 L"--switch2=value "
299 L"--switch3=\"a value with spaces\" "
300 L"--switch4=\"\\\"a value with quotes\\\"\" "
301 // Even though the switches are unique, appending can add repeat
302 // switches to argv.
303 L"--quotes=\"\\\"a value with quotes\\\"\" "
304 L"--quotes=\"" +
305 kTrickyQuoted + L"\"",
jdoerrie5c4dc4e2019-02-01 18:02:33306 cl.GetCommandLineString());
[email protected]98a1c2682010-08-10 18:14:19307#endif
initial.commitd7cae122008-07-26 21:49:38308}
[email protected]e6124ad52010-11-15 04:17:52309
[email protected]a40ca4302011-05-14 01:10:24310TEST(CommandLineTest, AppendSwitchesDashDash) {
311 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
312 FILE_PATH_LITERAL("--"),
313 FILE_PATH_LITERAL("--arg1") };
jdoerrie5c4dc4e2019-02-01 18:02:33314 CommandLine cl(size(raw_argv), raw_argv);
[email protected]a40ca4302011-05-14 01:10:24315
Avi Drissmane3b70bf2019-01-04 19:50:22316 cl.AppendSwitch("switch1");
317 cl.AppendSwitchASCII("switch2", "foo");
[email protected]a40ca4302011-05-14 01:10:24318
Avi Drissmane3b70bf2019-01-04 19:50:22319 cl.AppendArg("--arg2");
[email protected]a40ca4302011-05-14 01:10:24320
Avi Drissmane3b70bf2019-01-04 19:50:22321 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
322 cl.GetCommandLineString());
323 CommandLine::StringVector cl_argv = cl.argv();
324 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
325 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
326 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
327 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
328 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
329 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
[email protected]a40ca4302011-05-14 01:10:24330}
331
[email protected]e6124ad52010-11-15 04:17:52332// Tests that when AppendArguments is called that the program is set correctly
333// on the target CommandLine object and the switches from the source
334// CommandLine are added to the target.
335TEST(CommandLineTest, AppendArguments) {
336 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
337 cl1.AppendSwitch("switch1");
338 cl1.AppendSwitchASCII("switch2", "foo");
339
340 CommandLine cl2(CommandLine::NO_PROGRAM);
341 cl2.AppendArguments(cl1, true);
342 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52343 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
[email protected]e6124ad52010-11-15 04:17:52344
345 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
346 c1.AppendSwitch("switch1");
347 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
348 c2.AppendSwitch("switch2");
349
350 c1.AppendArguments(c2, true);
351 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
352 EXPECT_TRUE(c1.HasSwitch("switch1"));
353 EXPECT_TRUE(c1.HasSwitch("switch2"));
354}
355
[email protected]450b34ec2010-11-29 21:12:22356#if defined(OS_WIN)
[email protected]61a4c6f2011-07-20 04:54:52357// Make sure that the command line string program paths are quoted as necessary.
[email protected]450b34ec2010-11-29 21:12:22358// This only makes sense on Windows and the test is basically here to guard
359// against regressions.
360TEST(CommandLineTest, ProgramQuotes) {
[email protected]a40ca4302011-05-14 01:10:24361 // Check that quotes are not added for paths without spaces.
Jan Wilken Dörrieda77fd432019-10-24 21:40:34362 const FilePath kProgram(L"Program");
[email protected]a40ca4302011-05-14 01:10:24363 CommandLine cl_program(kProgram);
364 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52365 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24366
Jan Wilken Dörrieda77fd432019-10-24 21:40:34367 const FilePath kProgramPath(L"Program Path");
[email protected]450b34ec2010-11-29 21:12:22368
369 // Check that quotes are not returned from GetProgram().
[email protected]a40ca4302011-05-14 01:10:24370 CommandLine cl_program_path(kProgramPath);
371 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
[email protected]450b34ec2010-11-29 21:12:22372
[email protected]a40ca4302011-05-14 01:10:24373 // Check that quotes are added to command line string paths containing spaces.
[email protected]61a4c6f2011-07-20 04:54:52374 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
Jan Wilken Dörrieda77fd432019-10-24 21:40:34375 EXPECT_EQ(L"\"Program Path\"", cmd_string);
mgiucac974d5102014-10-01 09:24:51376
377 // Check the optional quoting of placeholders in programs.
Jan Wilken Dörrieda77fd432019-10-24 21:40:34378 CommandLine cl_quote_placeholder(FilePath(L"%1"));
379 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
380 EXPECT_EQ(L"\"%1\"",
mgiucac974d5102014-10-01 09:24:51381 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
[email protected]450b34ec2010-11-29 21:12:22382}
383#endif
[email protected]f96fe2c42011-07-13 18:03:34384
385// Calling Init multiple times should not modify the previous CommandLine.
386TEST(CommandLineTest, Init) {
arihce89963ac2015-08-12 23:45:48387 // Call Init without checking output once so we know it's been called
388 // whether or not the test runner does so.
Ivan Kotenkova16212a52017-11-08 12:37:33389 CommandLine::Init(0, nullptr);
[email protected]f96fe2c42011-07-13 18:03:34390 CommandLine* initial = CommandLine::ForCurrentProcess();
Ivan Kotenkova16212a52017-11-08 12:37:33391 EXPECT_FALSE(CommandLine::Init(0, nullptr));
[email protected]f96fe2c42011-07-13 18:03:34392 CommandLine* current = CommandLine::ForCurrentProcess();
393 EXPECT_EQ(initial, current);
394}
pgal.u-szeged421dddb2014-11-25 12:55:02395
jackhou1bd9da92015-05-21 04:48:00396// Test that copies of CommandLine have a valid StringPiece map.
397TEST(CommandLineTest, Copy) {
dcheng093de9b2016-04-04 21:25:51398 std::unique_ptr<CommandLine> initial(
399 new CommandLine(CommandLine::NO_PROGRAM));
jackhou1bd9da92015-05-21 04:48:00400 initial->AppendSwitch("a");
401 initial->AppendSwitch("bbbbbbbbbbbbbbb");
402 initial->AppendSwitch("c");
403 CommandLine copy_constructed(*initial);
404 CommandLine assigned = *initial;
405 CommandLine::SwitchMap switch_map = initial->GetSwitches();
406 initial.reset();
407 for (const auto& pair : switch_map)
408 EXPECT_TRUE(copy_constructed.HasSwitch(pair.first));
409 for (const auto& pair : switch_map)
410 EXPECT_TRUE(assigned.HasSwitch(pair.first));
411}
412
skyostild851aa12017-03-29 17:38:35413TEST(CommandLineTest, PrependSimpleWrapper) {
414 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
415 cl.AppendSwitch("a");
416 cl.AppendSwitch("b");
417 cl.PrependWrapper(FILE_PATH_LITERAL("wrapper --foo --bar"));
418
419 EXPECT_EQ(6u, cl.argv().size());
420 EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
421 EXPECT_EQ(FILE_PATH_LITERAL("--foo"), cl.argv()[1]);
422 EXPECT_EQ(FILE_PATH_LITERAL("--bar"), cl.argv()[2]);
423 EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
424 EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
425 EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
426}
427
428TEST(CommandLineTest, PrependComplexWrapper) {
429 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
430 cl.AppendSwitch("a");
431 cl.AppendSwitch("b");
432 cl.PrependWrapper(
433 FILE_PATH_LITERAL("wrapper --foo='hello world' --bar=\"let's go\""));
434
435 EXPECT_EQ(6u, cl.argv().size());
436 EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]);
437 EXPECT_EQ(FILE_PATH_LITERAL("--foo='hello world'"), cl.argv()[1]);
438 EXPECT_EQ(FILE_PATH_LITERAL("--bar=\"let's go\""), cl.argv()[2]);
439 EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]);
440 EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]);
441 EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]);
442}
443
Avi Drissman1aa6cb92019-01-23 15:58:38444TEST(CommandLineTest, RemoveSwitch) {
Pavol Markobf16b812019-06-14 00:53:12445 const std::string switch1 = "switch1";
446 const std::string switch2 = "switch2";
447 const std::string value2 = "value";
Avi Drissman1aa6cb92019-01-23 15:58:38448
449 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
450
451 cl.AppendSwitch(switch1);
452 cl.AppendSwitchASCII(switch2, value2);
453
454 EXPECT_TRUE(cl.HasSwitch(switch1));
455 EXPECT_TRUE(cl.HasSwitch(switch2));
456 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
Pavol Markobf16b812019-06-14 00:53:12457 EXPECT_THAT(cl.argv(),
458 testing::ElementsAre(FILE_PATH_LITERAL("Program"),
459 FILE_PATH_LITERAL("--switch1"),
460 FILE_PATH_LITERAL("--switch2=value")));
Avi Drissman1aa6cb92019-01-23 15:58:38461
462 cl.RemoveSwitch(switch1);
463
464 EXPECT_FALSE(cl.HasSwitch(switch1));
465 EXPECT_TRUE(cl.HasSwitch(switch2));
466 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
Pavol Markobf16b812019-06-14 00:53:12467 EXPECT_THAT(cl.argv(),
468 testing::ElementsAre(FILE_PATH_LITERAL("Program"),
469 FILE_PATH_LITERAL("--switch2=value")));
470}
471
472TEST(CommandLineTest, RemoveSwitchWithValue) {
473 const std::string switch1 = "switch1";
474 const std::string switch2 = "switch2";
475 const std::string value2 = "value";
476
477 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
478
479 cl.AppendSwitch(switch1);
480 cl.AppendSwitchASCII(switch2, value2);
481
482 EXPECT_TRUE(cl.HasSwitch(switch1));
483 EXPECT_TRUE(cl.HasSwitch(switch2));
484 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
485 EXPECT_THAT(cl.argv(),
486 testing::ElementsAre(FILE_PATH_LITERAL("Program"),
487 FILE_PATH_LITERAL("--switch1"),
488 FILE_PATH_LITERAL("--switch2=value")));
489
490 cl.RemoveSwitch(switch2);
491
492 EXPECT_TRUE(cl.HasSwitch(switch1));
493 EXPECT_FALSE(cl.HasSwitch(switch2));
494 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
495 FILE_PATH_LITERAL("--switch1")));
496}
497
Andrei Polushin2ec89bc2019-07-30 20:47:17498TEST(CommandLineTest, RemoveSwitchDropsMultipleSameSwitches) {
499 const std::string switch1 = "switch1";
500 const std::string value2 = "value2";
501
502 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
503
504 cl.AppendSwitch(switch1);
505 cl.AppendSwitchASCII(switch1, value2);
506
507 EXPECT_TRUE(cl.HasSwitch(switch1));
508 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch1));
509 EXPECT_THAT(cl.argv(),
510 testing::ElementsAre(FILE_PATH_LITERAL("Program"),
511 FILE_PATH_LITERAL("--switch1"),
512 FILE_PATH_LITERAL("--switch1=value2")));
513
514 cl.RemoveSwitch(switch1);
515
516 EXPECT_FALSE(cl.HasSwitch(switch1));
517 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program")));
518}
519
Pavol Markobf16b812019-06-14 00:53:12520TEST(CommandLineTest, AppendAndRemoveSwitchWithDefaultPrefix) {
521 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
522
523 cl.AppendSwitch("foo");
524 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
525 FILE_PATH_LITERAL("--foo")));
526 EXPECT_EQ(0u, cl.GetArgs().size());
527
528 cl.RemoveSwitch("foo");
529 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program")));
530 EXPECT_EQ(0u, cl.GetArgs().size());
531}
532
533TEST(CommandLineTest, AppendAndRemoveSwitchWithAlternativePrefix) {
534 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
535
536 cl.AppendSwitch("-foo");
537 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
538 FILE_PATH_LITERAL("-foo")));
539 EXPECT_EQ(0u, cl.GetArgs().size());
540
541 cl.RemoveSwitch("foo");
542 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program")));
543 EXPECT_EQ(0u, cl.GetArgs().size());
544}
545
546TEST(CommandLineTest, AppendAndRemoveSwitchPreservesOtherSwitchesAndArgs) {
547 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
548
549 cl.AppendSwitch("foo");
550 cl.AppendSwitch("bar");
551 cl.AppendArg("arg");
552 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
553 FILE_PATH_LITERAL("--foo"),
554 FILE_PATH_LITERAL("--bar"),
555 FILE_PATH_LITERAL("arg")));
556 EXPECT_THAT(cl.GetArgs(), testing::ElementsAre(FILE_PATH_LITERAL("arg")));
557
558 cl.RemoveSwitch("foo");
559 EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"),
560 FILE_PATH_LITERAL("--bar"),
561 FILE_PATH_LITERAL("arg")));
562 EXPECT_THAT(cl.GetArgs(), testing::ElementsAre(FILE_PATH_LITERAL("arg")));
Avi Drissman1aa6cb92019-01-23 15:58:38563}
564
John Rummellb1d5fcb2019-04-27 01:13:33565TEST(CommandLineTest, MultipleSameSwitch) {
566 const CommandLine::CharType* argv[] = {
567 FILE_PATH_LITERAL("program"),
568 FILE_PATH_LITERAL("--foo=one"), // --foo first time
569 FILE_PATH_LITERAL("-baz"),
570 FILE_PATH_LITERAL("--foo=two") // --foo second time
571 };
572 CommandLine cl(size(argv), argv);
573
574 EXPECT_TRUE(cl.HasSwitch("foo"));
575 EXPECT_TRUE(cl.HasSwitch("baz"));
576
577 EXPECT_EQ("two", cl.GetSwitchValueASCII("foo"));
578}
579
pgal.u-szeged421dddb2014-11-25 12:55:02580} // namespace base