Skip to content

Commit

Permalink
Add startup options to bazel binary template (#350)
Browse files Browse the repository at this point in the history
Hi, as described in #349 I would like to add pass `--nosystem_rc` as a
startup option to bazel.

This PR introduces the `BIT_STARTUP_OPTIONS` environment variable in the
bazel binary template that can be used to pass arbitrary flags to bazel.
Also I added `startup_options` filed to the test macros for convenance.

---------

Co-authored-by: Chuck Grindel <chuck.grindel@gmail.com>
  • Loading branch information
LeFrosch and cgrindel committed Aug 20, 2024
1 parent 5b568fd commit c89ad21
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export BAZELISK_HOME="${PWD}"
BINARY="$(rlocation {bazelisk})" || \
(echo >&2 "Failed to locate bazelisk at {bazelisk}" && exit 1)

exec "${BINARY}" "$@"
exec "${BINARY}" ${BIT_STARTUP_OPTIONS:-} "$@"
14 changes: 13 additions & 1 deletion bazel_integration_test/private/bazel_integration_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ def bazel_integration_test(
workspace_files = None,
tags = integration_test_utils.DEFAULT_INTEGRATION_TEST_TAGS,
timeout = "long",
env = {},
env = None,
env_inherit = _DEFAULT_ENV_INHERIT,
additional_env_inherit = [],
bazel_binaries = None,
data = None,
startup_options = "",
**kwargs):
"""Macro that defines a set of targets for a single Bazel integration test.
Expand Down Expand Up @@ -96,8 +97,12 @@ def bazel_integration_test(
`load("@bazel_binaries//:defs.bzl", "bazel_binaries")` to your
build file.
data: Optional. A list of files to make present at test runtime.
startup_options: Optional. Flags that should be passed to Bazel as
startup options using the `BIT_STARTUP_OPTIONS`
environment variable.
**kwargs: additional attributes like timeout and visibility
"""
env = env or {}

# To support clients that have not transitioned to bzlmod, we provide a
# bazel_binaries implementation that works in that mode. If a client using
Expand Down Expand Up @@ -161,6 +166,8 @@ def bazel_integration_test(
if env_vars_are_rootpaths:
env["ENV_VARS_TO_ABSOLUTIFY"] = " ".join(env_vars_are_rootpaths)

env["BIT_STARTUP_OPTIONS"] = startup_options

native.sh_test(
name = name,
srcs = [
Expand All @@ -186,6 +193,7 @@ def bazel_integration_tests(
env_inherit = _DEFAULT_ENV_INHERIT,
additional_env_inherit = [],
bazel_binaries = None,
startup_options = "",
**kwargs):
"""Macro that defines a set Bazel integration tests each executed with a different version of Bazel.
Expand Down Expand Up @@ -216,6 +224,9 @@ def bazel_integration_tests(
is loaded by adding
`load("@bazel_binaries//:defs.bzl", "bazel_binaries")` to your
build file.
startup_options: Optional. Flags that should be passed to Bazel as
startup options using the `BIT_STARTUP_OPTIONS`
environment variable.
**kwargs: additional attributes like timeout and visibility
"""
if bazel_versions == []:
Expand All @@ -236,5 +247,6 @@ def bazel_integration_tests(
env_inherit = env_inherit,
additional_env_inherit = additional_env_inherit,
bazel_binaries = bazel_binaries,
startup_options = startup_options,
**kwargs
)
9 changes: 6 additions & 3 deletions doc/rules_and_macros_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ default_test_runner(<a href="#default_test_runner-name">name</a>, <a href="#defa
<pre>
bazel_integration_test(<a href="#bazel_integration_test-name">name</a>, <a href="#bazel_integration_test-test_runner">test_runner</a>, <a href="#bazel_integration_test-bazel_version">bazel_version</a>, <a href="#bazel_integration_test-bazel_binary">bazel_binary</a>, <a href="#bazel_integration_test-workspace_path">workspace_path</a>,
<a href="#bazel_integration_test-workspace_files">workspace_files</a>, <a href="#bazel_integration_test-tags">tags</a>, <a href="#bazel_integration_test-timeout">timeout</a>, <a href="#bazel_integration_test-env">env</a>, <a href="#bazel_integration_test-env_inherit">env_inherit</a>, <a href="#bazel_integration_test-additional_env_inherit">additional_env_inherit</a>,
<a href="#bazel_integration_test-bazel_binaries">bazel_binaries</a>, <a href="#bazel_integration_test-data">data</a>, <a href="#bazel_integration_test-kwargs">kwargs</a>)
<a href="#bazel_integration_test-bazel_binaries">bazel_binaries</a>, <a href="#bazel_integration_test-data">data</a>, <a href="#bazel_integration_test-startup_options">startup_options</a>, <a href="#bazel_integration_test-kwargs">kwargs</a>)
</pre>

Macro that defines a set of targets for a single Bazel integration test.
Expand Down Expand Up @@ -65,11 +65,12 @@ default test runner is provided by the `default_test_runner` macro.
| <a id="bazel_integration_test-workspace_files"></a>workspace_files | Optional. A <code>list</code> of files for the child workspace. If not specified, then it is derived from the <code>workspace_path</code>. | <code>None</code> |
| <a id="bazel_integration_test-tags"></a>tags | The Bazel tags to apply to the test declaration. | <code>["exclusive", "manual"]</code> |
| <a id="bazel_integration_test-timeout"></a>timeout | A valid Bazel timeout value. https://docs.bazel.build/versions/main/test-encyclopedia.html#role-of-the-test-runner | <code>"long"</code> |
| <a id="bazel_integration_test-env"></a>env | Optional. A dictionary of <code>strings</code>. Specifies additional environment variables to be passed to the test. | <code>{}</code> |
| <a id="bazel_integration_test-env"></a>env | Optional. A dictionary of <code>strings</code>. Specifies additional environment variables to be passed to the test. | <code>None</code> |
| <a id="bazel_integration_test-env_inherit"></a>env_inherit | Optional. Override the env_inherit values passed to the test. Only do this if you understand what needs to be passed along. Most folks will want to use <code>additional_env_inherit</code> to pass additional env_inherit values. | <code>["SUDO_ASKPASS", "HOME", "CC"]</code> |
| <a id="bazel_integration_test-additional_env_inherit"></a>additional_env_inherit | Optional. Specify additional <code>env_inherit</code> values that should be passed to the test. | <code>[]</code> |
| <a id="bazel_integration_test-bazel_binaries"></a>bazel_binaries | Optional for WORKSPACE loaded repositories. Required for repositories that enable bzlmod. The value for this parameter is loaded by adding <code>load("@bazel_binaries//:defs.bzl", "bazel_binaries")</code> to your build file. | <code>None</code> |
| <a id="bazel_integration_test-data"></a>data | Optional. A list of files to make present at test runtime. | <code>None</code> |
| <a id="bazel_integration_test-startup_options"></a>startup_options | Optional. Flags that should be passed to Bazel as startup options using the <code>BIT_STARTUP_OPTIONS</code> environment variable. | <code>""</code> |
| <a id="bazel_integration_test-kwargs"></a>kwargs | additional attributes like timeout and visibility | none |


Expand All @@ -79,7 +80,8 @@ default test runner is provided by the `default_test_runner` macro.

<pre>
bazel_integration_tests(<a href="#bazel_integration_tests-name">name</a>, <a href="#bazel_integration_tests-test_runner">test_runner</a>, <a href="#bazel_integration_tests-bazel_versions">bazel_versions</a>, <a href="#bazel_integration_tests-workspace_path">workspace_path</a>, <a href="#bazel_integration_tests-workspace_files">workspace_files</a>, <a href="#bazel_integration_tests-tags">tags</a>,
<a href="#bazel_integration_tests-timeout">timeout</a>, <a href="#bazel_integration_tests-env_inherit">env_inherit</a>, <a href="#bazel_integration_tests-additional_env_inherit">additional_env_inherit</a>, <a href="#bazel_integration_tests-bazel_binaries">bazel_binaries</a>, <a href="#bazel_integration_tests-kwargs">kwargs</a>)
<a href="#bazel_integration_tests-timeout">timeout</a>, <a href="#bazel_integration_tests-env_inherit">env_inherit</a>, <a href="#bazel_integration_tests-additional_env_inherit">additional_env_inherit</a>, <a href="#bazel_integration_tests-bazel_binaries">bazel_binaries</a>, <a href="#bazel_integration_tests-startup_options">startup_options</a>,
<a href="#bazel_integration_tests-kwargs">kwargs</a>)
</pre>

Macro that defines a set Bazel integration tests each executed with a different version of Bazel.
Expand All @@ -99,6 +101,7 @@ Macro that defines a set Bazel integration tests each executed with a different
| <a id="bazel_integration_tests-env_inherit"></a>env_inherit | Optional. Override the env_inherit values passed to the test. Only do this if you understand what needs to be passed along. Most folks will want to use <code>additional_env_inherit</code> to pass additional env_inherit values. | <code>["SUDO_ASKPASS", "HOME", "CC"]</code> |
| <a id="bazel_integration_tests-additional_env_inherit"></a>additional_env_inherit | Optional. Specify additional <code>env_inherit</code> values that should be passed to the test. | <code>[]</code> |
| <a id="bazel_integration_tests-bazel_binaries"></a>bazel_binaries | Optional for WORKSPACE loaded repositories. Required for repositories that enable bzlmod. The value for this parameter is loaded by adding <code>load("@bazel_binaries//:defs.bzl", "bazel_binaries")</code> to your build file. | <code>None</code> |
| <a id="bazel_integration_tests-startup_options"></a>startup_options | Optional. Flags that should be passed to Bazel as startup options using the <code>BIT_STARTUP_OPTIONS</code> environment variable. | <code>""</code> |
| <a id="bazel_integration_tests-kwargs"></a>kwargs | additional attributes like timeout and visibility | none |


0 comments on commit c89ad21

Please sign in to comment.