blob: 79c9aecc2a24e933bf7688fe12fa302db6adcfe4 [file] [log] [blame]
erikwright@chromium.org506c72b2012-02-28 03:38:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
dchengcc8e4d82016-04-05 06:25:51 +09005#include "base/command_line.h"
6
7#include <memory>
initial.commit3f4a7322008-07-27 06:49:38 +09008#include <string>
9#include <vector>
10
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090011#include "base/files/file_path.h"
avia6a6a682015-12-27 07:15:14 +090012#include "base/macros.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090013#include "base/strings/utf_string_conversions.h"
avia6a6a682015-12-27 07:15:14 +090014#include "build/build_config.h"
initial.commit3f4a7322008-07-27 06:49:38 +090015#include "testing/gtest/include/gtest/gtest.h"
16
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +090017namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090018
evan@chromium.orgcdf80852010-08-11 03:14:19 +090019// 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.
msw@chromium.org53fbac12011-05-14 10:10:24 +090023static const CommandLine::StringType kTrickyQuoted =
24 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
evan@chromium.orgcdf80852010-08-11 03:14:19 +090025// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
26// Here that is with C-style escapes.
msw@chromium.org53fbac12011-05-14 10:10:24 +090027static const CommandLine::StringType kTricky =
28 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
evan@chromium.orgcdf80852010-08-11 03:14:19 +090029
initial.commit3f4a7322008-07-27 06:49:38 +090030TEST(CommandLineTest, CommandLineConstructor) {
msw@chromium.org53fbac12011-05-14 10:10:24 +090031 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"),
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090041 FILE_PATH_LITERAL("-"),
msw@chromium.org53fbac12011-05-14 10:10:24 +090042 FILE_PATH_LITERAL("FLAN"),
joth@chromium.orgb23d1842011-09-14 00:45:34 +090043 FILE_PATH_LITERAL("a"),
msw@chromium.org53fbac12011-05-14 10:10:24 +090044 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
msw@chromium.orgc57622c2011-07-20 13:54:52 +090052 EXPECT_FALSE(cl.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +090053 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"));
jackhou5c9fc1f2015-04-22 11:21:44 +090066#if defined(OS_WIN)
67 EXPECT_TRUE(cl.HasSwitch("bar"));
68#else
69 EXPECT_FALSE(cl.HasSwitch("bar"));
70#endif
msw@chromium.org53fbac12011-05-14 10:10:24 +090071 EXPECT_TRUE(cl.HasSwitch("baz"));
72 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
msw@chromium.org53fbac12011-05-14 10:10:24 +090073 EXPECT_TRUE(cl.HasSwitch("other-switches"));
74 EXPECT_TRUE(cl.HasSwitch("input-translation"));
75
76 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhoua5995af2015-05-21 13:48:00 +090077 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
msw@chromium.org53fbac12011-05-14 10:10:24 +090078 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
msw@chromium.orga25adf42011-07-14 08:41:22 +090084 const CommandLine::StringVector& args = cl.GetArgs();
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090085 ASSERT_EQ(8U, args.size());
msw@chromium.org53fbac12011-05-14 10:10:24 +090086
87 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
88 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
89 ++iter;
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090090 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
91 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +090092 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
93 ++iter;
joth@chromium.orgb23d1842011-09-14 00:45:34 +090094 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
95 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +090096 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) {
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900108#if defined(OS_WIN)
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900109 CommandLine cl = CommandLine::FromString(
msw@chromium.org53fbac12011-05-14 10:10:24 +0900110 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
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900118 EXPECT_FALSE(cl.GetCommandLineString().empty());
evan@chromium.orge3fca692009-10-13 11:07:25 +0900119 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.commit3f4a7322008-07-27 06:49:38 +0900127
evan@chromium.orgafe7c022010-10-08 09:06:31 +0900128 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
129 cl.GetProgram().value());
initial.commit3f4a7322008-07-27 06:49:38 +0900130
evan@chromium.orge3fca692009-10-13 11:07:25 +0900131 EXPECT_TRUE(cl.HasSwitch("foo"));
132 EXPECT_TRUE(cl.HasSwitch("bar"));
133 EXPECT_TRUE(cl.HasSwitch("baz"));
134 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
evan@chromium.orge3fca692009-10-13 11:07:25 +0900135 EXPECT_TRUE(cl.HasSwitch("other-switches"));
136 EXPECT_TRUE(cl.HasSwitch("input-translation"));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900137 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commit3f4a7322008-07-27 06:49:38 +0900138
tony@chromium.org7bcc4c12009-11-07 04:55:16 +0900139 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhoua5995af2015-05-21 13:48:00 +0900140 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
tony@chromium.org7bcc4c12009-11-07 04:55:16 +0900141 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"));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900146 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commit3f4a7322008-07-27 06:49:38 +0900147
msw@chromium.orga25adf42011-07-14 08:41:22 +0900148 const CommandLine::StringVector& args = cl.GetArgs();
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900149 ASSERT_EQ(5U, args.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900150
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900151 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
152 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900153 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900154 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900155 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900156 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900157 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900158 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900159 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900160 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900161 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900162 EXPECT_TRUE(iter == args.end());
estade@chromium.org92c59f22008-10-16 06:59:08 +0900163
msw@chromium.org53fbac12011-05-14 10:10:24 +0900164 // Check that a generated string produces an equivalent command line.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900165 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
166 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
estade@chromium.org92c59f22008-10-16 06:59:08 +0900167#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900168}
169
initial.commit3f4a7322008-07-27 06:49:38 +0900170// Tests behavior with an empty input string.
171TEST(CommandLineTest, EmptyString) {
evanm@google.com91cdff82008-08-08 05:07:32 +0900172#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900173 CommandLine cl_from_string = CommandLine::FromString(L"");
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900174 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900175 EXPECT_TRUE(cl_from_string.GetProgram().empty());
176 EXPECT_EQ(1U, cl_from_string.argv().size());
msw@chromium.orga25adf42011-07-14 08:41:22 +0900177 EXPECT_TRUE(cl_from_string.GetArgs().empty());
evanm@google.com91cdff82008-08-08 05:07:32 +0900178#endif
msw@chromium.org53fbac12011-05-14 10:10:24 +0900179 CommandLine cl_from_argv(0, NULL);
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900180 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900181 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
182 EXPECT_EQ(1U, cl_from_argv.argv().size());
msw@chromium.orga25adf42011-07-14 08:41:22 +0900183 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900184}
185
gab@chromium.orge399ff42012-10-30 06:31:31 +0900186TEST(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";
mgiuca90d74492014-10-01 18:24:51 +0900196 static const char kFifthArgName[] = "%1";
gab@chromium.orge399ff42012-10-30 06:31:31 +0900197
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);
mgiuca90d74492014-10-01 18:24:51 +0900203 cl.AppendArg(kFifthArgName);
gab@chromium.orge399ff42012-10-30 06:31:31 +0900204
205#if defined(OS_WIN)
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900206 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));
gab@chromium.orge399ff42012-10-30 06:31:31 +0900211#elif defined(OS_POSIX)
212 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);
mgiuca90d74492014-10-01 18:24:51 +0900216 CommandLine::StringType expected_fifth_arg(kFifthArgName);
gab@chromium.orge399ff42012-10-30 06:31:31 +0900217#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(" "))
mgiuca90d74492014-10-01 18:24:51 +0900244 .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
gab@chromium.orge399ff42012-10-30 06:31:31 +0900259}
260
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900261// Test methods for appending switches to a command line.
initial.commit3f4a7322008-07-27 06:49:38 +0900262TEST(CommandLineTest, AppendSwitches) {
evan@chromium.orge3fca692009-10-13 11:07:25 +0900263 std::string switch1 = "switch1";
264 std::string switch2 = "switch2";
msw@chromium.org53fbac12011-05-14 10:10:24 +0900265 std::string value2 = "value";
evan@chromium.orge3fca692009-10-13 11:07:25 +0900266 std::string switch3 = "switch3";
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900267 std::string value3 = "a value with spaces";
evan@chromium.orge3fca692009-10-13 11:07:25 +0900268 std::string switch4 = "switch4";
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900269 std::string value4 = "\"a value with quotes\"";
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900270 std::string switch5 = "quotes";
msw@chromium.org53fbac12011-05-14 10:10:24 +0900271 CommandLine::StringType value5 = kTricky;
initial.commit3f4a7322008-07-27 06:49:38 +0900272
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900273 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commit3f4a7322008-07-27 06:49:38 +0900274
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900275 cl.AppendSwitch(switch1);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900276 cl.AppendSwitchASCII(switch2, value2);
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900277 cl.AppendSwitchASCII(switch3, value3);
278 cl.AppendSwitchASCII(switch4, value4);
jackhoua5995af2015-05-21 13:48:00 +0900279 cl.AppendSwitchASCII(switch5, value4);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900280 cl.AppendSwitchNative(switch5, value5);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900281
initial.commit3f4a7322008-07-27 06:49:38 +0900282 EXPECT_TRUE(cl.HasSwitch(switch1));
283 EXPECT_TRUE(cl.HasSwitch(switch2));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900284 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commit3f4a7322008-07-27 06:49:38 +0900285 EXPECT_TRUE(cl.HasSwitch(switch3));
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900286 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
estade@chromium.orga048e382008-10-11 06:38:20 +0900287 EXPECT_TRUE(cl.HasSwitch(switch4));
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900288 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900289 EXPECT_TRUE(cl.HasSwitch(switch5));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900290 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900291
292#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900293 EXPECT_EQ(L"Program "
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900294 L"--switch1 "
295 L"--switch2=value "
296 L"--switch3=\"a value with spaces\" "
297 L"--switch4=\"\\\"a value with quotes\\\"\" "
jackhoua5995af2015-05-21 13:48:00 +0900298 // Even though the switches are unique, appending can add repeat
299 // switches to argv.
300 L"--quotes=\"\\\"a value with quotes\\\"\" "
msw@chromium.org53fbac12011-05-14 10:10:24 +0900301 L"--quotes=\"" + kTrickyQuoted + L"\"",
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900302 cl.GetCommandLineString());
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900303#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900304}
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900305
msw@chromium.org53fbac12011-05-14 10:10:24 +0900306TEST(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"),
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900318 cl.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900319 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
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900328// 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());
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900339 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900340
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
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900352#if defined(OS_WIN)
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900353// Make sure that the command line string program paths are quoted as necessary.
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900354// This only makes sense on Windows and the test is basically here to guard
355// against regressions.
356TEST(CommandLineTest, ProgramQuotes) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900357 // Check that quotes are not added for paths without spaces.
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900358 const FilePath kProgram(L"Program");
msw@chromium.org53fbac12011-05-14 10:10:24 +0900359 CommandLine cl_program(kProgram);
360 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900361 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900362
363 const FilePath kProgramPath(L"Program Path");
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900364
365 // Check that quotes are not returned from GetProgram().
msw@chromium.org53fbac12011-05-14 10:10:24 +0900366 CommandLine cl_program_path(kProgramPath);
367 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900368
msw@chromium.org53fbac12011-05-14 10:10:24 +0900369 // Check that quotes are added to command line string paths containing spaces.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900370 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
mgiuca6742f342014-09-24 13:55:31 +0900371 EXPECT_EQ(L"\"Program Path\"", cmd_string);
mgiuca90d74492014-10-01 18:24:51 +0900372
373 // Check the optional quoting of placeholders in programs.
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900374 CommandLine cl_quote_placeholder(FilePath(L"%1"));
mgiuca90d74492014-10-01 18:24:51 +0900375 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
376 EXPECT_EQ(L"\"%1\"",
377 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900378}
379#endif
rvargas@google.com58a11842011-07-14 03:03:34 +0900380
381// Calling Init multiple times should not modify the previous CommandLine.
382TEST(CommandLineTest, Init) {
arihc7614aae2015-08-13 08:45:48 +0900383 // Call Init without checking output once so we know it's been called
384 // whether or not the test runner does so.
385 CommandLine::Init(0, NULL);
rvargas@google.com58a11842011-07-14 03:03:34 +0900386 CommandLine* initial = CommandLine::ForCurrentProcess();
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900387 EXPECT_FALSE(CommandLine::Init(0, NULL));
rvargas@google.com58a11842011-07-14 03:03:34 +0900388 CommandLine* current = CommandLine::ForCurrentProcess();
389 EXPECT_EQ(initial, current);
390}
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900391
jackhoua5995af2015-05-21 13:48:00 +0900392// Test that copies of CommandLine have a valid StringPiece map.
393TEST(CommandLineTest, Copy) {
dchengcc8e4d82016-04-05 06:25:51 +0900394 std::unique_ptr<CommandLine> initial(
395 new CommandLine(CommandLine::NO_PROGRAM));
jackhoua5995af2015-05-21 13:48:00 +0900396 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
skyostil09e952b2017-03-30 02:38:35 +0900409TEST(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-szeged9c8f32a2014-11-25 21:55:02 +0900440} // namespace base