Skip to content

Commit ce384ad

Browse files
A Googlercopybara-github
authored andcommitted
This change adds the standard 'env' attribute to the android_local_test rule.
The value of the attribute is a string dictionary. The values in the dictionary have 'make variables' expanded. PiperOrigin-RevId: 582043784 Change-Id: I254cccafe642954aa6fb367a576708b065c0b52f
1 parent 3a5f30f commit ce384ad

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

rules/android_local_test/attrs.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ def make_attrs(additional_aspects = [], native_libs_transition = None):
172172
"added to the manifest if it does not already contain a " +
173173
"superset listing.",
174174
),
175+
env = attr.string_dict(
176+
doc = "A dictionary of environment variables set for the execution of the test. Will be subject to make variable and $(location) expansion.",
177+
),
175178
robolectric_properties_file = attr.string(
176179
doc = "The classpath to robolectric-deps.properties file.",
177180
default = "${JAVA_RUNFILES}/robolectric/bazel/robolectric-deps.properties",

rules/android_local_test/impl.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,12 @@ def _process_stub(ctx, deploy_jar_ctx, jvm_ctx, stub_preprocess_ctx, **_unused_s
310310
jvm_ctx.coverage_start_class,
311311
merged_instr,
312312
)
313+
313314
return ProviderInfo(
314315
name = "stub_ctx",
315316
value = struct(
316317
stub = stub,
318+
providers = [testing.TestEnvironment(utils.expand_make_vars(ctx, ctx.attr.env))],
317319
),
318320
runfiles = ctx.runfiles(
319321
files = runfiles,

test/rules/android_local_test/java/com/starlark_resources/BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ android_local_test(
3232
],
3333
)
3434

35+
android_local_test(
36+
name = "environment",
37+
srcs = ["SampleTestWithEnvSetting.java"],
38+
data = ["env_location_data.txt"],
39+
env = {
40+
"android_local_test_test_env": "peekaboo",
41+
"android_local_test_loc_env": "$(location :env_location_data.txt)",
42+
},
43+
manifest = "AndroidManifest.xml",
44+
test_class = "com.starlark_resources.SampleTestWithEnvSetting",
45+
deps = [
46+
"//java/com/google/thirdparty/robolectric",
47+
"//third_party/java/hamcrest:android",
48+
"//third_party/java/robolectric",
49+
"@rules_android_maven//:junit_junit",
50+
],
51+
)
52+
3553
rule_test(
3654
name = "no_deps_with_resources_rule_test",
3755
target_under_test = ":no_deps_with_resources",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2021 The Bazel Authors. All rights reserved.
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.starlark_resources;
15+
16+
import static org.hamcrest.CoreMatchers.containsString;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.junit.Assert.assertEquals;
19+
20+
import androidx.test.ext.junit.runners.AndroidJUnit4;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
@RunWith(AndroidJUnit4.class)
25+
public class SampleTestWithEnvSetting {
26+
27+
@Test
28+
public void test() throws Exception {
29+
assertEquals("peekaboo", System.getenv("android_local_test_test_env"));
30+
assertThat(
31+
System.getenv("android_local_test_loc_env"), containsString("env_location_data.txt"));
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is used to validate environment setting with $(location).

0 commit comments

Comments
 (0)