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. |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 25 | void ExpectIsNullaryOption(const StartupOptions* options, |
| 26 | const std::string& flag_name) { |
| 27 | EXPECT_TRUE(options->IsNullary("--" + flag_name)); |
| 28 | EXPECT_TRUE(options->IsNullary("--no" + flag_name)); |
| 29 | |
| 30 | EXPECT_FALSE(options->IsNullary("--" + flag_name + "__invalid")); |
| 31 | |
| 32 | EXPECT_DEATH(options->IsNullary("--" + flag_name + "=foo"), |
| 33 | ("In argument '--" + flag_name + "=foo': option '--" + |
| 34 | flag_name + "' does not take a value") |
| 35 | .c_str()); |
| 36 | |
| 37 | EXPECT_DEATH(options->IsNullary("--no" + flag_name + "=foo"), |
| 38 | ("In argument '--no" + flag_name + "=foo': option '--no" + |
| 39 | flag_name + "' does not take a value") |
| 40 | .c_str()); |
| 41 | |
| 42 | EXPECT_FALSE(options->IsUnary("--" + flag_name)); |
| 43 | EXPECT_FALSE(options->IsUnary("--no" + flag_name)); |
| 44 | } |
| 45 | |
| 46 | void ExpectIsUnaryOption(const StartupOptions* options, |
| 47 | const std::string& flag_name) { |
| 48 | EXPECT_TRUE(options->IsUnary("--" + flag_name)); |
| 49 | EXPECT_TRUE(options->IsUnary("--" + flag_name + "=")); |
| 50 | EXPECT_TRUE(options->IsUnary("--" + flag_name + "=foo")); |
| 51 | |
| 52 | EXPECT_FALSE(options->IsUnary("--" + flag_name + "__invalid")); |
| 53 | EXPECT_FALSE(options->IsNullary("--" + flag_name)); |
| 54 | EXPECT_FALSE(options->IsNullary("--no" + flag_name)); |
| 55 | } |
| 56 | |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 57 | void ParseStartupOptionsAndExpectWarning( |
| 58 | StartupOptions* startup_options, |
| 59 | const std::vector<std::string>& options_to_parse, |
| 60 | const std::string& expected_warning) { |
| 61 | std::vector<RcStartupFlag> flags; |
Googler | 188a29a | 2018-06-06 05:29:15 -0700 | [diff] [blame] | 62 | flags.reserve(options_to_parse.size()); |
| 63 | for (const std::string& option : options_to_parse) { |
ccalvarin | 90e116c | 2018-05-15 16:41:19 -0700 | [diff] [blame] | 64 | flags.push_back(RcStartupFlag("", option)); |
| 65 | } |
| 66 | |
| 67 | std::string error; |
| 68 | EXPECT_EQ(blaze_exit_code::SUCCESS, |
| 69 | startup_options->ProcessArgs(flags, &error)); |
| 70 | ASSERT_EQ("", error); |
| 71 | |
| 72 | testing::internal::CaptureStderr(); |
| 73 | startup_options->MaybeLogStartupOptionWarnings(); |
| 74 | const std::string& output = testing::internal::GetCapturedStderr(); |
| 75 | |
| 76 | EXPECT_EQ(expected_warning, output); |
| 77 | } |
| 78 | |
ccalvarin | 10d9155 | 2018-04-13 12:20:43 -0700 | [diff] [blame] | 79 | } // namespace blaze |