ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include "src/test/cpp/test_util.h" |
| 15 | |
| 16 | #include "src/main/cpp/startup_options.h" |
| 17 | #include "googletest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace blaze { |
| 20 | |
ccalvarin | 6c8166e | 2018-07-23 09:17:26 -0700 | [diff] [blame] | 21 | // TODO(b/31290599): We should not only check that the options are registered, |
| 22 | // but also that they are parsed. StartupOptions* options would need to be |
| 23 | // non-const to call ProcessArgs and test that the value is recognized by the |
| 24 | // command line. |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 25 | void ExpectValidNullaryOption(const StartupOptions* options, |
| 26 | const std::string& flag_name) { |
| 27 | { |
| 28 | bool result; |
| 29 | std::string error; |
| 30 | EXPECT_TRUE( |
| 31 | options->MaybeCheckValidNullary("--" + flag_name, &result, &error)); |
| 32 | EXPECT_TRUE(result); |
| 33 | } |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 34 | |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 35 | { |
| 36 | bool result; |
| 37 | std::string error; |
| 38 | EXPECT_TRUE( |
| 39 | options->MaybeCheckValidNullary("--no" + flag_name, &result, &error)); |
| 40 | EXPECT_TRUE(result); |
| 41 | } |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 42 | |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 43 | { |
| 44 | bool result; |
| 45 | std::string error; |
| 46 | EXPECT_TRUE(options->MaybeCheckValidNullary("--" + flag_name + "__invalid", |
| 47 | &result, &error)); |
| 48 | EXPECT_FALSE(result); |
| 49 | } |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 50 | |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 51 | { |
| 52 | bool result; |
| 53 | std::string error; |
| 54 | EXPECT_FALSE(options->MaybeCheckValidNullary("--" + flag_name + "=foo", |
| 55 | &result, &error)); |
| 56 | EXPECT_EQ("In argument '--" + flag_name + "=foo': option '--" + flag_name + |
| 57 | "' does not take a value.", |
| 58 | error); |
| 59 | } |
| 60 | |
| 61 | { |
| 62 | bool result; |
| 63 | std::string error; |
| 64 | EXPECT_FALSE(options->MaybeCheckValidNullary("--no" + flag_name + "=foo", |
| 65 | &result, &error)); |
| 66 | EXPECT_EQ("In argument '--no" + flag_name + "=foo': option '--no" + |
| 67 | flag_name + "' does not take a value.", |
| 68 | error); |
| 69 | } |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 70 | |
| 71 | EXPECT_FALSE(options->IsUnary("--" + flag_name)); |
| 72 | EXPECT_FALSE(options->IsUnary("--no" + flag_name)); |
| 73 | } |
| 74 | |
| 75 | void ExpectIsUnaryOption(const StartupOptions* options, |
| 76 | const std::string& flag_name) { |
| 77 | EXPECT_TRUE(options->IsUnary("--" + flag_name)); |
| 78 | EXPECT_TRUE(options->IsUnary("--" + flag_name + "=")); |
| 79 | EXPECT_TRUE(options->IsUnary("--" + flag_name + "=foo")); |
| 80 | |
| 81 | EXPECT_FALSE(options->IsUnary("--" + flag_name + "__invalid")); |
nharmata | be4b2dc | 2020-03-14 00:52:56 -0700 | [diff] [blame] | 82 | |
| 83 | { |
| 84 | bool result; |
| 85 | std::string error; |
| 86 | EXPECT_TRUE( |
| 87 | options->MaybeCheckValidNullary("--" + flag_name, &result, &error)); |
| 88 | EXPECT_FALSE(result); |
| 89 | } |
| 90 | |
| 91 | { |
| 92 | bool result; |
| 93 | std::string error; |
| 94 | EXPECT_TRUE( |
| 95 | options->MaybeCheckValidNullary("--no" + flag_name, &result, &error)); |
| 96 | EXPECT_FALSE(result); |
| 97 | } |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 100 | void ParseStartupOptionsAndExpectWarning( |
| 101 | StartupOptions* startup_options, |
| 102 | const std::vector<std::string>& options_to_parse, |
| 103 | const std::string& expected_warning) { |
| 104 | std::vector<RcStartupFlag> flags; |
Googler | 188a29a | 2018-06-06 05:29:15 -0700 | [diff] [blame] | 105 | flags.reserve(options_to_parse.size()); |
| 106 | for (const std::string& option : options_to_parse) { |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 107 | flags.push_back(RcStartupFlag("", option)); |
| 108 | } |
| 109 | |
| 110 | std::string error; |
| 111 | EXPECT_EQ(blaze_exit_code::SUCCESS, |
| 112 | startup_options->ProcessArgs(flags, &error)); |
| 113 | ASSERT_EQ("", error); |
| 114 | |
| 115 | testing::internal::CaptureStderr(); |
| 116 | startup_options->MaybeLogStartupOptionWarnings(); |
| 117 | const std::string& output = testing::internal::GetCapturedStderr(); |
| 118 | |
| 119 | EXPECT_EQ(expected_warning, output); |
| 120 | } |
| 121 | |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 122 | } // namespace blaze |