Skip to content

Commit 25a8e9d

Browse files
authored
Add new "env" variable to native_* rules (#561)
This (standard) variable "env" allows the propagation of environment variables to the test being executed or tested.
1 parent 97a6270 commit 25a8e9d

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

docs/native_binary_doc.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ don't depend on Bash and work with --shell_executable="".
1414
<pre>
1515
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
1616

17-
native_binary(<a href="#native_binary-name">name</a>, <a href="#native_binary-src">src</a>, <a href="#native_binary-data">data</a>, <a href="#native_binary-out">out</a>)
17+
native_binary(<a href="#native_binary-name">name</a>, <a href="#native_binary-src">src</a>, <a href="#native_binary-data">data</a>, <a href="#native_binary-out">out</a>, <a href="#native_binary-env">env</a>)
1818
</pre>
1919

2020
Wraps a pre-built binary or script with a binary rule.
@@ -31,6 +31,7 @@ in genrule.tools for example. You can also augment the binary with runfiles.
3131
| <a id="native_binary-src"></a>src | path of the pre-built executable | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
3232
| <a id="native_binary-data"></a>data | data dependencies. See https://bazel.build/reference/be/common-definitions#typical.data | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
3333
| <a id="native_binary-out"></a>out | An output name for the copy of the binary. Defaults to name.exe. (We add .exe to the name by default because it's required on Windows and tolerated on other platforms.) | String | optional | `""` |
34+
| <a id="native_binary-env"></a>env | Environment variables to set when running the binary. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
3435

3536

3637
<a id="native_test"></a>
@@ -40,7 +41,7 @@ in genrule.tools for example. You can also augment the binary with runfiles.
4041
<pre>
4142
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
4243

43-
native_test(<a href="#native_test-name">name</a>, <a href="#native_test-src">src</a>, <a href="#native_test-data">data</a>, <a href="#native_test-out">out</a>)
44+
native_test(<a href="#native_test-name">name</a>, <a href="#native_test-src">src</a>, <a href="#native_test-data">data</a>, <a href="#native_test-out">out</a>, <a href="#native_test-env">env</a>)
4445
</pre>
4546

4647
Wraps a pre-built binary or script with a test rule.
@@ -57,5 +58,6 @@ the binary with runfiles.
5758
| <a id="native_test-src"></a>src | path of the pre-built executable | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
5859
| <a id="native_test-data"></a>data | data dependencies. See https://bazel.build/reference/be/common-definitions#typical.data | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
5960
| <a id="native_test-out"></a>out | An output name for the copy of the binary. Defaults to name.exe. (We add .exe to the name by default because it's required on Windows and tolerated on other platforms.) | String | optional | `""` |
61+
| <a id="native_test-env"></a>env | Environment variables to set when running the binary. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
6062

6163

rules/native_binary.bzl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ def _impl_rule(ctx):
3434
for d in ctx.attr.data + [ctx.attr.src]
3535
])
3636

37-
return DefaultInfo(
38-
executable = out,
39-
files = depset([out]),
40-
runfiles = runfiles,
41-
)
37+
return [
38+
DefaultInfo(
39+
executable = out,
40+
files = depset([out]),
41+
runfiles = runfiles,
42+
),
43+
RunEnvironmentInfo(
44+
environment = ctx.attr.env,
45+
),
46+
]
4247

4348
_ATTRS = {
4449
"src": attr.label(
@@ -63,6 +68,9 @@ _ATTRS = {
6368
"name.exe. (We add .exe to the name by default because it's " +
6469
"required on Windows and tolerated on other platforms.)",
6570
),
71+
"env": attr.string_dict(
72+
doc = "Environment variables to set when running the binary.",
73+
),
6674
}
6775

6876
native_binary = rule(

tests/native_binary/BUILD

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ copy_file(
5555
is_executable = True,
5656
)
5757

58+
cc_binary(
59+
name = "assertenv",
60+
srcs = ["assertenv.cc"],
61+
)
62+
63+
# A rule that copies "assertenv"'s output as an opaque executable, simulating a
64+
# binary that's not built from source and needs to be wrapped in native_binary.
65+
copy_file(
66+
name = "copy_assertenv_exe",
67+
src = ":assertenv",
68+
# On Windows we need the ".exe" extension.
69+
# On other platforms the extension doesn't matter.
70+
# Therefore we can use ".exe" on every platform.
71+
out = "assertenv_copy.exe",
72+
is_executable = True,
73+
)
74+
5875
_ARGS = [
5976
"'a b'",
6077
"c\\ d",
@@ -122,3 +139,23 @@ native_test(
122139
# Therefore we can use ".exe" on every platform.
123140
out = "data_from_binary_test.exe",
124141
)
142+
143+
native_binary(
144+
name = "env_bin",
145+
src = ":copy_assertenv_exe",
146+
# On Windows we need the ".exe" extension.
147+
# On other platforms the extension doesn't matter.
148+
# Therefore we can use ".exe" on every platform.
149+
out = "env_bin.exe",
150+
env = {"TEST_ENV_VAR": "ENV_VALUE"},
151+
)
152+
153+
native_test(
154+
name = "env_test",
155+
src = ":copy_assertenv_exe",
156+
# On Windows we need the ".exe" extension.
157+
# On other platforms the extension doesn't matter.
158+
# Therefore we can use ".exe" on every platform.
159+
out = "env_test.exe",
160+
env = {"TEST_ENV_VAR": "ENV_VALUE"},
161+
)

tests/native_binary/assertenv.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 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+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
19+
#include <utility>
20+
21+
int main(int argc, char** argv) {
22+
static const std::pair<const char*, const char*> kExpected[] = {
23+
{"TEST_ENV_VAR", "ENV_VALUE"}};
24+
25+
for (auto expected : kExpected) {
26+
const char* key = expected.first;
27+
const char* value = expected.second;
28+
if (strcmp(getenv(key), value)) {
29+
fprintf(stderr, "Expected %s to be %s, but it was %s\n", key, value,
30+
getenv(key));
31+
return 1;
32+
} else {
33+
printf("key[%s]=(%s) OK\n", key, value);
34+
}
35+
}
36+
return 0;
37+
}
38+

0 commit comments

Comments
 (0)