[email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 5 | #include "base/command_line.h" |
| 6 | |
| 7 | #include <memory> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 17 | namespace base { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 18 | |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 19 | // To test Windows quoting behavior, we use a string that has some backslashes |
| 20 | // and quotes. |
| 21 | // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" |
| 22 | // Here it is with C-style escapes. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 23 | static const CommandLine::StringType kTrickyQuoted = |
| 24 | FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\""); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 25 | // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 26 | // Here that is with C-style escapes. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 27 | static const CommandLine::StringType kTricky = |
| 28 | FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\""); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 29 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 31 | const CommandLine::CharType* argv[] = { |
| 32 | FILE_PATH_LITERAL("program"), |
| 33 | FILE_PATH_LITERAL("--foo="), |
| 34 | FILE_PATH_LITERAL("-bAr"), |
| 35 | FILE_PATH_LITERAL("-spaetzel=pierogi"), |
| 36 | FILE_PATH_LITERAL("-baz"), |
| 37 | FILE_PATH_LITERAL("flim"), |
| 38 | FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"), |
| 39 | FILE_PATH_LITERAL("-spaetzle=Crepe"), |
| 40 | FILE_PATH_LITERAL("-=loosevalue"), |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 41 | FILE_PATH_LITERAL("-"), |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 42 | FILE_PATH_LITERAL("FLAN"), |
[email protected] | 1fa39f0 | 2011-09-13 15:45:34 | [diff] [blame] | 43 | FILE_PATH_LITERAL("a"), |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 44 | FILE_PATH_LITERAL("--input-translation=45--output-rotation"), |
| 45 | FILE_PATH_LITERAL("--"), |
| 46 | FILE_PATH_LITERAL("--"), |
| 47 | FILE_PATH_LITERAL("--not-a-switch"), |
| 48 | FILE_PATH_LITERAL("\"in the time of submarines...\""), |
| 49 | FILE_PATH_LITERAL("unquoted arg-with-space")}; |
| 50 | CommandLine cl(arraysize(argv), argv); |
| 51 | |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 52 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 53 | 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")); |
jackhou | b20cbb4 | 2015-04-22 02:21:44 | [diff] [blame] | 66 | #if defined(OS_WIN) |
| 67 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 68 | #else |
| 69 | EXPECT_FALSE(cl.HasSwitch("bar")); |
| 70 | #endif |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 71 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 72 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 73 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 74 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
| 75 | |
| 76 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 77 | EXPECT_EQ("", cl.GetSwitchValueASCII("foo")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 78 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 79 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 80 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 81 | "other-switches")); |
| 82 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 83 | |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 84 | const CommandLine::StringVector& args = cl.GetArgs(); |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 85 | ASSERT_EQ(8U, args.size()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 86 | |
jdoerrie | 1c4b8ff | 2018-10-03 00:10:57 | [diff] [blame] | 87 | auto iter = args.begin(); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 88 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
| 89 | ++iter; |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 90 | EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter); |
| 91 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 92 | EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
| 93 | ++iter; |
[email protected] | 1fa39f0 | 2011-09-13 15:45:34 | [diff] [blame] | 94 | EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter); |
| 95 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 96 | 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 | |
| 107 | TEST(CommandLineTest, CommandLineFromString) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 108 | #if defined(OS_WIN) |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 109 | CommandLine cl = CommandLine::FromString( |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 110 | L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 111 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 112 | L"-spaetzle=Crepe -=loosevalue FLAN " |
| 113 | L"--input-translation=\"45\"--output-rotation " |
| 114 | L"--quotes=" + kTrickyQuoted + L" " |
| 115 | L"-- -- --not-a-switch " |
| 116 | L"\"in the time of submarines...\""); |
| 117 | |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 118 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 119 | 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 127 | |
[email protected] | 78c4c42 | 2010-10-08 00:06:31 | [diff] [blame] | 128 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 129 | cl.GetProgram().value()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 131 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 132 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 133 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 134 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 135 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 136 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 137 | EXPECT_TRUE(cl.HasSwitch("quotes")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 138 | |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 139 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 140 | EXPECT_EQ("", cl.GetSwitchValueASCII("foo")); |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 141 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 142 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 143 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 144 | "other-switches")); |
| 145 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 146 | EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 147 | |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 148 | const CommandLine::StringVector& args = cl.GetArgs(); |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 149 | ASSERT_EQ(5U, args.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 151 | std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
| 152 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 153 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 154 | EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 155 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 156 | EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 157 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 158 | EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 159 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 160 | EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 161 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 162 | EXPECT_TRUE(iter == args.end()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 163 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 164 | // Check that a generated string produces an equivalent command line. |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 165 | CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString()); |
| 166 | EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 167 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | } |
| 169 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | // Tests behavior with an empty input string. |
| 171 | TEST(CommandLineTest, EmptyString) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 172 | #if defined(OS_WIN) |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 173 | CommandLine cl_from_string = CommandLine::FromString(L""); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 174 | EXPECT_TRUE(cl_from_string.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 175 | EXPECT_TRUE(cl_from_string.GetProgram().empty()); |
| 176 | EXPECT_EQ(1U, cl_from_string.argv().size()); |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 177 | EXPECT_TRUE(cl_from_string.GetArgs().empty()); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 178 | #endif |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 179 | CommandLine cl_from_argv(0, nullptr); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 180 | EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 181 | EXPECT_TRUE(cl_from_argv.GetProgram().empty()); |
| 182 | EXPECT_EQ(1U, cl_from_argv.argv().size()); |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 183 | EXPECT_TRUE(cl_from_argv.GetArgs().empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 186 | TEST(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"; |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 196 | static const char kFifthArgName[] = "%1"; |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 197 | |
| 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); |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 203 | cl.AppendArg(kFifthArgName); |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 204 | |
| 205 | #if defined(OS_WIN) |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 206 | CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName)); |
| 207 | CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName)); |
| 208 | CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName)); |
| 209 | CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName)); |
| 210 | CommandLine::StringType expected_fifth_arg(UTF8ToUTF16(kFifthArgName)); |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 211 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 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); |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 216 | CommandLine::StringType expected_fifth_arg(kFifthArgName); |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 217 | #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(" ")) |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 244 | .append(expected_fourth_arg) |
| 245 | .append(FILE_PATH_LITERAL(" ")); |
| 246 | |
| 247 | CommandLine::StringType expected_str_no_quote_placeholders(expected_str); |
| 248 | expected_str_no_quote_placeholders.append(expected_fifth_arg); |
| 249 | EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString()); |
| 250 | |
| 251 | #if defined(OS_WIN) |
| 252 | CommandLine::StringType expected_str_quote_placeholders(expected_str); |
| 253 | expected_str_quote_placeholders.append(QUOTE_ON_WIN) |
| 254 | .append(expected_fifth_arg) |
| 255 | .append(QUOTE_ON_WIN); |
| 256 | EXPECT_EQ(expected_str_quote_placeholders, |
| 257 | cl.GetArgumentsStringWithPlaceholders()); |
| 258 | #endif |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 261 | // Test methods for appending switches to a command line. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | TEST(CommandLineTest, AppendSwitches) { |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 263 | std::string switch1 = "switch1"; |
| 264 | std::string switch2 = "switch2"; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 265 | std::string value2 = "value"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 266 | std::string switch3 = "switch3"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 267 | std::string value3 = "a value with spaces"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 268 | std::string switch4 = "switch4"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 269 | std::string value4 = "\"a value with quotes\""; |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 270 | std::string switch5 = "quotes"; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 271 | CommandLine::StringType value5 = kTricky; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 272 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 273 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 274 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 275 | cl.AppendSwitch(switch1); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 276 | cl.AppendSwitchASCII(switch2, value2); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 277 | cl.AppendSwitchASCII(switch3, value3); |
| 278 | cl.AppendSwitchASCII(switch4, value4); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 279 | cl.AppendSwitchASCII(switch5, value4); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 280 | cl.AppendSwitchNative(switch5, value5); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 281 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 282 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 283 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 284 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 285 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 286 | EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
[email protected] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 287 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 288 | EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 289 | EXPECT_TRUE(cl.HasSwitch(switch5)); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 290 | EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 291 | |
| 292 | #if defined(OS_WIN) |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 293 | EXPECT_EQ(L"Program " |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 294 | L"--switch1 " |
| 295 | L"--switch2=value " |
| 296 | L"--switch3=\"a value with spaces\" " |
| 297 | L"--switch4=\"\\\"a value with quotes\\\"\" " |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 298 | // Even though the switches are unique, appending can add repeat |
| 299 | // switches to argv. |
| 300 | L"--quotes=\"\\\"a value with quotes\\\"\" " |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 301 | L"--quotes=\"" + kTrickyQuoted + L"\"", |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 302 | cl.GetCommandLineString()); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 303 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 304 | } |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 305 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 306 | TEST(CommandLineTest, AppendSwitchesDashDash) { |
| 307 | const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), |
| 308 | FILE_PATH_LITERAL("--"), |
| 309 | FILE_PATH_LITERAL("--arg1") }; |
| 310 | CommandLine cl(arraysize(raw_argv), raw_argv); |
| 311 | |
| 312 | cl.AppendSwitch("switch1"); |
| 313 | cl.AppendSwitchASCII("switch2", "foo"); |
| 314 | |
| 315 | cl.AppendArg("--arg2"); |
| 316 | |
| 317 | EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 318 | cl.GetCommandLineString()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 319 | CommandLine::StringVector cl_argv = cl.argv(); |
| 320 | EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); |
| 321 | EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); |
| 322 | EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); |
| 323 | EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); |
| 324 | EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); |
| 325 | EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); |
| 326 | } |
| 327 | |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 328 | // 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. |
| 331 | TEST(CommandLineTest, AppendArguments) { |
| 332 | CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
| 333 | cl1.AppendSwitch("switch1"); |
| 334 | cl1.AppendSwitchASCII("switch2", "foo"); |
| 335 | |
| 336 | CommandLine cl2(CommandLine::NO_PROGRAM); |
| 337 | cl2.AppendArguments(cl1, true); |
| 338 | EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 339 | EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString()); |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 340 | |
| 341 | CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
| 342 | c1.AppendSwitch("switch1"); |
| 343 | CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
| 344 | c2.AppendSwitch("switch2"); |
| 345 | |
| 346 | c1.AppendArguments(c2, true); |
| 347 | EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
| 348 | EXPECT_TRUE(c1.HasSwitch("switch1")); |
| 349 | EXPECT_TRUE(c1.HasSwitch("switch2")); |
| 350 | } |
| 351 | |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 352 | #if defined(OS_WIN) |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 353 | // Make sure that the command line string program paths are quoted as necessary. |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 354 | // This only makes sense on Windows and the test is basically here to guard |
| 355 | // against regressions. |
| 356 | TEST(CommandLineTest, ProgramQuotes) { |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 357 | // Check that quotes are not added for paths without spaces. |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 358 | const FilePath kProgram(L"Program"); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 359 | CommandLine cl_program(kProgram); |
| 360 | EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 361 | EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 362 | |
| 363 | const FilePath kProgramPath(L"Program Path"); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 364 | |
| 365 | // Check that quotes are not returned from GetProgram(). |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 366 | CommandLine cl_program_path(kProgramPath); |
| 367 | EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 368 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 369 | // Check that quotes are added to command line string paths containing spaces. |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 370 | CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); |
mgiuca | b937b242 | 2014-09-24 04:55:31 | [diff] [blame] | 371 | EXPECT_EQ(L"\"Program Path\"", cmd_string); |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 372 | |
| 373 | // Check the optional quoting of placeholders in programs. |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 374 | CommandLine cl_quote_placeholder(FilePath(L"%1")); |
mgiuca | c974d510 | 2014-10-01 09:24:51 | [diff] [blame] | 375 | EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString()); |
| 376 | EXPECT_EQ(L"\"%1\"", |
| 377 | cl_quote_placeholder.GetCommandLineStringWithPlaceholders()); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 378 | } |
| 379 | #endif |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 380 | |
| 381 | // Calling Init multiple times should not modify the previous CommandLine. |
| 382 | TEST(CommandLineTest, Init) { |
arihc | e89963ac | 2015-08-12 23:45:48 | [diff] [blame] | 383 | // Call Init without checking output once so we know it's been called |
| 384 | // whether or not the test runner does so. |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 385 | CommandLine::Init(0, nullptr); |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 386 | CommandLine* initial = CommandLine::ForCurrentProcess(); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 387 | EXPECT_FALSE(CommandLine::Init(0, nullptr)); |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 388 | CommandLine* current = CommandLine::ForCurrentProcess(); |
| 389 | EXPECT_EQ(initial, current); |
| 390 | } |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 391 | |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 392 | // Test that copies of CommandLine have a valid StringPiece map. |
| 393 | TEST(CommandLineTest, Copy) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 394 | std::unique_ptr<CommandLine> initial( |
| 395 | new CommandLine(CommandLine::NO_PROGRAM)); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 396 | 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 | |
skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 409 | TEST(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 | |
| 424 | TEST(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-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 440 | } // namespace base |