aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/cpp/bazel_startup_options_test.cc
blob: 3e372e4a88038a8aa6cd1e92248e8bcfc18a1e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "src/main/cpp/bazel_startup_options.h"

#include <stdlib.h>

#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/workspace_layout.h"
#include "src/test/cpp/test_util.h"
#include "googletest/include/gtest/gtest.h"

namespace blaze {

class BazelStartupOptionsTest : public ::testing::Test {
 protected:
  BazelStartupOptionsTest() : workspace_layout_(new WorkspaceLayout()) {}
  ~BazelStartupOptionsTest() = default;

  void SetUp() override {
    // This knowingly ignores the possibility of these environment variables
    // being unset because we expect our test runner to set them in all cases.
    // Otherwise, we'll crash here, but this keeps our code simpler.
    old_test_tmpdir_ = GetEnv("TEST_TMPDIR");

    ReinitStartupOptions();
  }

  void TearDown() override { SetEnv("TEST_TMPDIR", old_test_tmpdir_); }

  // Recreates startup_options_ after changes to the environment.
  void ReinitStartupOptions() {
    startup_options_.reset(new BazelStartupOptions(workspace_layout_.get()));
  }

 private:
  std::unique_ptr<WorkspaceLayout> workspace_layout_;

 protected:
  std::unique_ptr<BazelStartupOptions> startup_options_;

 private:
  std::string old_test_tmpdir_;
};

TEST_F(BazelStartupOptionsTest, ProductName) {
  ASSERT_EQ("Bazel", startup_options_->product_name);
}

TEST_F(BazelStartupOptionsTest, JavaLoggingOptions) {
  ASSERT_EQ("com.google.devtools.build.lib.util.SingleLineFormatter",
            startup_options_->java_logging_formatter);
}

TEST_F(BazelStartupOptionsTest, EmptyFlagsAreInvalid) {
  EXPECT_FALSE(startup_options_->IsNullary(""));
  EXPECT_FALSE(startup_options_->IsNullary("--"));
  EXPECT_FALSE(startup_options_->IsUnary(""));
  EXPECT_FALSE(startup_options_->IsUnary("--"));
}

// TODO(#4502 related cleanup) This test serves as a catalog of the valid
// options - make this test check that the list is complete, that no options are
// missing.
TEST_F(BazelStartupOptionsTest, ValidStartupFlags) {
  // IMPORTANT: Before modifying this test, please contact a Bazel core team
  // member that knows the Google-internal procedure for adding/deprecating
  // startup flags.
  const StartupOptions* options = startup_options_.get();
  ExpectIsNullaryOption(options, "batch");
  ExpectIsNullaryOption(options, "batch_cpu_scheduling");
  ExpectIsNullaryOption(options, "block_for_lock");
  ExpectIsNullaryOption(options, "client_debug");
  ExpectIsNullaryOption(options, "deep_execroot");
  ExpectIsNullaryOption(options, "experimental_oom_more_eagerly");
  ExpectIsNullaryOption(options, "fatal_event_bus_exceptions");
  ExpectIsNullaryOption(options, "home_rc");
  ExpectIsNullaryOption(options, "host_jvm_debug");
  ExpectIsNullaryOption(options, "ignore_all_rc_files");
  ExpectIsNullaryOption(options, "master_bazelrc");
  ExpectIsNullaryOption(options, "system_rc");
  ExpectIsNullaryOption(options, "watchfs");
  ExpectIsNullaryOption(options, "workspace_rc");
  ExpectIsNullaryOption(options, "write_command_log");
  ExpectIsUnaryOption(options, "bazelrc");
  ExpectIsUnaryOption(options, "command_port");
  ExpectIsUnaryOption(options, "connect_timeout_secs");
  ExpectIsUnaryOption(options, "digest_function");
  ExpectIsUnaryOption(options, "experimental_oom_more_eagerly_threshold");
  ExpectIsUnaryOption(options, "host_javabase");
  ExpectIsUnaryOption(options, "server_javabase");
  ExpectIsUnaryOption(options, "host_jvm_args");
  ExpectIsUnaryOption(options, "host_jvm_profile");
  ExpectIsUnaryOption(options, "invocation_policy");
  ExpectIsUnaryOption(options, "io_nice_level");
  ExpectIsUnaryOption(options, "install_base");
  ExpectIsUnaryOption(options, "max_idle_secs");
  ExpectIsUnaryOption(options, "output_base");
  ExpectIsUnaryOption(options, "output_user_root");
}

TEST_F(BazelStartupOptionsTest, BlazercFlagsAreNotAccepted) {
  EXPECT_FALSE(startup_options_->IsNullary("--master_blazerc"));
  EXPECT_FALSE(startup_options_->IsUnary("--master_blazerc"));
  EXPECT_FALSE(startup_options_->IsNullary("--blazerc"));
  EXPECT_FALSE(startup_options_->IsUnary("--blazerc"));
}

TEST_F(BazelStartupOptionsTest, IgnoredBazelrcFlagWarns) {
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(), {"--bazelrc=somefile", "--ignore_all_rc_files"},
      "WARNING: Value of --bazelrc is ignored, since --ignore_all_rc_files is "
      "on.\n");
}

