OptionsTest.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <string>
  21. #include <vector>
  22. #include <android-base/test_utils.h>
  23. #include <gtest/gtest.h>
  24. #include "Options.h"
  25. namespace android {
  26. namespace gtest_extras {
  27. TEST(OptionsTest, unknown_arg) {
  28. CapturedStdout capture;
  29. std::vector<const char*> cur_args{"ignore", "--unknown_arg"};
  30. std::vector<const char*> child_args;
  31. Options options;
  32. bool parsed = options.Process(cur_args, &child_args);
  33. capture.Stop();
  34. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  35. EXPECT_EQ("Unknown argument: --unknown_arg\n", capture.str());
  36. }
  37. TEST(OptionsTest, unknown_arg_single_dash) {
  38. CapturedStdout capture;
  39. std::vector<const char*> cur_args{"ignore", "-unknown_arg"};
  40. std::vector<const char*> child_args;
  41. Options options;
  42. bool parsed = options.Process(cur_args, &child_args);
  43. capture.Stop();
  44. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  45. EXPECT_EQ("Unknown argument: -unknown_arg\n", capture.str());
  46. }
  47. TEST(OptionsTest, extra_arg) {
  48. CapturedStdout capture;
  49. std::vector<const char*> cur_args{"ignore", "extra"};
  50. std::vector<const char*> child_args;
  51. Options options;
  52. bool parsed = options.Process(cur_args, &child_args);
  53. capture.Stop();
  54. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  55. EXPECT_EQ("Unexpected argument 'extra'\n", capture.str());
  56. }
  57. TEST(OptionsTest, check_defaults) {
  58. std::vector<const char*> cur_args{"ignore"};
  59. std::vector<const char*> child_args;
  60. Options options;
  61. ASSERT_TRUE(options.Process(cur_args, &child_args));
  62. EXPECT_LT(0U, options.job_count());
  63. EXPECT_EQ(90000ULL, options.deadline_threshold_ms());
  64. EXPECT_EQ(2000ULL, options.slow_threshold_ms());
  65. EXPECT_EQ(0ULL, options.shard_index());
  66. EXPECT_EQ(0ULL, options.total_shards());
  67. EXPECT_EQ("auto", options.color());
  68. EXPECT_EQ("", options.xml_file());
  69. EXPECT_EQ("", options.filter());
  70. EXPECT_EQ(1, options.num_iterations());
  71. EXPECT_TRUE(options.print_time());
  72. EXPECT_TRUE(options.gtest_format());
  73. EXPECT_FALSE(options.allow_disabled_tests());
  74. EXPECT_FALSE(options.list_tests());
  75. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  76. }
  77. TEST(OptionsTest, gtest_format) {
  78. std::vector<const char*> cur_args{"ignore", "--gtest_format"};
  79. std::vector<const char*> child_args;
  80. Options options;
  81. ASSERT_TRUE(options.Process(cur_args, &child_args));
  82. EXPECT_TRUE(options.gtest_format());
  83. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  84. }
  85. TEST(OptionsTest, gtest_format_error_argument) {
  86. CapturedStdout capture;
  87. std::vector<const char*> cur_args{"ignore", "--gtest_format=not_allowed"};
  88. std::vector<const char*> child_args;
  89. Options options;
  90. bool parsed = options.Process(cur_args, &child_args);
  91. capture.Stop();
  92. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  93. EXPECT_EQ("--gtest_format does not take an argument.\n", capture.str());
  94. }
  95. TEST(OptionsTest, no_gtest_format) {
  96. std::vector<const char*> cur_args{"ignore", "--no_gtest_format"};
  97. std::vector<const char*> child_args;
  98. Options options;
  99. ASSERT_TRUE(options.Process(cur_args, &child_args));
  100. EXPECT_FALSE(options.gtest_format());
  101. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  102. }
  103. TEST(OptionsTest, no_gtest_format_and_gtest_format) {
  104. std::vector<const char*> cur_args{"ignore", "--no_gtest_format", "--gtest_format"};
  105. std::vector<const char*> child_args;
  106. Options options;
  107. ASSERT_TRUE(options.Process(cur_args, &child_args));
  108. EXPECT_FALSE(options.gtest_format());
  109. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  110. }
  111. TEST(OptionsTest, no_gtest_format_error_argument) {
  112. CapturedStdout capture;
  113. std::vector<const char*> cur_args{"ignore", "--no_gtest_format=not_allowed"};
  114. std::vector<const char*> child_args;
  115. Options options;
  116. bool parsed = options.Process(cur_args, &child_args);
  117. capture.Stop();
  118. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  119. EXPECT_EQ("--no_gtest_format does not take an argument.\n", capture.str());
  120. }
  121. TEST(OptionsTest, gtest_list_tests) {
  122. std::vector<const char*> cur_args{"ignore", "--gtest_list_tests"};
  123. std::vector<const char*> child_args;
  124. Options options;
  125. ASSERT_TRUE(options.Process(cur_args, &child_args));
  126. EXPECT_TRUE(options.list_tests());
  127. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  128. }
  129. TEST(OptionsTest, gtest_list_tests_error_argument) {
  130. CapturedStdout capture;
  131. std::vector<const char*> cur_args{"ignore", "--gtest_list_tests=nothing"};
  132. std::vector<const char*> child_args;
  133. Options options;
  134. bool parsed = options.Process(cur_args, &child_args);
  135. capture.Stop();
  136. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  137. EXPECT_EQ("--gtest_list_tests does not take an argument.\n", capture.str());
  138. }
  139. TEST(OptionsTest, job_count_single_arg) {
  140. std::vector<const char*> cur_args{"ignore", "-j11"};
  141. std::vector<const char*> child_args;
  142. Options options;
  143. ASSERT_TRUE(options.Process(cur_args, &child_args));
  144. EXPECT_EQ(11U, options.job_count());
  145. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  146. }
  147. TEST(OptionsTest, job_count_second_arg) {
  148. std::vector<const char*> cur_args{"ignore", "-j", "23"};
  149. std::vector<const char*> child_args;
  150. Options options;
  151. ASSERT_TRUE(options.Process(cur_args, &child_args));
  152. EXPECT_EQ(23U, options.job_count());
  153. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  154. }
  155. TEST(OptionsTest, job_count_error_single_arg) {
  156. CapturedStdout capture;
  157. std::vector<const char*> cur_args{"ignore", "-j0bad"};
  158. std::vector<const char*> child_args;
  159. Options options;
  160. bool parsed = options.Process(cur_args, &child_args);
  161. capture.Stop();
  162. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  163. EXPECT_EQ("-j value is not formatted as a numeric value (0bad)\n", capture.str());
  164. }
  165. TEST(OptionsTest, job_count_error_second_arg) {
  166. CapturedStdout capture;
  167. std::vector<const char*> cur_args{"ignore", "-j", "34b"};
  168. std::vector<const char*> child_args;
  169. Options options;
  170. bool parsed = options.Process(cur_args, &child_args);
  171. capture.Stop();
  172. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  173. EXPECT_EQ("-j value is not formatted as a numeric value (34b)\n", capture.str());
  174. }
  175. TEST(OptionsTest, job_count_error_no_arg) {
  176. CapturedStdout capture;
  177. std::vector<const char*> cur_args{"ignore", "-j"};
  178. std::vector<const char*> child_args;
  179. Options options;
  180. bool parsed = options.Process(cur_args, &child_args);
  181. capture.Stop();
  182. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  183. EXPECT_EQ("-j requires an argument.\n", capture.str());
  184. }
  185. TEST(OptionsTest, deadline_threshold_ms) {
  186. std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms=3200"};
  187. std::vector<const char*> child_args;
  188. Options options;
  189. ASSERT_TRUE(options.Process(cur_args, &child_args));
  190. EXPECT_EQ(3200ULL, options.deadline_threshold_ms());
  191. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  192. }
  193. TEST(OptionsTest, deadline_threshold_ms_error_no_value) {
  194. CapturedStdout capture;
  195. std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms"};
  196. std::vector<const char*> child_args;
  197. Options options;
  198. bool parsed = options.Process(cur_args, &child_args);
  199. capture.Stop();
  200. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  201. EXPECT_EQ("--deadline_threshold_ms requires an argument.\n", capture.str());
  202. }
  203. TEST(OptionsTest, deadline_threshold_ms_error_not_a_number) {
  204. CapturedStdout capture;
  205. std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms=bad"};
  206. std::vector<const char*> child_args;
  207. Options options;
  208. bool parsed = options.Process(cur_args, &child_args);
  209. capture.Stop();
  210. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  211. EXPECT_EQ("--deadline_threshold_ms value is not formatted as a numeric value (bad)\n",
  212. capture.str());
  213. }
  214. TEST(OptionsTest, deadline_threshold_ms_error_illegal_value) {
  215. CapturedStdout capture;
  216. std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms=0"};
  217. std::vector<const char*> child_args;
  218. Options options;
  219. bool parsed = options.Process(cur_args, &child_args);
  220. capture.Stop();
  221. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  222. EXPECT_EQ("--deadline_threshold_ms requires a number greater than zero.\n", capture.str());
  223. }
  224. TEST(OptionsTest, slow_threshold_ms) {
  225. std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms=4580"};
  226. std::vector<const char*> child_args;
  227. Options options;
  228. ASSERT_TRUE(options.Process(cur_args, &child_args));
  229. EXPECT_EQ(4580ULL, options.slow_threshold_ms());
  230. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  231. }
  232. TEST(OptionsTest, slow_threshold_ms_error_no_value) {
  233. CapturedStdout capture;
  234. std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms"};
  235. std::vector<const char*> child_args;
  236. Options options;
  237. bool parsed = options.Process(cur_args, &child_args);
  238. capture.Stop();
  239. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  240. EXPECT_EQ("--slow_threshold_ms requires an argument.\n", capture.str());
  241. }
  242. TEST(OptionsTest, slow_threshold_ms_error_not_a_number) {
  243. CapturedStdout capture;
  244. Options options;
  245. std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms=not"};
  246. std::vector<const char*> child_args;
  247. bool parsed = options.Process(cur_args, &child_args);
  248. capture.Stop();
  249. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  250. EXPECT_EQ("--slow_threshold_ms value is not formatted as a numeric value (not)\n", capture.str());
  251. }
  252. TEST(OptionsTest, slow_threshold_ms_error_illegal_value) {
  253. CapturedStdout capture;
  254. std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms=0"};
  255. std::vector<const char*> child_args;
  256. Options options;
  257. bool parsed = options.Process(cur_args, &child_args);
  258. capture.Stop();
  259. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  260. EXPECT_EQ("--slow_threshold_ms requires a number greater than zero.\n", capture.str());
  261. }
  262. TEST(OptionsTest, shard_index) {
  263. ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "100", 1));
  264. std::vector<const char*> cur_args{"ignore"};
  265. std::vector<const char*> child_args;
  266. Options options;
  267. ASSERT_TRUE(options.Process(cur_args, &child_args));
  268. EXPECT_EQ(100ULL, options.shard_index());
  269. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  270. ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "0", 1));
  271. ASSERT_TRUE(options.Process(cur_args, &child_args));
  272. EXPECT_EQ(0ULL, options.shard_index());
  273. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  274. ASSERT_NE(-1, unsetenv("GTEST_SHARD_INDEX"));
  275. }
  276. TEST(OptionsTest, shard_index_error_no_value) {
  277. ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "", 1));
  278. CapturedStdout capture;
  279. std::vector<const char*> cur_args{"ignore"};
  280. std::vector<const char*> child_args;
  281. Options options;
  282. bool parsed = options.Process(cur_args, &child_args);
  283. capture.Stop();
  284. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  285. EXPECT_EQ("env[GTEST_SHARD_INDEX] requires an argument.\n", capture.str());
  286. ASSERT_NE(-1, unsetenv("GTEST_SHARD_INDEX"));
  287. }
  288. TEST(OptionsTest, shard_index_error_not_a_number) {
  289. ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "bad", 1));
  290. CapturedStdout capture;
  291. std::vector<const char*> cur_args{"ignore"};
  292. std::vector<const char*> child_args;
  293. Options options;
  294. bool parsed = options.Process(cur_args, &child_args);
  295. capture.Stop();
  296. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  297. EXPECT_EQ("env[GTEST_SHARD_INDEX] value is not formatted as a numeric value (bad)\n",
  298. capture.str());
  299. ASSERT_NE(-1, unsetenv("GTEST_SHARD_INDEX"));
  300. }
  301. TEST(OptionsTest, shard_index_error_not_from_env) {
  302. CapturedStdout capture;
  303. std::vector<const char*> cur_args{"ignore", "--gtest_shard_index=100"};
  304. std::vector<const char*> child_args;
  305. Options options;
  306. bool parsed = options.Process(cur_args, &child_args);
  307. capture.Stop();
  308. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  309. EXPECT_EQ("--gtest_shard_index is only supported as an environment variable.\n", capture.str());
  310. }
  311. TEST(OptionsTest, total_shards) {
  312. ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "500", 1));
  313. std::vector<const char*> cur_args{"ignore"};
  314. std::vector<const char*> child_args;
  315. Options options;
  316. ASSERT_TRUE(options.Process(cur_args, &child_args));
  317. EXPECT_EQ(500ULL, options.total_shards());
  318. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  319. ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "0", 1));
  320. ASSERT_TRUE(options.Process(cur_args, &child_args));
  321. EXPECT_EQ(0ULL, options.total_shards());
  322. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  323. ASSERT_NE(-1, unsetenv("GTEST_TOTAL_SHARDS"));
  324. }
  325. TEST(OptionsTest, total_shards_error_no_value) {
  326. ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "", 1));
  327. CapturedStdout capture;
  328. std::vector<const char*> cur_args{"ignore"};
  329. std::vector<const char*> child_args;
  330. Options options;
  331. bool parsed = options.Process(cur_args, &child_args);
  332. capture.Stop();
  333. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  334. EXPECT_EQ("env[GTEST_TOTAL_SHARDS] requires an argument.\n", capture.str());
  335. ASSERT_NE(-1, unsetenv("GTEST_TOTAL_SHARDS"));
  336. }
  337. TEST(OptionsTest, total_shards_error_not_a_number) {
  338. ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "bad", 1));
  339. CapturedStdout capture;
  340. std::vector<const char*> cur_args{"ignore"};
  341. std::vector<const char*> child_args;
  342. Options options;
  343. bool parsed = options.Process(cur_args, &child_args);
  344. capture.Stop();
  345. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  346. EXPECT_EQ("env[GTEST_TOTAL_SHARDS] value is not formatted as a numeric value (bad)\n",
  347. capture.str());
  348. ASSERT_NE(-1, unsetenv("GTEST_TOTAL_SHARDS"));
  349. }
  350. TEST(OptionsTest, total_shards_error_not_from_env) {
  351. CapturedStdout capture;
  352. std::vector<const char*> cur_args{"ignore", "--gtest_total_shards=100"};
  353. std::vector<const char*> child_args;
  354. Options options;
  355. bool parsed = options.Process(cur_args, &child_args);
  356. capture.Stop();
  357. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  358. EXPECT_EQ("--gtest_total_shards is only supported as an environment variable.\n", capture.str());
  359. }
  360. TEST(OptionsTest, gtest_color) {
  361. std::vector<const char*> cur_args{"ignore", "--gtest_color=yes"};
  362. std::vector<const char*> child_args;
  363. Options options;
  364. ASSERT_TRUE(options.Process(cur_args, &child_args));
  365. EXPECT_EQ("yes", options.color());
  366. EXPECT_EQ((std::vector<const char*>{"ignore", "--gtest_color=yes"}), child_args);
  367. }
  368. TEST(OptionsTest, gtest_color_error_no_value) {
  369. CapturedStdout capture;
  370. std::vector<const char*> cur_args{"ignore", "--gtest_color="};
  371. std::vector<const char*> child_args;
  372. Options options;
  373. bool parsed = options.Process(cur_args, &child_args);
  374. capture.Stop();
  375. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  376. EXPECT_EQ("--gtest_color requires an argument.\n", capture.str());
  377. }
  378. TEST(OptionsTest, gtest_filter) {
  379. std::vector<const char*> cur_args{"ignore", "--gtest_filter=filter"};
  380. std::vector<const char*> child_args;
  381. Options options;
  382. ASSERT_TRUE(options.Process(cur_args, &child_args));
  383. EXPECT_EQ("filter", options.filter());
  384. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  385. }
  386. TEST(OptionsTest, gtest_filter_error_no_value) {
  387. CapturedStdout capture;
  388. std::vector<const char*> cur_args{"ignore", "--gtest_filter"};
  389. std::vector<const char*> child_args;
  390. Options options;
  391. bool parsed = options.Process(cur_args, &child_args);
  392. capture.Stop();
  393. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  394. EXPECT_EQ("--gtest_filter requires an argument.\n", capture.str());
  395. }
  396. TEST(OptionsTest, gtest_also_run_disabled_tests) {
  397. std::vector<const char*> cur_args{"ignore", "--gtest_also_run_disabled_tests"};
  398. std::vector<const char*> child_args;
  399. Options options;
  400. ASSERT_TRUE(options.Process(cur_args, &child_args));
  401. EXPECT_TRUE(options.allow_disabled_tests());
  402. EXPECT_EQ((std::vector<const char*>{"ignore", "--gtest_also_run_disabled_tests"}), child_args);
  403. }
  404. TEST(OptionsTest, gtest_also_run_disabled_tests_error_argument) {
  405. CapturedStdout capture;
  406. std::vector<const char*> cur_args{"ignore", "--gtest_also_run_disabled_tests=nothing"};
  407. std::vector<const char*> child_args;
  408. Options options;
  409. bool parsed = options.Process(cur_args, &child_args);
  410. capture.Stop();
  411. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  412. EXPECT_EQ("--gtest_also_run_disabled_tests does not take an argument.\n", capture.str());
  413. }
  414. TEST(OptionsTest, gtest_repeat) {
  415. std::vector<const char*> cur_args{"ignore", "--gtest_repeat=10"};
  416. std::vector<const char*> child_args;
  417. Options options;
  418. ASSERT_TRUE(options.Process(cur_args, &child_args));
  419. EXPECT_EQ(10, options.num_iterations());
  420. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  421. cur_args = std::vector<const char*>{"ignore", "--gtest_repeat=-1"};
  422. ASSERT_TRUE(options.Process(cur_args, &child_args));
  423. EXPECT_EQ(-1, options.num_iterations());
  424. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  425. }
  426. TEST(OptionsTest, gtest_repeat_error_no_value) {
  427. CapturedStdout capture;
  428. std::vector<const char*> cur_args{"ignore", "--gtest_repeat"};
  429. std::vector<const char*> child_args;
  430. Options options;
  431. bool parsed = options.Process(cur_args, &child_args);
  432. capture.Stop();
  433. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  434. EXPECT_EQ("--gtest_repeat requires an argument.\n", capture.str());
  435. }
  436. TEST(OptionsTest, gtest_repeat_error_overflow) {
  437. CapturedStdout capture;
  438. std::vector<const char*> cur_args{"ignore", "--gtest_repeat=2147483747"};
  439. std::vector<const char*> child_args;
  440. Options options;
  441. bool parsed = options.Process(cur_args, &child_args);
  442. capture.Stop();
  443. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  444. EXPECT_EQ("--gtest_repeat value overflows (2147483747)\n", capture.str());
  445. capture.Reset();
  446. capture.Start();
  447. cur_args = std::vector<const char*>{"ignore", "--gtest_repeat=-2147483747"};
  448. parsed = options.Process(cur_args, &child_args);
  449. capture.Stop();
  450. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  451. EXPECT_EQ("--gtest_repeat value overflows (-2147483747)\n", capture.str());
  452. }
  453. TEST(OptionsTest, gtest_print_time) {
  454. std::vector<const char*> cur_args{"ignore", "--gtest_print_time"};
  455. std::vector<const char*> child_args;
  456. Options options;
  457. ASSERT_TRUE(options.Process(cur_args, &child_args));
  458. EXPECT_TRUE(options.print_time());
  459. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  460. cur_args = std::vector<const char*>{"ignore", "--gtest_print_time=0"};
  461. ASSERT_TRUE(options.Process(cur_args, &child_args));
  462. EXPECT_FALSE(options.print_time());
  463. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  464. cur_args = std::vector<const char*>{"ignore", "--gtest_print_time=1"};
  465. ASSERT_TRUE(options.Process(cur_args, &child_args));
  466. EXPECT_TRUE(options.print_time());
  467. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  468. }
  469. TEST(OptionsTest, gtest_output) {
  470. std::vector<const char*> cur_args{"ignore", "--gtest_output=xml:/file.xml"};
  471. std::vector<const char*> child_args;
  472. Options options;
  473. ASSERT_TRUE(options.Process(cur_args, &child_args));
  474. EXPECT_EQ("/file.xml", options.xml_file());
  475. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  476. cur_args = std::vector<const char*>{"ignore", "--gtest_output=xml:/directory/"};
  477. ASSERT_TRUE(options.Process(cur_args, &child_args));
  478. EXPECT_EQ("/directory/test_details.xml", options.xml_file());
  479. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  480. cur_args = std::vector<const char*>{"ignore", "--gtest_output=xml:cwd.xml"};
  481. ASSERT_TRUE(options.Process(cur_args, &child_args));
  482. char* cwd = getcwd(nullptr, 0);
  483. std::string expected_file(cwd);
  484. expected_file += "/cwd.xml";
  485. free(cwd);
  486. EXPECT_EQ(expected_file, options.xml_file());
  487. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  488. }
  489. TEST(OptionsTest, gtest_output_error_no_value) {
  490. CapturedStdout capture;
  491. std::vector<const char*> cur_args{"ignore", "--gtest_output"};
  492. std::vector<const char*> child_args;
  493. Options options;
  494. bool parsed = options.Process(cur_args, &child_args);
  495. capture.Stop();
  496. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  497. EXPECT_EQ("--gtest_output requires an argument.\n", capture.str());
  498. }
  499. TEST(OptionsTest, gtest_output_error_no_xml) {
  500. CapturedStdout capture;
  501. std::vector<const char*> cur_args{"ignore", "--gtest_output=xml:"};
  502. std::vector<const char*> child_args;
  503. Options options;
  504. bool parsed = options.Process(cur_args, &child_args);
  505. capture.Stop();
  506. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  507. EXPECT_EQ("--gtest_output requires a file name after xml:\n", capture.str());
  508. capture.Reset();
  509. capture.Start();
  510. cur_args = std::vector<const char*>{"ignore", "--gtest_output=not_xml"};
  511. parsed = options.Process(cur_args, &child_args);
  512. capture.Stop();
  513. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  514. EXPECT_EQ("--gtest_output only supports an xml output file.\n", capture.str());
  515. }
  516. TEST(OptionsTest, gtest_death_test_style) {
  517. std::vector<const char*> cur_args{"ignore", "--gtest_death_test_style=something"};
  518. std::vector<const char*> child_args;
  519. Options options;
  520. ASSERT_TRUE(options.Process(cur_args, &child_args));
  521. EXPECT_EQ((std::vector<const char*>{"ignore", "--gtest_death_test_style=something"}), child_args);
  522. }
  523. TEST(OptionsTest, gtest_death_test_style_error_no_value) {
  524. CapturedStdout capture;
  525. std::vector<const char*> cur_args{"ignore", "--gtest_death_test_style"};
  526. std::vector<const char*> child_args;
  527. Options options;
  528. bool parsed = options.Process(cur_args, &child_args);
  529. capture.Stop();
  530. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  531. EXPECT_EQ("--gtest_death_test_style requires an argument.\n", capture.str());
  532. }
  533. static void CheckIncompatible(const std::string arg) {
  534. CapturedStdout capture;
  535. std::vector<const char*> cur_args{"ignore", arg.c_str()};
  536. std::vector<const char*> child_args;
  537. Options options;
  538. bool parsed = options.Process(cur_args, &child_args);
  539. capture.Stop();
  540. ASSERT_FALSE(parsed) << "Process did not fail properly for arg " + arg;
  541. EXPECT_EQ(arg + " is not compatible with isolation runs.\n", capture.str());
  542. }
  543. TEST(OptionsTest, incompatible) {
  544. ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_break_on_failure"));
  545. ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_catch_exceptions"));
  546. ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_random_seed"));
  547. ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_shuffle"));
  548. ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_stream_result_to"));
  549. ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_throw_on_failure"));
  550. }
  551. TEST(OptionsTest, verify_non_env_variables) {
  552. EXPECT_NE(-1, setenv("DEADLINE_THRESHOLD_MS", "VALUE", 1));
  553. EXPECT_NE(-1, setenv("SLOW_THRESHOLD_MS", "VALUE", 1));
  554. EXPECT_NE(-1, setenv("GTEST_FORMAT", "VALUE", 1));
  555. EXPECT_NE(-1, setenv("GTEST_LIST_TESTS", "VALUE", 1));
  556. std::vector<const char*> cur_args{"ignore"};
  557. std::vector<const char*> child_args;
  558. Options options;
  559. EXPECT_TRUE(options.Process(cur_args, &child_args));
  560. EXPECT_LT(0U, options.job_count());
  561. EXPECT_EQ(90000ULL, options.deadline_threshold_ms());
  562. EXPECT_EQ(2000ULL, options.slow_threshold_ms());
  563. EXPECT_EQ("auto", options.color());
  564. EXPECT_EQ("", options.xml_file());
  565. EXPECT_EQ("", options.filter());
  566. EXPECT_EQ(1, options.num_iterations());
  567. EXPECT_TRUE(options.print_time());
  568. EXPECT_TRUE(options.gtest_format());
  569. EXPECT_FALSE(options.allow_disabled_tests());
  570. EXPECT_FALSE(options.list_tests());
  571. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  572. EXPECT_NE(-1, unsetenv("DEADLINE_THRESHOLD_MS"));
  573. EXPECT_NE(-1, unsetenv("SLOW_THRESHOLD_MS"));
  574. EXPECT_NE(-1, unsetenv("GTEST_FORMAT"));
  575. EXPECT_NE(-1, unsetenv("GTEST_LIST_TESTS"));
  576. }
  577. TEST(OptionsTest, gtest_filter_from_env) {
  578. ASSERT_NE(-1, setenv("GTEST_FILTER", "filter_value", 1));
  579. std::vector<const char*> cur_args{"ignore"};
  580. std::vector<const char*> child_args;
  581. Options options;
  582. EXPECT_TRUE(options.Process(cur_args, &child_args));
  583. EXPECT_EQ("filter_value", options.filter());
  584. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  585. ASSERT_NE(-1, unsetenv("GTEST_FILTER"));
  586. }
  587. TEST(OptionsTest, gtest_filter_error_no_value_from_env) {
  588. ASSERT_NE(-1, setenv("GTEST_FILTER", "", 1));
  589. CapturedStdout capture;
  590. std::vector<const char*> cur_args{"ignore"};
  591. std::vector<const char*> child_args;
  592. Options options;
  593. bool parsed = options.Process(cur_args, &child_args);
  594. capture.Stop();
  595. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  596. EXPECT_EQ("env[GTEST_FILTER] requires an argument.\n", capture.str());
  597. ASSERT_NE(-1, unsetenv("GTEST_FILTER"));
  598. }
  599. TEST(OptionsTest, gtest_also_run_disabled_tests_from_env) {
  600. ASSERT_NE(-1, setenv("GTEST_ALSO_RUN_DISABLED_TESTS", "", 1));
  601. std::vector<const char*> cur_args{"ignore"};
  602. std::vector<const char*> child_args;
  603. Options options;
  604. ASSERT_TRUE(options.Process(cur_args, &child_args));
  605. EXPECT_TRUE(options.allow_disabled_tests());
  606. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  607. ASSERT_NE(-1, unsetenv("GTEST_ALSO_RUN_DISABLED_TESTS"));
  608. }
  609. TEST(OptionsTest, gtest_also_run_disabled_tests_error_argument_from_env) {
  610. ASSERT_NE(-1, setenv("GTEST_ALSO_RUN_DISABLED_TESTS", "one", 1));
  611. CapturedStdout capture;
  612. std::vector<const char*> cur_args{"ignore"};
  613. std::vector<const char*> child_args;
  614. Options options;
  615. bool parsed = options.Process(cur_args, &child_args);
  616. capture.Stop();
  617. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  618. EXPECT_EQ("env[GTEST_ALSO_RUN_DISABLED_TESTS] does not take an argument.\n", capture.str());
  619. ASSERT_NE(-1, unsetenv("GTEST_ALSO_RUN_DISABLED_TESTS"));
  620. }
  621. TEST(OptionsTest, gtest_repeat_from_env) {
  622. ASSERT_NE(-1, setenv("GTEST_REPEAT", "34", 1));
  623. std::vector<const char*> cur_args{"ignore"};
  624. std::vector<const char*> child_args;
  625. Options options;
  626. ASSERT_TRUE(options.Process(cur_args, &child_args));
  627. EXPECT_EQ(34, options.num_iterations());
  628. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  629. ASSERT_NE(-1, unsetenv("GTEST_REPEAT"));
  630. }
  631. TEST(OptionsTest, gtest_repeat_error_no_value_from_env) {
  632. ASSERT_NE(-1, setenv("GTEST_REPEAT", "", 1));
  633. CapturedStdout capture;
  634. std::vector<const char*> cur_args{"ignore"};
  635. std::vector<const char*> child_args;
  636. Options options;
  637. bool parsed = options.Process(cur_args, &child_args);
  638. capture.Stop();
  639. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  640. EXPECT_EQ("env[GTEST_REPEAT] requires an argument.\n", capture.str());
  641. ASSERT_NE(-1, unsetenv("GTEST_REPEAT"));
  642. }
  643. TEST(OptionsTest, gtest_repeat_error_overflow_from_env) {
  644. ASSERT_NE(-1, setenv("GTEST_REPEAT", "2147483747", 1));
  645. CapturedStdout capture;
  646. std::vector<const char*> cur_args{"ignore"};
  647. std::vector<const char*> child_args;
  648. Options options;
  649. bool parsed = options.Process(cur_args, &child_args);
  650. capture.Stop();
  651. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  652. EXPECT_EQ("env[GTEST_REPEAT] value overflows (2147483747)\n", capture.str());
  653. ASSERT_NE(-1, setenv("GTEST_REPEAT", "-2147483747", 1));
  654. capture.Reset();
  655. capture.Start();
  656. parsed = options.Process(cur_args, &child_args);
  657. capture.Stop();
  658. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  659. EXPECT_EQ("env[GTEST_REPEAT] value overflows (-2147483747)\n", capture.str());
  660. ASSERT_NE(-1, unsetenv("GTEST_REPEAT"));
  661. }
  662. TEST(OptionsTest, gtest_color_from_env) {
  663. ASSERT_NE(-1, setenv("GTEST_COLOR", "yes", 1));
  664. std::vector<const char*> cur_args{"ignore"};
  665. std::vector<const char*> child_args;
  666. Options options;
  667. ASSERT_TRUE(options.Process(cur_args, &child_args));
  668. EXPECT_EQ("yes", options.color());
  669. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  670. ASSERT_NE(-1, unsetenv("GTEST_COLOR"));
  671. }
  672. TEST(OptionsTest, gtest_color_error_no_value_from_env) {
  673. ASSERT_NE(-1, setenv("GTEST_COLOR", "", 1));
  674. CapturedStdout capture;
  675. std::vector<const char*> cur_args{"ignore"};
  676. std::vector<const char*> child_args;
  677. Options options;
  678. bool parsed = options.Process(cur_args, &child_args);
  679. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  680. EXPECT_EQ("env[GTEST_COLOR] requires an argument.\n", capture.str());
  681. ASSERT_NE(-1, unsetenv("GTEST_COLOR"));
  682. }
  683. TEST(OptionsTest, gtest_print_time_from_env) {
  684. ASSERT_NE(-1, setenv("GTEST_PRINT_TIME", "0", 1));
  685. std::vector<const char*> cur_args{"ignore"};
  686. std::vector<const char*> child_args;
  687. Options options;
  688. ASSERT_TRUE(options.Process(cur_args, &child_args));
  689. EXPECT_FALSE(options.print_time());
  690. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  691. ASSERT_NE(-1, unsetenv("GTEST_PRINT_TIME"));
  692. }
  693. TEST(OptionsTest, gtest_print_time_no_value_from_env) {
  694. ASSERT_NE(-1, setenv("GTEST_PRINT_TIME", "", 1));
  695. std::vector<const char*> cur_args{"ignore"};
  696. std::vector<const char*> child_args;
  697. Options options;
  698. ASSERT_TRUE(options.Process(cur_args, &child_args));
  699. EXPECT_TRUE(options.print_time());
  700. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  701. ASSERT_NE(-1, unsetenv("GTEST_PRINT_TIME"));
  702. }
  703. TEST(OptionsTest, gtest_output_from_env) {
  704. ASSERT_NE(-1, setenv("GTEST_OUTPUT", "xml:/file.xml", 1));
  705. std::vector<const char*> cur_args{"ignore"};
  706. std::vector<const char*> child_args;
  707. Options options;
  708. ASSERT_TRUE(options.Process(cur_args, &child_args));
  709. EXPECT_EQ("/file.xml", options.xml_file());
  710. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  711. ASSERT_NE(-1, unsetenv("GTEST_PRINT_TIME"));
  712. }
  713. TEST(OptionsTest, gtest_output_error_no_value_from_env) {
  714. ASSERT_NE(-1, setenv("GTEST_OUTPUT", "", 1));
  715. CapturedStdout capture;
  716. std::vector<const char*> cur_args{"ignore"};
  717. std::vector<const char*> child_args;
  718. Options options;
  719. bool parsed = options.Process(cur_args, &child_args);
  720. capture.Stop();
  721. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  722. EXPECT_EQ("env[GTEST_OUTPUT] requires an argument.\n", capture.str());
  723. ASSERT_NE(-1, unsetenv("GTEST_OUTPUT"));
  724. }
  725. TEST(OptionsTest, gtest_output_error_no_xml_from_env) {
  726. ASSERT_NE(-1, setenv("GTEST_OUTPUT", "xml:", 1));
  727. CapturedStdout capture;
  728. std::vector<const char*> cur_args{"ignore"};
  729. std::vector<const char*> child_args;
  730. Options options;
  731. bool parsed = options.Process(cur_args, &child_args);
  732. capture.Stop();
  733. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  734. EXPECT_EQ("env[GTEST_OUTPUT] requires a file name after xml:\n", capture.str());
  735. ASSERT_NE(-1, setenv("GTEST_OUTPUT", "not_xml", 1));
  736. capture.Reset();
  737. capture.Start();
  738. parsed = options.Process(cur_args, &child_args);
  739. capture.Stop();
  740. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  741. EXPECT_EQ("env[GTEST_OUTPUT] only supports an xml output file.\n", capture.str());
  742. ASSERT_NE(-1, unsetenv("GTEST_OUTPUT"));
  743. }
  744. TEST(OptionsTest, gtest_death_test_style_from_env) {
  745. ASSERT_NE(-1, setenv("GTEST_DEATH_TEST_STYLE", "fast", 1));
  746. std::vector<const char*> cur_args{"ignore"};
  747. std::vector<const char*> child_args;
  748. Options options;
  749. ASSERT_TRUE(options.Process(cur_args, &child_args));
  750. EXPECT_EQ(std::vector<const char*>{"ignore"}, child_args);
  751. ASSERT_NE(-1, unsetenv("GTEST_DEATH_TEST_STYLE"));
  752. }
  753. TEST(OptionsTest, gtest_death_test_style_error_no_value_from_env) {
  754. ASSERT_NE(-1, setenv("GTEST_DEATH_TEST_STYLE", "", 1));
  755. CapturedStdout capture;
  756. std::vector<const char*> cur_args{"ignore"};
  757. std::vector<const char*> child_args;
  758. Options options;
  759. bool parsed = options.Process(cur_args, &child_args);
  760. capture.Stop();
  761. ASSERT_FALSE(parsed) << "Process did not fail properly.";
  762. EXPECT_EQ("env[GTEST_DEATH_TEST_STYLE] requires an argument.\n", capture.str());
  763. ASSERT_NE(-1, unsetenv("GTEST_DEATH_TEST_STYLE"));
  764. }
  765. static void CheckIncompatibleFromEnv(const std::string env_var) {
  766. ASSERT_NE(-1, setenv(env_var.c_str(), "", 1));
  767. CapturedStdout capture;
  768. Options options;
  769. std::vector<const char*> cur_args{"ignore"};
  770. std::vector<const char*> child_args;
  771. bool parsed = options.Process(cur_args, &child_args);
  772. capture.Stop();
  773. ASSERT_FALSE(parsed) << "Process did not fail properly for env var " + env_var;
  774. EXPECT_EQ("env[" + env_var + "] is not compatible with isolation runs.\n", capture.str());
  775. ASSERT_NE(-1, setenv(env_var.c_str(), "not_empty", 1));
  776. capture.Reset();
  777. capture.Start();
  778. parsed = options.Process(cur_args, &child_args);
  779. capture.Stop();
  780. ASSERT_FALSE(parsed) << "Process did not fail properly for env var " + env_var;
  781. EXPECT_EQ("env[" + env_var + "] is not compatible with isolation runs.\n", capture.str());
  782. ASSERT_NE(-1, unsetenv(env_var.c_str()));
  783. }
  784. TEST(OptionsTest, incompatible_from_env) {
  785. ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_BREAK_ON_FAILURE"));
  786. ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_CATCH_EXCEPTIONS"));
  787. ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_RANDOM_SEED"));
  788. ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_SHUFFLE"));
  789. ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_STREAM_RESULT_TO"));
  790. ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_THROW_ON_FAILURE"));
  791. }
  792. } // namespace gtest_extras
  793. } // namespace android