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

Env Var Expansion and Cleanup #23

Merged
merged 1 commit into from
Oct 27, 2023
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
13 changes: 13 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BATS_SUPPORT_VERSION = "0.3.0"
BATS_SUPPORT_SHA256 = "7815237aafeb42ddcc1b8c698fc5808026d33317d8701d5ec2396e9634e2918f"

load("@bazel_bats//:deps.bzl", "bazel_bats_dependencies")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

bazel_bats_dependencies(
version = BATS_CORE_VERSION,
Expand All @@ -19,3 +20,15 @@ bazel_bats_dependencies(
bats_support_version = BATS_SUPPORT_VERSION,
bats_support_sha256 = BATS_SUPPORT_SHA256
)

# Used for testing support/compatibility with `string_flag()` from
# `@bazel_skylib//rules:common_settings.bzl` (specifically testing make variable support).
# TODO: Update from `git_repository()` to `http_archive()` once a tag/release >1.4.2 is out.
# Specifically looking for changes from https://github.com/bazelbuild/bazel-skylib/pull/440.
# See https://github.com/bazelbuild/bazel-skylib/issues/471.
git_repository(
name = "bazel_skylib",
# Latest `main` commit that contains desired changes.
commit = "652c8f0b2817daaa2570b7a3b2147643210f7dc7",
remote = "https://github.com/bazelbuild/bazel-skylib.git",
)
12 changes: 6 additions & 6 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ filegroup(
name = "load_files",
srcs = [
"load.bash",
"src/assert.bash",
],
] + glob([
filmil marked this conversation as resolved.
Show resolved Hide resolved
"src/**/*.bash",
]),
visibility = ["//visibility:public"],
)
"""
Expand All @@ -85,10 +86,9 @@ filegroup(
name = "load_files",
srcs = [
"load.bash",
"src/error.bash",
"src/lang.bash",
"src/output.bash",
],
] + glob([
filmil marked this conversation as resolved.
Show resolved Hide resolved
"src/**/*.bash",
]),
visibility = ["//visibility:public"],
)
"""
Expand Down
14 changes: 11 additions & 3 deletions rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ def _bats_test_impl(ctx):
["export TMPDIR=\"$TEST_TMPDIR\""] +
["export PATH=\"{bats_bins_path}\":$PATH".format(bats_bins_path = sep.join(path))] +
[
'export {}="{}"'.format(key, ctx.expand_location(val, ctx.attr.deps))
# First try and expand `$(location ...)`.
# Then try for make variables (possibly supplied by toolchains).
'export {}="{}"'.format(
key,
ctx.expand_make_variables(
key,
ctx.expand_location(val, ctx.attr.deps),
{}
)
)
for key, val in ctx.attr.env.items()
] +
[_test_files(ctx.executable._bats, ctx.files.srcs)],
Expand Down Expand Up @@ -97,15 +106,14 @@ _bats_test_attrs = {
),
}

_bats_with_bats_assert_test_attrs = {
_bats_with_bats_assert_test_attrs = _bats_test_attrs | {
"_bats_support": attr.label(
default = Label("@bats_support//:load_files"),
),
"_bats_assert": attr.label(
default = Label("@bats_assert//:load_files"),
),
}
_bats_with_bats_assert_test_attrs.update(_bats_test_attrs)

_bats_test = rule(
_bats_test_impl,
Expand Down
11 changes: 11 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_bats//:rules.bzl", "bats_test", "bats_test_suite")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

sh_binary(
name = "exit_with_input_bin",
Expand Down Expand Up @@ -57,8 +58,12 @@ bats_test(
env = {
"PROGRAM": "hello_world",
"LOCATED": "$(location :dummy)",
"TOOLCHAIN_STRING_FLAG_ENV_VAR": "$(STRING_FLAG_ENV_VAR)",
},
deps = [":dummy"],
toolchains = [
":hello_world_flag",
],
)

bats_test(
Expand Down Expand Up @@ -95,3 +100,9 @@ bats_test_suite(
"hello_world_2.bats",
]
)

string_flag(
name = "hello_world_flag",
build_setting_default = "flag_value",
make_variable = "STRING_FLAG_ENV_VAR",
)
9 changes: 7 additions & 2 deletions tests/hello_world.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
[ "$result" -eq 4 ]
}

@test "Test program name" {
@test "Test simple environment variable" {
[ "${PROGRAM}" == "hello_world" ]
}

@test "Test program name with location" {
@test "Test environment variable expanded from bazel location" {
echo "Location: ${LOCATED}"
[ "${LOCATED}" == "tests/dummy.txt" ]
}

@test "Test environment variable expanded from bazel string flag" {
echo "Flag env var: ${TOOLCHAIN_STRING_FLAG_ENV_VAR}"
[ "${TOOLCHAIN_STRING_FLAG_ENV_VAR}" == "flag_value" ]
}