blob: 6015d88696f99fd5d0f2c74901cf715334642b4a [file] [log] [blame]
ccalvarin10d91552018-04-13 12:20:43 -07001// 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
19namespace blaze {
20
ccalvarin6c8166e2018-07-23 09:17:26 -070021// 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.
nharmatabe4b2dc2020-03-14 00:52:56 -070025void 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 }
ccalvarin10d91552018-04-13 12:20:43 -070034
nharmatabe4b2dc2020-03-14 00:52:56 -070035 {
36 bool result;
37 std::string error;
38 EXPECT_TRUE(
39 options->MaybeCheckValidNullary("--no" + flag_name, &result, &error));
40 EXPECT_TRUE(result);
41 }
ccalvarin10d91552018-04-13 12:20:43 -070042
nharmatabe4b2dc2020-03-14 00:52:56 -070043 {
44 bool result;
45 std::string error;
46 EXPECT_TRUE(options->MaybeCheckValidNullary("--" + flag_name + "__invalid",
47 &result, &error));
48 EXPECT_FALSE(result);
49 }
ccalvarin10d91552018-04-13 12:20:43 -070050
nharmatabe4b2dc2020-03-14 00:52:56 -070051 {
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 }
ccalvarin10d91552018-04-13 12:20:43 -070070
71 EXPECT_FALSE(options->IsUnary("--" + flag_name));
72 EXPECT_FALSE(options->IsUnary("--no" + flag_name));
73}
74
75void 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"));
nharmatabe4b2dc2020-03-14 00:52:56 -070082
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 }
ccalvarin10d91552018-04-13 12:20:43 -070098}
99
ccalvarin90e116c2018-05-15 16:41:19 -0700100void 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;
Googler188a29a2018-06-06 05:29:15 -0700105 flags.reserve(options_to_parse.size());
106 for (const std::string& option : options_to_parse) {
ccalvarin90e116c2018-05-15 16:41:19 -0700107 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
ccalvarin10d91552018-04-13 12:20:43 -0700122} // namespace blaze