blob: ac8a39567c31d3e39fad2c5a7e5ef6533d02c401 [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
5#include <string>
6#include <vector>
7
tommi@chromium.orgf6e64922010-11-15 09:18:30 +09008#include "base/basictypes.h"
tommi@chromium.org877e84e2010-11-15 13:17:52 +09009#include "base/command_line.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090010#include "base/files/file_path.h"
jackhoua5995af2015-05-21 13:48:00 +090011#include "base/memory/scoped_ptr.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090012#include "base/strings/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090013#include "testing/gtest/include/gtest/gtest.h"
14
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +090015namespace base {
brettw@chromium.org82bcf512013-02-17 14:07:23 +090016
evan@chromium.orgcdf80852010-08-11 03:14:19 +090017// To test Windows quoting behavior, we use a string that has some backslashes
18// and quotes.
19// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
20// Here it is with C-style escapes.
msw@chromium.org53fbac12011-05-14 10:10:24 +090021static const CommandLine::StringType kTrickyQuoted =
22 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
evan@chromium.orgcdf80852010-08-11 03:14:19 +090023// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
24// Here that is with C-style escapes.
msw@chromium.org53fbac12011-05-14 10:10:24 +090025static const CommandLine::StringType kTricky =
26 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
evan@chromium.orgcdf80852010-08-11 03:14:19 +090027
initial.commit3f4a7322008-07-27 06:49:38 +090028TEST(CommandLineTest, CommandLineConstructor) {
msw@chromium.org53fbac12011-05-14 10:10:24 +090029 const CommandLine::CharType* argv[] = {
30 FILE_PATH_LITERAL("program"),
31 FILE_PATH_LITERAL("--foo="),
32 FILE_PATH_LITERAL("-bAr"),
33 FILE_PATH_LITERAL("-spaetzel=pierogi"),
34 FILE_PATH_LITERAL("-baz"),
35 FILE_PATH_LITERAL("flim"),
36 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
37 FILE_PATH_LITERAL("-spaetzle=Crepe"),
38 FILE_PATH_LITERAL("-=loosevalue"),
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090039 FILE_PATH_LITERAL("-"),
msw@chromium.org53fbac12011-05-14 10:10:24 +090040 FILE_PATH_LITERAL("FLAN"),
joth@chromium.orgb23d1842011-09-14 00:45:34 +090041 FILE_PATH_LITERAL("a"),
msw@chromium.org53fbac12011-05-14 10:10:24 +090042 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
43 FILE_PATH_LITERAL("--"),
44 FILE_PATH_LITERAL("--"),
45 FILE_PATH_LITERAL("--not-a-switch"),
46 FILE_PATH_LITERAL("\"in the time of submarines...\""),
47 FILE_PATH_LITERAL("unquoted arg-with-space")};
48 CommandLine cl(arraysize(argv), argv);
49
msw@chromium.orgc57622c2011-07-20 13:54:52 +090050 EXPECT_FALSE(cl.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +090051 EXPECT_FALSE(cl.HasSwitch("cruller"));
52 EXPECT_FALSE(cl.HasSwitch("flim"));
53 EXPECT_FALSE(cl.HasSwitch("program"));
54 EXPECT_FALSE(cl.HasSwitch("dog"));
55 EXPECT_FALSE(cl.HasSwitch("cat"));
56 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
57 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
58 EXPECT_FALSE(cl.HasSwitch("--"));
59
60 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
61 cl.GetProgram().value());
62
63 EXPECT_TRUE(cl.HasSwitch("foo"));
jackhou5c9fc1f2015-04-22 11:21:44 +090064#if defined(OS_WIN)
65 EXPECT_TRUE(cl.HasSwitch("bar"));
66#else
67 EXPECT_FALSE(cl.HasSwitch("bar"));
68#endif
msw@chromium.org53fbac12011-05-14 10:10:24 +090069 EXPECT_TRUE(cl.HasSwitch("baz"));
70 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
msw@chromium.org53fbac12011-05-14 10:10:24 +090071 EXPECT_TRUE(cl.HasSwitch("other-switches"));
72 EXPECT_TRUE(cl.HasSwitch("input-translation"));
73
74 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhoua5995af2015-05-21 13:48:00 +090075 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
msw@chromium.org53fbac12011-05-14 10:10:24 +090076 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
77 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
78 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
79 "other-switches"));
80 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
81
msw@chromium.orga25adf42011-07-14 08:41:22 +090082 const CommandLine::StringVector& args = cl.GetArgs();
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090083 ASSERT_EQ(8U, args.size());
msw@chromium.org53fbac12011-05-14 10:10:24 +090084
85 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
86 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
87 ++iter;
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090088 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
89 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +090090 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
91 ++iter;
joth@chromium.orgb23d1842011-09-14 00:45:34 +090092 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
93 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +090094 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
95 ++iter;
96 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
97 ++iter;
98 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
99 ++iter;
100 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
101 ++iter;
102 EXPECT_TRUE(iter == args.end());
103}
104
105TEST(CommandLineTest, CommandLineFromString) {
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900106#if defined(OS_WIN)
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900107 CommandLine cl = CommandLine::FromString(
msw@chromium.org53fbac12011-05-14 10:10:24 +0900108 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
109 L"--other-switches=\"--dog=canine --cat=feline\" "
110 L"-spaetzle=Crepe -=loosevalue FLAN "
111 L"--input-translation=\"45\"--output-rotation "
112 L"--quotes=" + kTrickyQuoted + L" "
113 L"-- -- --not-a-switch "
114 L"\"in the time of submarines...\"");
115
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900116 EXPECT_FALSE(cl.GetCommandLineString().empty());
evan@chromium.orge3fca692009-10-13 11:07:25 +0900117 EXPECT_FALSE(cl.HasSwitch("cruller"));
118 EXPECT_FALSE(cl.HasSwitch("flim"));
119 EXPECT_FALSE(cl.HasSwitch("program"));
120 EXPECT_FALSE(cl.HasSwitch("dog"));
121 EXPECT_FALSE(cl.HasSwitch("cat"));
122 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
123 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
124 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commit3f4a7322008-07-27 06:49:38 +0900125
evan@chromium.orgafe7c022010-10-08 09:06:31 +0900126 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
127 cl.GetProgram().value());
initial.commit3f4a7322008-07-27 06:49:38 +0900128
evan@chromium.orge3fca692009-10-13 11:07:25 +0900129 EXPECT_TRUE(cl.HasSwitch("foo"));
130 EXPECT_TRUE(cl.HasSwitch("bar"));
131 EXPECT_TRUE(cl.HasSwitch("baz"));
132 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
evan@chromium.orge3fca692009-10-13 11:07:25 +0900133 EXPECT_TRUE(cl.HasSwitch("other-switches"));
134 EXPECT_TRUE(cl.HasSwitch("input-translation"));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900135 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commit3f4a7322008-07-27 06:49:38 +0900136
tony@chromium.org7bcc4c12009-11-07 04:55:16 +0900137 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
jackhoua5995af2015-05-21 13:48:00 +0900138 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
tony@chromium.org7bcc4c12009-11-07 04:55:16 +0900139 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
140 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
141 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
142 "other-switches"));
143 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900144 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commit3f4a7322008-07-27 06:49:38 +0900145
msw@chromium.orga25adf42011-07-14 08:41:22 +0900146 const CommandLine::StringVector& args = cl.GetArgs();
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900147 ASSERT_EQ(5U, args.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900148
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900149 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
150 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900151 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900152 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900153 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900154 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900155 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900156 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *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("in the time of submarines..."), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900159 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900160 EXPECT_TRUE(iter == args.end());
estade@chromium.org92c59f22008-10-16 06:59:08 +0900161
msw@chromium.org53fbac12011-05-14 10:10:24 +0900162 // Check that a generated string produces an equivalent command line.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900163 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
164 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
estade@chromium.org92c59f22008-10-16 06:59:08 +0900165#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900166}
167
initial.commit3f4a7322008-07-27 06:49:38 +0900168// Tests behavior with an empty input string.
169TEST(CommandLineTest, EmptyString) {
evanm@google.com91cdff82008-08-08 05:07:32 +0900170#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900171 CommandLine cl_from_string = CommandLine::FromString(L"");
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900172 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900173 EXPECT_TRUE(cl_from_string.GetProgram().empty());
174 EXPECT_EQ(1U, cl_from_string.argv().size());
msw@chromium.orga25adf42011-07-14 08:41:22 +0900175 EXPECT_TRUE(cl_from_string.GetArgs().empty());
evanm@google.com91cdff82008-08-08 05:07:32 +0900176#endif
msw@chromium.org53fbac12011-05-14 10:10:24 +0900177 CommandLine cl_from_argv(0, NULL);
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900178 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900179 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
180 EXPECT_EQ(1U, cl_from_argv.argv().size());
msw@chromium.orga25adf42011-07-14 08:41:22 +0900181 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900182}
183
gab@chromium.orge399ff42012-10-30 06:31:31 +0900184TEST(CommandLineTest, GetArgumentsString) {
185 static const FilePath::CharType kPath1[] =
186 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
187 static const FilePath::CharType kPath2[] =
188 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
189
190 static const char kFirstArgName[] = "first-arg";
191 static const char kSecondArgName[] = "arg2";
192 static const char kThirdArgName[] = "arg with space";
193 static const char kFourthArgName[] = "nospace";
mgiuca90d74492014-10-01 18:24:51 +0900194 static const char kFifthArgName[] = "%1";
gab@chromium.orge399ff42012-10-30 06:31:31 +0900195
196 CommandLine cl(CommandLine::NO_PROGRAM);
197 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
198 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
199 cl.AppendArg(kThirdArgName);
200 cl.AppendArg(kFourthArgName);
mgiuca90d74492014-10-01 18:24:51 +0900201 cl.AppendArg(kFifthArgName);
gab@chromium.orge399ff42012-10-30 06:31:31 +0900202
203#if defined(OS_WIN)
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900204 CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName));
205 CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName));
206 CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName));
207 CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName));
208 CommandLine::StringType expected_fifth_arg(UTF8ToUTF16(kFifthArgName));
gab@chromium.orge399ff42012-10-30 06:31:31 +0900209#elif defined(OS_POSIX)
210 CommandLine::StringType expected_first_arg(kFirstArgName);
211 CommandLine::StringType expected_second_arg(kSecondArgName);
212 CommandLine::StringType expected_third_arg(kThirdArgName);
213 CommandLine::StringType expected_fourth_arg(kFourthArgName);
mgiuca90d74492014-10-01 18:24:51 +0900214 CommandLine::StringType expected_fifth_arg(kFifthArgName);
gab@chromium.orge399ff42012-10-30 06:31:31 +0900215#endif
216
217#if defined(OS_WIN)
218#define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
219#else
220#define QUOTE_ON_WIN FILE_PATH_LITERAL("")
221#endif // OS_WIN
222
223 CommandLine::StringType expected_str;
224 expected_str.append(FILE_PATH_LITERAL("--"))
225 .append(expected_first_arg)
226 .append(FILE_PATH_LITERAL("="))
227 .append(QUOTE_ON_WIN)
228 .append(kPath1)
229 .append(QUOTE_ON_WIN)
230 .append(FILE_PATH_LITERAL(" "))
231 .append(FILE_PATH_LITERAL("--"))
232 .append(expected_second_arg)
233 .append(FILE_PATH_LITERAL("="))
234 .append(QUOTE_ON_WIN)
235 .append(kPath2)
236 .append(QUOTE_ON_WIN)
237 .append(FILE_PATH_LITERAL(" "))
238 .append(QUOTE_ON_WIN)
239 .append(expected_third_arg)
240 .append(QUOTE_ON_WIN)
241 .append(FILE_PATH_LITERAL(" "))
mgiuca90d74492014-10-01 18:24:51 +0900242 .append(expected_fourth_arg)
243 .append(FILE_PATH_LITERAL(" "));
244
245 CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
246 expected_str_no_quote_placeholders.append(expected_fifth_arg);
247 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
248
249#if defined(OS_WIN)
250 CommandLine::StringType expected_str_quote_placeholders(expected_str);
251 expected_str_quote_placeholders.append(QUOTE_ON_WIN)
252 .append(expected_fifth_arg)
253 .append(QUOTE_ON_WIN);
254 EXPECT_EQ(expected_str_quote_placeholders,
255 cl.GetArgumentsStringWithPlaceholders());
256#endif
gab@chromium.orge399ff42012-10-30 06:31:31 +0900257}
258
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900259// Test methods for appending switches to a command line.
initial.commit3f4a7322008-07-27 06:49:38 +0900260TEST(CommandLineTest, AppendSwitches) {
evan@chromium.orge3fca692009-10-13 11:07:25 +0900261 std::string switch1 = "switch1";
262 std::string switch2 = "switch2";
msw@chromium.org53fbac12011-05-14 10:10:24 +0900263 std::string value2 = "value";
evan@chromium.orge3fca692009-10-13 11:07:25 +0900264 std::string switch3 = "switch3";
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900265 std::string value3 = "a value with spaces";
evan@chromium.orge3fca692009-10-13 11:07:25 +0900266 std::string switch4 = "switch4";
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900267 std::string value4 = "\"a value with quotes\"";
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900268 std::string switch5 = "quotes";
msw@chromium.org53fbac12011-05-14 10:10:24 +0900269 CommandLine::StringType value5 = kTricky;
initial.commit3f4a7322008-07-27 06:49:38 +0900270
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900271 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commit3f4a7322008-07-27 06:49:38 +0900272
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900273 cl.AppendSwitch(switch1);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900274 cl.AppendSwitchASCII(switch2, value2);
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900275 cl.AppendSwitchASCII(switch3, value3);
276 cl.AppendSwitchASCII(switch4, value4);
jackhoua5995af2015-05-21 13:48:00 +0900277 cl.AppendSwitchASCII(switch5, value4);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900278 cl.AppendSwitchNative(switch5, value5);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900279
initial.commit3f4a7322008-07-27 06:49:38 +0900280 EXPECT_TRUE(cl.HasSwitch(switch1));
281 EXPECT_TRUE(cl.HasSwitch(switch2));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900282 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commit3f4a7322008-07-27 06:49:38 +0900283 EXPECT_TRUE(cl.HasSwitch(switch3));
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900284 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
estade@chromium.orga048e382008-10-11 06:38:20 +0900285 EXPECT_TRUE(cl.HasSwitch(switch4));
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900286 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900287 EXPECT_TRUE(cl.HasSwitch(switch5));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900288 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900289
290#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900291 EXPECT_EQ(L"Program "
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900292 L"--switch1 "
293 L"--switch2=value "
294 L"--switch3=\"a value with spaces\" "
295 L"--switch4=\"\\\"a value with quotes\\\"\" "
jackhoua5995af2015-05-21 13:48:00 +0900296 // Even though the switches are unique, appending can add repeat
297 // switches to argv.
298 L"--quotes=\"\\\"a value with quotes\\\"\" "
msw@chromium.org53fbac12011-05-14 10:10:24 +0900299 L"--quotes=\"" + kTrickyQuoted + L"\"",
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900300 cl.GetCommandLineString());
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900301#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900302}
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900303
msw@chromium.org53fbac12011-05-14 10:10:24 +0900304TEST(CommandLineTest, AppendSwitchesDashDash) {
305 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
306 FILE_PATH_LITERAL("--"),
307 FILE_PATH_LITERAL("--arg1") };
308 CommandLine cl(arraysize(raw_argv), raw_argv);
309
310 cl.AppendSwitch("switch1");
311 cl.AppendSwitchASCII("switch2", "foo");
312
313 cl.AppendArg("--arg2");
314
315 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900316 cl.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900317 CommandLine::StringVector cl_argv = cl.argv();
318 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
319 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
320 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
321 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
322 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
323 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
324}
325
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900326// Tests that when AppendArguments is called that the program is set correctly
327// on the target CommandLine object and the switches from the source
328// CommandLine are added to the target.
329TEST(CommandLineTest, AppendArguments) {
330 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
331 cl1.AppendSwitch("switch1");
332 cl1.AppendSwitchASCII("switch2", "foo");
333
334 CommandLine cl2(CommandLine::NO_PROGRAM);
335 cl2.AppendArguments(cl1, true);
336 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900337 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900338
339 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
340 c1.AppendSwitch("switch1");
341 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
342 c2.AppendSwitch("switch2");
343
344 c1.AppendArguments(c2, true);
345 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
346 EXPECT_TRUE(c1.HasSwitch("switch1"));
347 EXPECT_TRUE(c1.HasSwitch("switch2"));
348}
349
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900350#if defined(OS_WIN)
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900351// Make sure that the command line string program paths are quoted as necessary.
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900352// This only makes sense on Windows and the test is basically here to guard
353// against regressions.
354TEST(CommandLineTest, ProgramQuotes) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900355 // Check that quotes are not added for paths without spaces.
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900356 const FilePath kProgram(L"Program");
msw@chromium.org53fbac12011-05-14 10:10:24 +0900357 CommandLine cl_program(kProgram);
358 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900359 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900360
361 const FilePath kProgramPath(L"Program Path");
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900362
363 // Check that quotes are not returned from GetProgram().
msw@chromium.org53fbac12011-05-14 10:10:24 +0900364 CommandLine cl_program_path(kProgramPath);
365 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900366
msw@chromium.org53fbac12011-05-14 10:10:24 +0900367 // Check that quotes are added to command line string paths containing spaces.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900368 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
mgiuca6742f342014-09-24 13:55:31 +0900369 EXPECT_EQ(L"\"Program Path\"", cmd_string);
mgiuca90d74492014-10-01 18:24:51 +0900370
371 // Check the optional quoting of placeholders in programs.
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900372 CommandLine cl_quote_placeholder(FilePath(L"%1"));
mgiuca90d74492014-10-01 18:24:51 +0900373 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
374 EXPECT_EQ(L"\"%1\"",
375 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900376}
377#endif
rvargas@google.com58a11842011-07-14 03:03:34 +0900378
379// Calling Init multiple times should not modify the previous CommandLine.
380TEST(CommandLineTest, Init) {
arihc7614aae2015-08-13 08:45:48 +0900381 // Call Init without checking output once so we know it's been called
382 // whether or not the test runner does so.
383 CommandLine::Init(0, NULL);
rvargas@google.com58a11842011-07-14 03:03:34 +0900384 CommandLine* initial = CommandLine::ForCurrentProcess();
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900385 EXPECT_FALSE(CommandLine::Init(0, NULL));
rvargas@google.com58a11842011-07-14 03:03:34 +0900386 CommandLine* current = CommandLine::ForCurrentProcess();
387 EXPECT_EQ(initial, current);
388}
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900389
jackhoua5995af2015-05-21 13:48:00 +0900390// Test that copies of CommandLine have a valid StringPiece map.
391TEST(CommandLineTest, Copy) {
392 scoped_ptr<CommandLine> initial(new CommandLine(CommandLine::NO_PROGRAM));
393 initial->AppendSwitch("a");
394 initial->AppendSwitch("bbbbbbbbbbbbbbb");
395 initial->AppendSwitch("c");
396 CommandLine copy_constructed(*initial);
397 CommandLine assigned = *initial;
398 CommandLine::SwitchMap switch_map = initial->GetSwitches();
399 initial.reset();
400 for (const auto& pair : switch_map)
401 EXPECT_TRUE(copy_constructed.HasSwitch(pair.first));
402 for (const auto& pair : switch_map)
403 EXPECT_TRUE(assigned.HasSwitch(pair.first));
404}
405
pgal.u-szeged9c8f32a2014-11-25 21:55:02 +0900406} // namespace base