Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for inherited_environment in scala_test rule #1401

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ tasks:
bazel: 4.1.0
shell_commands:
- "./test_coverage.sh"
test_bazel_52_linux:
name: "./test_bazel_5_2"
platform: ubuntu2004
bazel: 5.2.0rc2
shell_commands:
- "./test_bazel_5_2.sh"
test_bazel_52_macos:
name: "./test_bazel_5_2"
platform: macos
bazel: 5.2.0rc2
shell_commands:
- "./test_bazel_5_2.sh "
test_reproducibility_linux:
name: "./test_reproducibility.sh"
platform: ubuntu1804
Expand Down
5 changes: 4 additions & 1 deletion docs/scala_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ scala_test(
scalac_jvm_flags,
javac_jvm_flags,
unused_dependency_checker_mode
env_inherit
)
```

Expand All @@ -25,4 +26,6 @@ using the `scalatest` library. It may depend on `scala_library`,
By default, `scala_test` runs _all_ tests in a given target.
For backwards compatibility, it accepts a `suites` attribute which
is ignored due to the ease with which that field is not correctly
populated and tests are not run.
populated and tests are not run.

Note: If you need to pass environment variables to your tests, you can use `env_inherit` with the list of environment variables names to pass. This flag require Bazel version 5.2 rc0 or higher.
30 changes: 27 additions & 3 deletions scala/private/phases/phase_test_environment.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,36 @@

def phase_test_environment(ctx, p):
test_env = ctx.attr.env
inherited_environment = ctx.attr.env_inherit

if test_env:
if inherited_environment and test_env:
return struct(
external_providers = {
"TestingEnvironment": testing.TestEnvironment(test_env),
"TestingEnvironment": testing.TestEnvironment(
test_env,
inherited_environment,
),
},
)

return struct()
elif test_env:
return struct(
external_providers = {
"TestingEnvironment": testing.TestEnvironment(
test_env,
),
},
)

elif inherited_environment:
return struct(
external_providers = {
"TestingEnvironment": testing.TestEnvironment(
{},
inherited_environment,
),
},
)

else:
return struct()
1 change: 1 addition & 0 deletions scala/private/rules/scala_junit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ _scala_junit_test_attrs = {
providers = [java_common.JavaRuntimeInfo],
),
"env": attr.string_dict(default = {}),
"env_inherit": attr.string_list(),
"_junit_classpath": attr.label(
default = Label("@io_bazel_rules_scala//testing/toolchain:junit_classpath"),
),
Expand Down
4 changes: 4 additions & 0 deletions scala/private/rules/scala_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ load(
"phase_merge_jars",
"phase_runfiles_scalatest",
"phase_scalac_provider",
"phase_test_environment",
"phase_write_executable_scalatest",
"phase_write_manifest",
"run_phases",
Expand All @@ -47,6 +48,7 @@ def _scala_test_impl(ctx):
("coverage_runfiles", phase_coverage_runfiles),
("write_executable", phase_write_executable_scalatest),
("default_info", phase_default_info),
("test_environment", phase_test_environment),
],
)

Expand Down Expand Up @@ -75,6 +77,8 @@ _scala_test_attrs = {
"_lcov_merger": attr.label(
default = Label("@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main"),
),
"env": attr.string_dict(default = {}),
"env_inherit": attr.string_list(),
}

_test_resolve_deps = {
Expand Down
11 changes: 11 additions & 0 deletions test/shell/test_inherited_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# shellcheck source=./test_runner.sh
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
runner=$(get_test_runner "${1:-local}")

function test_inherited_environment() {
a=b bazel test //test_bazel_5_2/inherited_environment:a
}

$runner test_inherited_environment
15 changes: 15 additions & 0 deletions test_bazel_5_2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e

if ! bazel_loc="$(type -p 'bazel')" || [[ -z "$bazel_loc" ]]; then
export PATH="$(cd "$(dirname "$0")"; pwd)"/tools:$PATH
echo 'Using ./tools/bazel directly for bazel calls'
fi

test_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/test/shell
# shellcheck source=./test_runner.sh
. "${test_dir}"/test_runner.sh
runner=$(get_test_runner "${1:-local}")

. "${test_dir}"/test_inherited_environment.sh
8 changes: 8 additions & 0 deletions test_bazel_5_2/inherited_environment/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import org.scalatest.funsuite._
import org.scalatest.matchers.should._

class A extends AnyFunSuite with Matchers {
test("number is positive") {
sys.env("a") should equal("b")
}
}
7 changes: 7 additions & 0 deletions test_bazel_5_2/inherited_environment/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_test")

scala_test(
name = "a",
srcs = ["A.scala"],
env_inherit = ["a"],
)