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
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <string>
23 #include <vector>
24
25 #include <android-base/file.h>
26 #include <android-base/test_utils.h>
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29
30 #include "Options.h"
31
32 using ::testing::ElementsAre;
33 using ::testing::StrEq;
34
35 namespace android {
36 namespace gtest_extras {
37
38 class OptionsTest : public ::testing::Test {
39 protected:
ClearChildArgs()40 void ClearChildArgs() {
41 for (auto arg : child_args_) {
42 free(arg);
43 }
44 child_args_.clear();
45 }
46
TearDown()47 void TearDown() { ClearChildArgs(); }
48
49 void CheckIncompatible(const std::string arg);
50 void CheckIncompatibleFromEnv(const std::string env_var);
51
52 std::vector<char*> child_args_;
53 };
54
TEST_F(OptionsTest,unknown_arg)55 TEST_F(OptionsTest, unknown_arg) {
56 CapturedStdout capture;
57 std::vector<const char*> cur_args{"ignore", "--unknown_arg"};
58 Options options;
59 bool parsed = options.Process(cur_args, &child_args_);
60 capture.Stop();
61 ASSERT_FALSE(parsed) << "Process did not fail properly.";
62 EXPECT_EQ("Unknown argument: --unknown_arg\n", capture.str());
63 }
64
TEST_F(OptionsTest,unknown_arg_single_dash)65 TEST_F(OptionsTest, unknown_arg_single_dash) {
66 CapturedStdout capture;
67 std::vector<const char*> cur_args{"ignore", "-unknown_arg"};
68 Options options;
69 bool parsed = options.Process(cur_args, &child_args_);
70 capture.Stop();
71 ASSERT_FALSE(parsed) << "Process did not fail properly.";
72 EXPECT_EQ("Unknown argument: -unknown_arg\n", capture.str());
73 }
74
TEST_F(OptionsTest,extra_arg)75 TEST_F(OptionsTest, extra_arg) {
76 CapturedStdout capture;
77 std::vector<const char*> cur_args{"ignore", "extra"};
78 Options options;
79 bool parsed = options.Process(cur_args, &child_args_);
80 capture.Stop();
81 ASSERT_FALSE(parsed) << "Process did not fail properly.";
82 EXPECT_EQ("Unexpected argument 'extra'\n", capture.str());
83 }
84
TEST_F(OptionsTest,check_defaults)85 TEST_F(OptionsTest, check_defaults) {
86 std::vector<const char*> cur_args{"ignore"};
87 Options options;
88 ASSERT_TRUE(options.Process(cur_args, &child_args_));
89 EXPECT_LT(0U, options.job_count());
90 EXPECT_EQ(90000ULL, options.deadline_threshold_ms());
91 EXPECT_EQ(2000ULL, options.slow_threshold_ms());
92 EXPECT_EQ(0ULL, options.shard_index());
93 EXPECT_EQ(0ULL, options.total_shards());
94 EXPECT_EQ("auto", options.color());
95 EXPECT_EQ("", options.xml_file());
96 EXPECT_EQ("", options.filter());
97 EXPECT_EQ(1, options.num_iterations());
98 EXPECT_FALSE(options.stop_on_error());
99 EXPECT_TRUE(options.print_time());
100 EXPECT_FALSE(options.allow_disabled_tests());
101 EXPECT_FALSE(options.list_tests());
102 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
103 }
104
TEST_F(OptionsTest,gtest_list_tests)105 TEST_F(OptionsTest, gtest_list_tests) {
106 std::vector<const char*> cur_args{"ignore", "--gtest_list_tests"};
107 Options options;
108 ASSERT_TRUE(options.Process(cur_args, &child_args_));
109 EXPECT_TRUE(options.list_tests());
110 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
111 }
112
TEST_F(OptionsTest,gtest_list_tests_error_argument)113 TEST_F(OptionsTest, gtest_list_tests_error_argument) {
114 CapturedStdout capture;
115 std::vector<const char*> cur_args{"ignore", "--gtest_list_tests=nothing"};
116 Options options;
117 bool parsed = options.Process(cur_args, &child_args_);
118 capture.Stop();
119 ASSERT_FALSE(parsed) << "Process did not fail properly.";
120 EXPECT_EQ("--gtest_list_tests does not take an argument.\n", capture.str());
121 }
122
TEST_F(OptionsTest,job_count_single_arg)123 TEST_F(OptionsTest, job_count_single_arg) {
124 std::vector<const char*> cur_args{"ignore", "-j11"};
125 Options options;
126 ASSERT_TRUE(options.Process(cur_args, &child_args_));
127 EXPECT_EQ(11U, options.job_count());
128 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
129 }
130
TEST_F(OptionsTest,job_count_second_arg)131 TEST_F(OptionsTest, job_count_second_arg) {
132 std::vector<const char*> cur_args{"ignore", "-j", "23"};
133 Options options;
134 ASSERT_TRUE(options.Process(cur_args, &child_args_));
135 EXPECT_EQ(23U, options.job_count());
136 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
137 }
138
TEST_F(OptionsTest,job_count_error_single_arg)139 TEST_F(OptionsTest, job_count_error_single_arg) {
140 CapturedStdout capture;
141 std::vector<const char*> cur_args{"ignore", "-j0bad"};
142 Options options;
143 bool parsed = options.Process(cur_args, &child_args_);
144 capture.Stop();
145 ASSERT_FALSE(parsed) << "Process did not fail properly.";
146 EXPECT_EQ("-j value is not formatted as a numeric value (0bad)\n", capture.str());
147 }
148
TEST_F(OptionsTest,job_count_error_second_arg)149 TEST_F(OptionsTest, job_count_error_second_arg) {
150 CapturedStdout capture;
151 std::vector<const char*> cur_args{"ignore", "-j", "34b"};
152 Options options;
153 bool parsed = options.Process(cur_args, &child_args_);
154 capture.Stop();
155 ASSERT_FALSE(parsed) << "Process did not fail properly.";
156 EXPECT_EQ("-j value is not formatted as a numeric value (34b)\n", capture.str());
157 }
158
TEST_F(OptionsTest,job_count_error_no_arg)159 TEST_F(OptionsTest, job_count_error_no_arg) {
160 CapturedStdout capture;
161 std::vector<const char*> cur_args{"ignore", "-j"};
162 Options options;
163 bool parsed = options.Process(cur_args, &child_args_);
164 capture.Stop();
165 ASSERT_FALSE(parsed) << "Process did not fail properly.";
166 EXPECT_EQ("-j requires an argument.\n", capture.str());
167 }
168
TEST_F(OptionsTest,deadline_threshold_ms)169 TEST_F(OptionsTest, deadline_threshold_ms) {
170 std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms=3200"};
171 Options options;
172 ASSERT_TRUE(options.Process(cur_args, &child_args_));
173 EXPECT_EQ(3200ULL, options.deadline_threshold_ms());
174 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
175 }
176
TEST_F(OptionsTest,deadline_threshold_ms_error_no_value)177 TEST_F(OptionsTest, deadline_threshold_ms_error_no_value) {
178 CapturedStdout capture;
179 std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms"};
180 Options options;
181 bool parsed = options.Process(cur_args, &child_args_);
182 capture.Stop();
183 ASSERT_FALSE(parsed) << "Process did not fail properly.";
184 EXPECT_EQ("--deadline_threshold_ms requires an argument.\n", capture.str());
185 }
186
TEST_F(OptionsTest,deadline_threshold_ms_error_not_a_number)187 TEST_F(OptionsTest, deadline_threshold_ms_error_not_a_number) {
188 CapturedStdout capture;
189 std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms=bad"};
190 Options options;
191 bool parsed = options.Process(cur_args, &child_args_);
192 capture.Stop();
193 ASSERT_FALSE(parsed) << "Process did not fail properly.";
194 EXPECT_EQ("--deadline_threshold_ms value is not formatted as a numeric value (bad)\n",
195 capture.str());
196 }
197
TEST_F(OptionsTest,deadline_threshold_ms_error_illegal_value)198 TEST_F(OptionsTest, deadline_threshold_ms_error_illegal_value) {
199 CapturedStdout capture;
200 std::vector<const char*> cur_args{"ignore", "--deadline_threshold_ms=0"};
201 Options options;
202 bool parsed = options.Process(cur_args, &child_args_);
203 capture.Stop();
204 ASSERT_FALSE(parsed) << "Process did not fail properly.";
205 EXPECT_EQ("--deadline_threshold_ms requires a number greater than zero.\n", capture.str());
206 }
207
TEST_F(OptionsTest,slow_threshold_ms)208 TEST_F(OptionsTest, slow_threshold_ms) {
209 std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms=4580"};
210 Options options;
211 ASSERT_TRUE(options.Process(cur_args, &child_args_));
212 EXPECT_EQ(4580ULL, options.slow_threshold_ms());
213 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
214 }
215
TEST_F(OptionsTest,slow_threshold_ms_error_no_value)216 TEST_F(OptionsTest, slow_threshold_ms_error_no_value) {
217 CapturedStdout capture;
218 std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms"};
219 Options options;
220 bool parsed = options.Process(cur_args, &child_args_);
221 capture.Stop();
222 ASSERT_FALSE(parsed) << "Process did not fail properly.";
223 EXPECT_EQ("--slow_threshold_ms requires an argument.\n", capture.str());
224 }
225
TEST_F(OptionsTest,slow_threshold_ms_error_not_a_number)226 TEST_F(OptionsTest, slow_threshold_ms_error_not_a_number) {
227 CapturedStdout capture;
228 Options options;
229 std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms=not"};
230 bool parsed = options.Process(cur_args, &child_args_);
231 capture.Stop();
232 ASSERT_FALSE(parsed) << "Process did not fail properly.";
233 EXPECT_EQ("--slow_threshold_ms value is not formatted as a numeric value (not)\n", capture.str());
234 }
235
TEST_F(OptionsTest,slow_threshold_ms_error_illegal_value)236 TEST_F(OptionsTest, slow_threshold_ms_error_illegal_value) {
237 CapturedStdout capture;
238 std::vector<const char*> cur_args{"ignore", "--slow_threshold_ms=0"};
239 Options options;
240 bool parsed = options.Process(cur_args, &child_args_);
241 capture.Stop();
242 ASSERT_FALSE(parsed) << "Process did not fail properly.";
243 EXPECT_EQ("--slow_threshold_ms requires a number greater than zero.\n", capture.str());
244 }
245
TEST_F(OptionsTest,shard_index)246 TEST_F(OptionsTest, shard_index) {
247 ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "100", 1));
248
249 std::vector<const char*> cur_args{"ignore"};
250 Options options;
251 ASSERT_TRUE(options.Process(cur_args, &child_args_));
252 EXPECT_EQ(100ULL, options.shard_index());
253 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
254
255 ClearChildArgs();
256 ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "0", 1));
257 ASSERT_TRUE(options.Process(cur_args, &child_args_));
258 EXPECT_EQ(0ULL, options.shard_index());
259 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
260
261 ASSERT_NE(-1, unsetenv("GTEST_SHARD_INDEX"));
262 }
263
TEST_F(OptionsTest,shard_index_error_no_value)264 TEST_F(OptionsTest, shard_index_error_no_value) {
265 ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "", 1));
266
267 CapturedStdout capture;
268 std::vector<const char*> cur_args{"ignore"};
269 Options options;
270 bool parsed = options.Process(cur_args, &child_args_);
271 capture.Stop();
272 ASSERT_FALSE(parsed) << "Process did not fail properly.";
273 EXPECT_EQ("env[GTEST_SHARD_INDEX] requires an argument.\n", capture.str());
274
275 ASSERT_NE(-1, unsetenv("GTEST_SHARD_INDEX"));
276 }
277
TEST_F(OptionsTest,shard_index_error_not_a_number)278 TEST_F(OptionsTest, shard_index_error_not_a_number) {
279 ASSERT_NE(-1, setenv("GTEST_SHARD_INDEX", "bad", 1));
280
281 CapturedStdout capture;
282 std::vector<const char*> cur_args{"ignore"};
283 Options options;
284 bool parsed = options.Process(cur_args, &child_args_);
285 capture.Stop();
286 ASSERT_FALSE(parsed) << "Process did not fail properly.";
287 EXPECT_EQ("env[GTEST_SHARD_INDEX] value is not formatted as a numeric value (bad)\n",
288 capture.str());
289
290 ASSERT_NE(-1, unsetenv("GTEST_SHARD_INDEX"));
291 }
292
TEST_F(OptionsTest,shard_index_error_not_from_env)293 TEST_F(OptionsTest, shard_index_error_not_from_env) {
294 CapturedStdout capture;
295 std::vector<const char*> cur_args{"ignore", "--gtest_shard_index=100"};
296 Options options;
297 bool parsed = options.Process(cur_args, &child_args_);
298 capture.Stop();
299 ASSERT_FALSE(parsed) << "Process did not fail properly.";
300 EXPECT_EQ("--gtest_shard_index is only supported as an environment variable.\n", capture.str());
301 }
302
TEST_F(OptionsTest,total_shards)303 TEST_F(OptionsTest, total_shards) {
304 ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "500", 1));
305
306 std::vector<const char*> cur_args{"ignore"};
307 Options options;
308 ASSERT_TRUE(options.Process(cur_args, &child_args_));
309 EXPECT_EQ(500ULL, options.total_shards());
310 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
311
312 ClearChildArgs();
313 ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "0", 1));
314 ASSERT_TRUE(options.Process(cur_args, &child_args_));
315 EXPECT_EQ(0ULL, options.total_shards());
316 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
317
318 ASSERT_NE(-1, unsetenv("GTEST_TOTAL_SHARDS"));
319 }
320
TEST_F(OptionsTest,total_shards_error_no_value)321 TEST_F(OptionsTest, total_shards_error_no_value) {
322 ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "", 1));
323
324 CapturedStdout capture;
325 std::vector<const char*> cur_args{"ignore"};
326 Options options;
327 bool parsed = options.Process(cur_args, &child_args_);
328 capture.Stop();
329 ASSERT_FALSE(parsed) << "Process did not fail properly.";
330 EXPECT_EQ("env[GTEST_TOTAL_SHARDS] requires an argument.\n", capture.str());
331
332 ASSERT_NE(-1, unsetenv("GTEST_TOTAL_SHARDS"));
333 }
334
TEST_F(OptionsTest,total_shards_error_not_a_number)335 TEST_F(OptionsTest, total_shards_error_not_a_number) {
336 ASSERT_NE(-1, setenv("GTEST_TOTAL_SHARDS", "bad", 1));
337
338 CapturedStdout capture;
339 std::vector<const char*> cur_args{"ignore"};
340 Options options;
341 bool parsed = options.Process(cur_args, &child_args_);
342 capture.Stop();
343 ASSERT_FALSE(parsed) << "Process did not fail properly.";
344 EXPECT_EQ("env[GTEST_TOTAL_SHARDS] value is not formatted as a numeric value (bad)\n",
345 capture.str());
346
347 ASSERT_NE(-1, unsetenv("GTEST_TOTAL_SHARDS"));
348 }
349
TEST_F(OptionsTest,total_shards_error_not_from_env)350 TEST_F(OptionsTest, total_shards_error_not_from_env) {
351 CapturedStdout capture;
352 std::vector<const char*> cur_args{"ignore", "--gtest_total_shards=100"};
353 Options options;
354 bool parsed = options.Process(cur_args, &child_args_);
355 capture.Stop();
356 ASSERT_FALSE(parsed) << "Process did not fail properly.";
357 EXPECT_EQ("--gtest_total_shards is only supported as an environment variable.\n", capture.str());
358 }
359
TEST_F(OptionsTest,gtest_color)360 TEST_F(OptionsTest, gtest_color) {
361 std::vector<const char*> cur_args{"ignore", "--gtest_color=yes"};
362 Options options;
363 ASSERT_TRUE(options.Process(cur_args, &child_args_));
364 EXPECT_EQ("yes", options.color());
365 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore"), StrEq("--gtest_color=yes")));
366 }
367
TEST_F(OptionsTest,gtest_color_error_no_value)368 TEST_F(OptionsTest, gtest_color_error_no_value) {
369 CapturedStdout capture;
370 std::vector<const char*> cur_args{"ignore", "--gtest_color="};
371 Options options;
372 bool parsed = options.Process(cur_args, &child_args_);
373 capture.Stop();
374 ASSERT_FALSE(parsed) << "Process did not fail properly.";
375 EXPECT_EQ("--gtest_color requires an argument.\n", capture.str());
376 }
377
TEST_F(OptionsTest,gtest_filter)378 TEST_F(OptionsTest, gtest_filter) {
379 std::vector<const char*> cur_args{"ignore", "--gtest_filter=filter"};
380 Options options;
381 ASSERT_TRUE(options.Process(cur_args, &child_args_));
382 EXPECT_EQ("filter", options.filter());
383 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
384 }
385
TEST_F(OptionsTest,gtest_filter_error_no_value)386 TEST_F(OptionsTest, gtest_filter_error_no_value) {
387 CapturedStdout capture;
388 std::vector<const char*> cur_args{"ignore", "--gtest_filter"};
389 Options options;
390 bool parsed = options.Process(cur_args, &child_args_);
391 capture.Stop();
392 ASSERT_FALSE(parsed) << "Process did not fail properly.";
393 EXPECT_EQ("--gtest_filter requires an argument.\n", capture.str());
394 }
395
TEST_F(OptionsTest,gtest_also_run_disabled_tests)396 TEST_F(OptionsTest, gtest_also_run_disabled_tests) {
397 std::vector<const char*> cur_args{"ignore", "--gtest_also_run_disabled_tests"};
398 Options options;
399 ASSERT_TRUE(options.Process(cur_args, &child_args_));
400 EXPECT_TRUE(options.allow_disabled_tests());
401 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore"), StrEq("--gtest_also_run_disabled_tests")));
402 }
403
TEST_F(OptionsTest,gtest_also_run_disabled_tests_error_argument)404 TEST_F(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 Options options;
408 bool parsed = options.Process(cur_args, &child_args_);
409 capture.Stop();
410 ASSERT_FALSE(parsed) << "Process did not fail properly.";
411 EXPECT_EQ("--gtest_also_run_disabled_tests does not take an argument.\n", capture.str());
412 }
413
TEST_F(OptionsTest,gtest_repeat)414 TEST_F(OptionsTest, gtest_repeat) {
415 std::vector<const char*> cur_args{"ignore", "--gtest_repeat=10"};
416 Options options;
417 ASSERT_TRUE(options.Process(cur_args, &child_args_));
418 EXPECT_EQ(10, options.num_iterations());
419 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
420
421 ClearChildArgs();
422 cur_args = std::vector<const char*>{"ignore", "--gtest_repeat=-1"};
423 ASSERT_TRUE(options.Process(cur_args, &child_args_));
424 EXPECT_EQ(-1, options.num_iterations());
425 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
426 }
427
TEST_F(OptionsTest,gtest_repeat_error_no_value)428 TEST_F(OptionsTest, gtest_repeat_error_no_value) {
429 CapturedStdout capture;
430 std::vector<const char*> cur_args{"ignore", "--gtest_repeat"};
431 Options options;
432 bool parsed = options.Process(cur_args, &child_args_);
433 capture.Stop();
434 ASSERT_FALSE(parsed) << "Process did not fail properly.";
435 EXPECT_EQ("--gtest_repeat requires an argument.\n", capture.str());
436 }
437
TEST_F(OptionsTest,gtest_repeat_error_overflow)438 TEST_F(OptionsTest, gtest_repeat_error_overflow) {
439 CapturedStdout capture;
440 std::vector<const char*> cur_args{"ignore", "--gtest_repeat=2147483747"};
441 Options options;
442 bool parsed = options.Process(cur_args, &child_args_);
443 capture.Stop();
444 ASSERT_FALSE(parsed) << "Process did not fail properly.";
445 EXPECT_EQ("--gtest_repeat value overflows (2147483747)\n", capture.str());
446
447 ClearChildArgs();
448 capture.Reset();
449 capture.Start();
450 cur_args = std::vector<const char*>{"ignore", "--gtest_repeat=-2147483747"};
451 parsed = options.Process(cur_args, &child_args_);
452 capture.Stop();
453 ASSERT_FALSE(parsed) << "Process did not fail properly.";
454 EXPECT_EQ("--gtest_repeat value overflows (-2147483747)\n", capture.str());
455 }
456
TEST_F(OptionsTest,gtest_print_time)457 TEST_F(OptionsTest, gtest_print_time) {
458 std::vector<const char*> cur_args{"ignore", "--gtest_print_time"};
459 Options options;
460 ASSERT_TRUE(options.Process(cur_args, &child_args_));
461 EXPECT_TRUE(options.print_time());
462 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
463
464 ClearChildArgs();
465 cur_args = std::vector<const char*>{"ignore", "--gtest_print_time=0"};
466 ASSERT_TRUE(options.Process(cur_args, &child_args_));
467 EXPECT_FALSE(options.print_time());
468 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
469
470 ClearChildArgs();
471 cur_args = std::vector<const char*>{"ignore", "--gtest_print_time=1"};
472 ASSERT_TRUE(options.Process(cur_args, &child_args_));
473 EXPECT_TRUE(options.print_time());
474 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
475 }
476
TEST_F(OptionsTest,gtest_output)477 TEST_F(OptionsTest, gtest_output) {
478 std::vector<const char*> cur_args{"ignore", "--gtest_output=xml:/file.xml"};
479 Options options;
480 ASSERT_TRUE(options.Process(cur_args, &child_args_));
481 EXPECT_EQ("/file.xml", options.xml_file());
482 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
483
484 ClearChildArgs();
485 cur_args = std::vector<const char*>{"ignore", "--gtest_output=xml:/directory/"};
486 ASSERT_TRUE(options.Process(cur_args, &child_args_));
487 EXPECT_EQ("/directory/test_details.xml", options.xml_file());
488 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
489
490 ClearChildArgs();
491 cur_args = std::vector<const char*>{"ignore", "--gtest_output=xml:cwd.xml"};
492 ASSERT_TRUE(options.Process(cur_args, &child_args_));
493 char* cwd = getcwd(nullptr, 0);
494 std::string expected_file(cwd);
495 expected_file += "/cwd.xml";
496 free(cwd);
497 EXPECT_EQ(expected_file, options.xml_file());
498 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
499 }
500
TEST_F(OptionsTest,gtest_output_error_no_value)501 TEST_F(OptionsTest, gtest_output_error_no_value) {
502 CapturedStdout capture;
503 std::vector<const char*> cur_args{"ignore", "--gtest_output"};
504 Options options;
505 bool parsed = options.Process(cur_args, &child_args_);
506 capture.Stop();
507 ASSERT_FALSE(parsed) << "Process did not fail properly.";
508 EXPECT_EQ("--gtest_output requires an argument.\n", capture.str());
509 }
510
TEST_F(OptionsTest,gtest_output_error_no_xml)511 TEST_F(OptionsTest, gtest_output_error_no_xml) {
512 CapturedStdout capture;
513 std::vector<const char*> cur_args{"ignore", "--gtest_output=xml:"};
514 Options options;
515 bool parsed = options.Process(cur_args, &child_args_);
516 capture.Stop();
517 ASSERT_FALSE(parsed) << "Process did not fail properly.";
518 EXPECT_EQ("--gtest_output requires a file name after xml:\n", capture.str());
519
520 ClearChildArgs();
521 capture.Reset();
522 capture.Start();
523 cur_args = std::vector<const char*>{"ignore", "--gtest_output=not_xml"};
524 parsed = options.Process(cur_args, &child_args_);
525 capture.Stop();
526 ASSERT_FALSE(parsed) << "Process did not fail properly.";
527 EXPECT_EQ("--gtest_output only supports an xml output file.\n", capture.str());
528 }
529
TEST_F(OptionsTest,gtest_death_test_style)530 TEST_F(OptionsTest, gtest_death_test_style) {
531 std::vector<const char*> cur_args{"ignore", "--gtest_death_test_style=something"};
532 Options options;
533 ASSERT_TRUE(options.Process(cur_args, &child_args_));
534 EXPECT_THAT(child_args_,
535 ElementsAre(StrEq("ignore"), StrEq("--gtest_death_test_style=something")));
536 }
537
TEST_F(OptionsTest,gtest_death_test_style_error_no_value)538 TEST_F(OptionsTest, gtest_death_test_style_error_no_value) {
539 CapturedStdout capture;
540 std::vector<const char*> cur_args{"ignore", "--gtest_death_test_style"};
541 Options options;
542 bool parsed = options.Process(cur_args, &child_args_);
543 capture.Stop();
544 ASSERT_FALSE(parsed) << "Process did not fail properly.";
545 EXPECT_EQ("--gtest_death_test_style requires an argument.\n", capture.str());
546 }
547
TEST_F(OptionsTest,gtest_flagfile)548 TEST_F(OptionsTest, gtest_flagfile) {
549 TemporaryFile tf;
550 ASSERT_TRUE(
551 android::base::WriteStringToFile("--gtest_color=no\n"
552 "\n"
553 "--gtest_print_time=0\n"
554 "--gtest_repeat=10\n",
555 tf.path));
556
557 std::string flag("--gtest_flagfile=");
558 flag += tf.path;
559 std::vector<const char*> cur_args{"ignore", flag.c_str()};
560 Options options;
561 ASSERT_TRUE(options.Process(cur_args, &child_args_));
562 EXPECT_EQ("no", options.color());
563 EXPECT_FALSE(options.print_time());
564 EXPECT_EQ(10, options.num_iterations());
565 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore"), StrEq("--gtest_color=no")));
566 }
567
TEST_F(OptionsTest,gtest_flagfile_no_newline)568 TEST_F(OptionsTest, gtest_flagfile_no_newline) {
569 TemporaryFile tf;
570 ASSERT_TRUE(android::base::WriteStringToFile("--gtest_color=no", tf.path));
571
572 std::string flag("--gtest_flagfile=");
573 flag += tf.path;
574 std::vector<const char*> cur_args{"ignore", flag.c_str()};
575 Options options;
576 ASSERT_TRUE(options.Process(cur_args, &child_args_));
577 EXPECT_EQ("no", options.color());
578 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore"), StrEq("--gtest_color=no")));
579 }
580
TEST_F(OptionsTest,gtest_flagfile_empty_file)581 TEST_F(OptionsTest, gtest_flagfile_empty_file) {
582 TemporaryFile tf;
583
584 std::string flag("--gtest_flagfile=");
585 flag += tf.path;
586 std::vector<const char*> cur_args{"ignore", flag.c_str()};
587 Options options;
588 ASSERT_TRUE(options.Process(cur_args, &child_args_));
589 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
590 }
591
TEST_F(OptionsTest,gtest_flagfile_disallow_j_option)592 TEST_F(OptionsTest, gtest_flagfile_disallow_j_option) {
593 TemporaryFile tf;
594 ASSERT_TRUE(android::base::WriteStringToFile("-j1\n", tf.path));
595
596 CapturedStdout capture;
597 std::string flag("--gtest_flagfile=");
598 flag += tf.path;
599 std::vector<const char*> cur_args{"ignore", flag.c_str()};
600 Options options;
601 bool parsed = options.Process(cur_args, &child_args_);
602 capture.Stop();
603 ASSERT_FALSE(parsed) << "Process did not fail properly.";
604 EXPECT_EQ("Unknown argument: -j1\n", capture.str());
605 }
606
TEST_F(OptionsTest,gtest_flagfile_disallow_gtest_flagfile_option_in_file)607 TEST_F(OptionsTest, gtest_flagfile_disallow_gtest_flagfile_option_in_file) {
608 TemporaryFile tf;
609 ASSERT_TRUE(android::base::WriteStringToFile("--gtest_flagfile=nothing\n", tf.path));
610
611 CapturedStdout capture;
612 std::string flag("--gtest_flagfile=");
613 flag += tf.path;
614 std::vector<const char*> cur_args{"ignore", flag.c_str()};
615 Options options;
616 bool parsed = options.Process(cur_args, &child_args_);
617 capture.Stop();
618 ASSERT_FALSE(parsed) << "Process did not fail properly.";
619 EXPECT_EQ("Argument: --gtest_flagfile=nothing is not allowed in flag file.\n", capture.str());
620 }
621
TEST_F(OptionsTest,gtest_flagfile_does_not_exist)622 TEST_F(OptionsTest, gtest_flagfile_does_not_exist) {
623 CapturedStdout capture;
624 std::vector<const char*> cur_args{"ignore", "--gtest_flagfile=/this/does/not/exist"};
625 Options options;
626 bool parsed = options.Process(cur_args, &child_args_);
627 capture.Stop();
628 ASSERT_FALSE(parsed) << "Process did not fail properly.";
629 EXPECT_EQ("Unable to read data from file /this/does/not/exist\n", capture.str());
630 }
631
TEST_F(OptionsTest,stop_on_error)632 TEST_F(OptionsTest, stop_on_error) {
633 std::vector<const char*> cur_args{"ignore", "--gtest_break_on_failure"};
634 Options options;
635 ASSERT_TRUE(options.Process(cur_args, &child_args_));
636 EXPECT_TRUE(options.stop_on_error());
637 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
638
639 ClearChildArgs();
640 cur_args = std::vector<const char*>{"ignore", "--gtest_throw_on_failure"};
641 ASSERT_TRUE(options.Process(cur_args, &child_args_));
642 EXPECT_TRUE(options.stop_on_error());
643 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
644
645 ClearChildArgs();
646 cur_args =
647 std::vector<const char*>{"ignore", "--gtest_break_on_failure", "--gtest_throw_on_failure"};
648 ASSERT_TRUE(options.Process(cur_args, &child_args_));
649 EXPECT_TRUE(options.stop_on_error());
650 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
651 }
652
CheckIncompatible(const std::string arg)653 void OptionsTest::CheckIncompatible(const std::string arg) {
654 CapturedStdout capture;
655 std::vector<const char*> cur_args{"ignore", arg.c_str()};
656 Options options;
657 bool parsed = options.Process(cur_args, &child_args_);
658 capture.Stop();
659 ASSERT_FALSE(parsed) << "Process did not fail properly for arg " + arg;
660 EXPECT_EQ(arg + " is not compatible with isolation runs.\n", capture.str());
661 }
662
TEST_F(OptionsTest,incompatible)663 TEST_F(OptionsTest, incompatible) {
664 ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_catch_exceptions"));
665 ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_random_seed"));
666 ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_shuffle"));
667 ASSERT_NO_FATAL_FAILURE(CheckIncompatible("--gtest_stream_result_to"));
668 }
669
TEST_F(OptionsTest,verify_non_env_variables)670 TEST_F(OptionsTest, verify_non_env_variables) {
671 EXPECT_NE(-1, setenv("DEADLINE_THRESHOLD_MS", "VALUE", 1));
672 EXPECT_NE(-1, setenv("SLOW_THRESHOLD_MS", "VALUE", 1));
673 EXPECT_NE(-1, setenv("GTEST_FORMAT", "VALUE", 1));
674 EXPECT_NE(-1, setenv("GTEST_LIST_TESTS", "VALUE", 1));
675
676 std::vector<const char*> cur_args{"ignore"};
677 Options options;
678 EXPECT_TRUE(options.Process(cur_args, &child_args_));
679 EXPECT_LT(0U, options.job_count());
680 EXPECT_EQ(90000ULL, options.deadline_threshold_ms());
681 EXPECT_EQ(2000ULL, options.slow_threshold_ms());
682 EXPECT_EQ("auto", options.color());
683 EXPECT_EQ("", options.xml_file());
684 EXPECT_EQ("", options.filter());
685 EXPECT_EQ(1, options.num_iterations());
686 EXPECT_FALSE(options.stop_on_error());
687 EXPECT_TRUE(options.print_time());
688 EXPECT_FALSE(options.allow_disabled_tests());
689 EXPECT_FALSE(options.list_tests());
690 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
691
692 EXPECT_NE(-1, unsetenv("DEADLINE_THRESHOLD_MS"));
693 EXPECT_NE(-1, unsetenv("SLOW_THRESHOLD_MS"));
694 EXPECT_NE(-1, unsetenv("GTEST_FORMAT"));
695 EXPECT_NE(-1, unsetenv("GTEST_LIST_TESTS"));
696 }
697
TEST_F(OptionsTest,gtest_filter_from_env)698 TEST_F(OptionsTest, gtest_filter_from_env) {
699 ASSERT_NE(-1, setenv("GTEST_FILTER", "filter_value", 1));
700
701 std::vector<const char*> cur_args{"ignore"};
702 Options options;
703 EXPECT_TRUE(options.Process(cur_args, &child_args_));
704 EXPECT_EQ("filter_value", options.filter());
705 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
706
707 ASSERT_NE(-1, unsetenv("GTEST_FILTER"));
708 }
709
TEST_F(OptionsTest,gtest_filter_error_no_value_from_env)710 TEST_F(OptionsTest, gtest_filter_error_no_value_from_env) {
711 ASSERT_NE(-1, setenv("GTEST_FILTER", "", 1));
712
713 CapturedStdout capture;
714 std::vector<const char*> cur_args{"ignore"};
715 Options options;
716 bool parsed = options.Process(cur_args, &child_args_);
717 capture.Stop();
718 ASSERT_FALSE(parsed) << "Process did not fail properly.";
719 EXPECT_EQ("env[GTEST_FILTER] requires an argument.\n", capture.str());
720
721 ASSERT_NE(-1, unsetenv("GTEST_FILTER"));
722 }
723
TEST_F(OptionsTest,gtest_also_run_disabled_tests_from_env)724 TEST_F(OptionsTest, gtest_also_run_disabled_tests_from_env) {
725 ASSERT_NE(-1, setenv("GTEST_ALSO_RUN_DISABLED_TESTS", "", 1));
726
727 std::vector<const char*> cur_args{"ignore"};
728 Options options;
729 ASSERT_TRUE(options.Process(cur_args, &child_args_));
730 EXPECT_TRUE(options.allow_disabled_tests());
731 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
732
733 ASSERT_NE(-1, unsetenv("GTEST_ALSO_RUN_DISABLED_TESTS"));
734 }
735
TEST_F(OptionsTest,gtest_also_run_disabled_tests_error_argument_from_env)736 TEST_F(OptionsTest, gtest_also_run_disabled_tests_error_argument_from_env) {
737 ASSERT_NE(-1, setenv("GTEST_ALSO_RUN_DISABLED_TESTS", "one", 1));
738
739 CapturedStdout capture;
740 std::vector<const char*> cur_args{"ignore"};
741 Options options;
742 bool parsed = options.Process(cur_args, &child_args_);
743 capture.Stop();
744 ASSERT_FALSE(parsed) << "Process did not fail properly.";
745 EXPECT_EQ("env[GTEST_ALSO_RUN_DISABLED_TESTS] does not take an argument.\n", capture.str());
746
747 ASSERT_NE(-1, unsetenv("GTEST_ALSO_RUN_DISABLED_TESTS"));
748 }
749
TEST_F(OptionsTest,gtest_repeat_from_env)750 TEST_F(OptionsTest, gtest_repeat_from_env) {
751 ASSERT_NE(-1, setenv("GTEST_REPEAT", "34", 1));
752
753 std::vector<const char*> cur_args{"ignore"};
754 Options options;
755 ASSERT_TRUE(options.Process(cur_args, &child_args_));
756 EXPECT_EQ(34, options.num_iterations());
757 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
758
759 ASSERT_NE(-1, unsetenv("GTEST_REPEAT"));
760 }
761
TEST_F(OptionsTest,gtest_repeat_error_no_value_from_env)762 TEST_F(OptionsTest, gtest_repeat_error_no_value_from_env) {
763 ASSERT_NE(-1, setenv("GTEST_REPEAT", "", 1));
764
765 CapturedStdout capture;
766 std::vector<const char*> cur_args{"ignore"};
767 Options options;
768 bool parsed = options.Process(cur_args, &child_args_);
769 capture.Stop();
770 ASSERT_FALSE(parsed) << "Process did not fail properly.";
771 EXPECT_EQ("env[GTEST_REPEAT] requires an argument.\n", capture.str());
772
773 ASSERT_NE(-1, unsetenv("GTEST_REPEAT"));
774 }
775
TEST_F(OptionsTest,gtest_repeat_error_overflow_from_env)776 TEST_F(OptionsTest, gtest_repeat_error_overflow_from_env) {
777 ASSERT_NE(-1, setenv("GTEST_REPEAT", "2147483747", 1));
778
779 CapturedStdout capture;
780 std::vector<const char*> cur_args{"ignore"};
781 Options options;
782 bool parsed = options.Process(cur_args, &child_args_);
783 capture.Stop();
784 ASSERT_FALSE(parsed) << "Process did not fail properly.";
785 EXPECT_EQ("env[GTEST_REPEAT] value overflows (2147483747)\n", capture.str());
786
787 ASSERT_NE(-1, setenv("GTEST_REPEAT", "-2147483747", 1));
788
789 ClearChildArgs();
790 capture.Reset();
791 capture.Start();
792 parsed = options.Process(cur_args, &child_args_);
793 capture.Stop();
794 ASSERT_FALSE(parsed) << "Process did not fail properly.";
795 EXPECT_EQ("env[GTEST_REPEAT] value overflows (-2147483747)\n", capture.str());
796
797 ASSERT_NE(-1, unsetenv("GTEST_REPEAT"));
798 }
799
TEST_F(OptionsTest,gtest_color_from_env)800 TEST_F(OptionsTest, gtest_color_from_env) {
801 ASSERT_NE(-1, setenv("GTEST_COLOR", "yes", 1));
802
803 std::vector<const char*> cur_args{"ignore"};
804 std::vector<const char*> child_args;
805 Options options;
806 ASSERT_TRUE(options.Process(cur_args, &child_args_));
807 EXPECT_EQ("yes", options.color());
808 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
809
810 ASSERT_NE(-1, unsetenv("GTEST_COLOR"));
811 }
812
TEST_F(OptionsTest,gtest_color_error_no_value_from_env)813 TEST_F(OptionsTest, gtest_color_error_no_value_from_env) {
814 ASSERT_NE(-1, setenv("GTEST_COLOR", "", 1));
815
816 CapturedStdout capture;
817 std::vector<const char*> cur_args{"ignore"};
818 Options options;
819 bool parsed = options.Process(cur_args, &child_args_);
820 ASSERT_FALSE(parsed) << "Process did not fail properly.";
821 EXPECT_EQ("env[GTEST_COLOR] requires an argument.\n", capture.str());
822
823 ASSERT_NE(-1, unsetenv("GTEST_COLOR"));
824 }
825
TEST_F(OptionsTest,gtest_print_time_from_env)826 TEST_F(OptionsTest, gtest_print_time_from_env) {
827 ASSERT_NE(-1, setenv("GTEST_PRINT_TIME", "0", 1));
828
829 std::vector<const char*> cur_args{"ignore"};
830 Options options;
831 ASSERT_TRUE(options.Process(cur_args, &child_args_));
832 EXPECT_FALSE(options.print_time());
833 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
834
835 ASSERT_NE(-1, unsetenv("GTEST_PRINT_TIME"));
836 }
837
TEST_F(OptionsTest,gtest_print_time_no_value_from_env)838 TEST_F(OptionsTest, gtest_print_time_no_value_from_env) {
839 ASSERT_NE(-1, setenv("GTEST_PRINT_TIME", "", 1));
840
841 std::vector<const char*> cur_args{"ignore"};
842 Options options;
843 ASSERT_TRUE(options.Process(cur_args, &child_args_));
844 EXPECT_TRUE(options.print_time());
845 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
846
847 ASSERT_NE(-1, unsetenv("GTEST_PRINT_TIME"));
848 }
849
TEST_F(OptionsTest,gtest_output_from_env)850 TEST_F(OptionsTest, gtest_output_from_env) {
851 ASSERT_NE(-1, setenv("GTEST_OUTPUT", "xml:/file.xml", 1));
852
853 std::vector<const char*> cur_args{"ignore"};
854 Options options;
855 ASSERT_TRUE(options.Process(cur_args, &child_args_));
856 EXPECT_EQ("/file.xml", options.xml_file());
857 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
858
859 ASSERT_NE(-1, unsetenv("GTEST_PRINT_TIME"));
860 }
861
TEST_F(OptionsTest,gtest_output_error_no_value_from_env)862 TEST_F(OptionsTest, gtest_output_error_no_value_from_env) {
863 ASSERT_NE(-1, setenv("GTEST_OUTPUT", "", 1));
864
865 CapturedStdout capture;
866 std::vector<const char*> cur_args{"ignore"};
867 Options options;
868 bool parsed = options.Process(cur_args, &child_args_);
869 capture.Stop();
870 ASSERT_FALSE(parsed) << "Process did not fail properly.";
871 EXPECT_EQ("env[GTEST_OUTPUT] requires an argument.\n", capture.str());
872
873 ASSERT_NE(-1, unsetenv("GTEST_OUTPUT"));
874 }
875
TEST_F(OptionsTest,gtest_output_error_no_xml_from_env)876 TEST_F(OptionsTest, gtest_output_error_no_xml_from_env) {
877 ASSERT_NE(-1, setenv("GTEST_OUTPUT", "xml:", 1));
878
879 CapturedStdout capture;
880 std::vector<const char*> cur_args{"ignore"};
881 Options options;
882 bool parsed = options.Process(cur_args, &child_args_);
883 capture.Stop();
884 ASSERT_FALSE(parsed) << "Process did not fail properly.";
885 EXPECT_EQ("env[GTEST_OUTPUT] requires a file name after xml:\n", capture.str());
886
887 ASSERT_NE(-1, setenv("GTEST_OUTPUT", "not_xml", 1));
888
889 ClearChildArgs();
890 capture.Reset();
891 capture.Start();
892 parsed = options.Process(cur_args, &child_args_);
893 capture.Stop();
894 ASSERT_FALSE(parsed) << "Process did not fail properly.";
895 EXPECT_EQ("env[GTEST_OUTPUT] only supports an xml output file.\n", capture.str());
896
897 ASSERT_NE(-1, unsetenv("GTEST_OUTPUT"));
898 }
899
TEST_F(OptionsTest,gtest_death_test_style_from_env)900 TEST_F(OptionsTest, gtest_death_test_style_from_env) {
901 ASSERT_NE(-1, setenv("GTEST_DEATH_TEST_STYLE", "fast", 1));
902
903 std::vector<const char*> cur_args{"ignore"};
904 Options options;
905 ASSERT_TRUE(options.Process(cur_args, &child_args_));
906 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
907
908 ASSERT_NE(-1, unsetenv("GTEST_DEATH_TEST_STYLE"));
909 }
910
TEST_F(OptionsTest,gtest_death_test_style_error_no_value_from_env)911 TEST_F(OptionsTest, gtest_death_test_style_error_no_value_from_env) {
912 ASSERT_NE(-1, setenv("GTEST_DEATH_TEST_STYLE", "", 1));
913
914 CapturedStdout capture;
915 std::vector<const char*> cur_args{"ignore"};
916 Options options;
917 bool parsed = options.Process(cur_args, &child_args_);
918 capture.Stop();
919 ASSERT_FALSE(parsed) << "Process did not fail properly.";
920 EXPECT_EQ("env[GTEST_DEATH_TEST_STYLE] requires an argument.\n", capture.str());
921
922 ASSERT_NE(-1, unsetenv("GTEST_DEATH_TEST_STYLE"));
923 }
924
TEST_F(OptionsTest,stop_on_error_from_env)925 TEST_F(OptionsTest, stop_on_error_from_env) {
926 ASSERT_NE(-1, setenv("GTEST_BREAK_ON_FAILURE", "", 1));
927
928 std::vector<const char*> cur_args{"ignore"};
929 Options options;
930 ASSERT_TRUE(options.Process(cur_args, &child_args_));
931 EXPECT_TRUE(options.stop_on_error());
932 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
933
934 ASSERT_NE(-1, unsetenv("GTEST_BREAK_ON_FAILURE"));
935
936 ASSERT_NE(-1, setenv("GTEST_THROW_ON_FAILURE", "", 1));
937
938 ClearChildArgs();
939 ASSERT_TRUE(options.Process(cur_args, &child_args_));
940 EXPECT_TRUE(options.stop_on_error());
941 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
942
943 ASSERT_NE(-1, unsetenv("GTEST_THROW_ON_FAILURE"));
944
945 ASSERT_NE(-1, setenv("GTEST_BREAK_ON_FAILURE", "", 1));
946 ASSERT_NE(-1, setenv("GTEST_THROW_ON_FAILURE", "", 1));
947
948 ClearChildArgs();
949 ASSERT_TRUE(options.Process(cur_args, &child_args_));
950 EXPECT_TRUE(options.stop_on_error());
951 EXPECT_THAT(child_args_, ElementsAre(StrEq("ignore")));
952
953 ASSERT_NE(-1, unsetenv("GTEST_BREAK_ON_FAILURE"));
954 ASSERT_NE(-1, unsetenv("GTEST_THROW_ON_FAILURE"));
955 }
956
CheckIncompatibleFromEnv(const std::string env_var)957 void OptionsTest::CheckIncompatibleFromEnv(const std::string env_var) {
958 ASSERT_NE(-1, setenv(env_var.c_str(), "", 1));
959
960 CapturedStdout capture;
961 Options options;
962 std::vector<const char*> cur_args{"ignore"};
963 bool parsed = options.Process(cur_args, &child_args_);
964 capture.Stop();
965 ASSERT_FALSE(parsed) << "Process did not fail properly for env var " + env_var;
966 EXPECT_EQ("env[" + env_var + "] is not compatible with isolation runs.\n", capture.str());
967
968 ASSERT_NE(-1, setenv(env_var.c_str(), "not_empty", 1));
969
970 ClearChildArgs();
971 capture.Reset();
972 capture.Start();
973 parsed = options.Process(cur_args, &child_args_);
974 capture.Stop();
975 ASSERT_FALSE(parsed) << "Process did not fail properly for env var " + env_var;
976 EXPECT_EQ("env[" + env_var + "] is not compatible with isolation runs.\n", capture.str());
977
978 ASSERT_NE(-1, unsetenv(env_var.c_str()));
979 }
980
TEST_F(OptionsTest,incompatible_from_env)981 TEST_F(OptionsTest, incompatible_from_env) {
982 ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_CATCH_EXCEPTIONS"));
983 ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_RANDOM_SEED"));
984 ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_SHUFFLE"));
985 ASSERT_NO_FATAL_FAILURE(CheckIncompatibleFromEnv("GTEST_STREAM_RESULT_TO"));
986 }
987
988 } // namespace gtest_extras
989 } // namespace android
990