TEST_F(BazelStartupOptionsTest, IgnoredBazelrcFlagWarnsWhenAfterIgnore) {
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(), {"--ignore_all_rc_files", "--bazelrc=somefile"},
      "WARNING: Value of --bazelrc is ignored, since --ignore_all_rc_files is "
      "on.\n");
}

TEST_F(BazelStartupOptionsTest, IgnoredWorkspaceRcFlagWarns) {
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(), {"--workspace_rc", "--ignore_all_rc_files"},
      "WARNING: Explicit value of --workspace_rc is ignored, "
      "since --ignore_all_rc_files is on.\n");
}

TEST_F(BazelStartupOptionsTest, IgnoredWorkspaceRcFlagWarnsAfterIgnore) {
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(), {"--ignore_all_rc_files", "--workspace_rc"},
      "WARNING: Explicit value of --workspace_rc is ignored, "
      "since --ignore_all_rc_files is on.\n");
}

TEST_F(BazelStartupOptionsTest, MultipleIgnoredRcFlagsWarnOnceEach) {
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(),
      {"--workspace_rc", "--bazelrc=somefile", "--ignore_all_rc_files",
       "--bazelrc=thefinalfile", "--workspace_rc"},
      "WARNING: Value of --bazelrc is ignored, "
      "since --ignore_all_rc_files is on.\n"
      "WARNING: Explicit value of --workspace_rc is ignored, "
      "since --ignore_all_rc_files is on.\n");
}

TEST_F(BazelStartupOptionsTest, IgnoredNoMasterBazelrcDoesNotWarn) {
  // Warning for nomaster would feel pretty spammy - it's redundant, but the
  // behavior is as one would expect, so warning is unnecessary.
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(), {"--ignore_all_rc_files", "--nomaster_bazelrc"},
      "");
}

TEST_F(BazelStartupOptionsTest, IgnoreOptionDoesNotWarnOnItsOwn) {
  ParseStartupOptionsAndExpectWarning(startup_options_.get(),
                                      {"--ignore_all_rc_files"}, "");
}

TEST_F(BazelStartupOptionsTest, NonIgnoredOptionDoesNotWarn) {
  ParseStartupOptionsAndExpectWarning(startup_options_.get(),
                                      {"--bazelrc=somefile"}, "");
}

TEST_F(BazelStartupOptionsTest, FinalValueOfIgnoreIsUsedForWarning) {
  ParseStartupOptionsAndExpectWarning(
      startup_options_.get(),
      {"--ignore_all_rc_files", "--master_bazelrc", "--noignore_all_rc_files"},
      "");
}

}  // namespace blaze