diff --git a/WORKSPACE b/WORKSPACE index 2a5edc0..afbbc26 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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, @@ -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", +) diff --git a/deps.bzl b/deps.bzl index d64150e..67b93af 100644 --- a/deps.bzl +++ b/deps.bzl @@ -74,8 +74,9 @@ filegroup( name = "load_files", srcs = [ "load.bash", - "src/assert.bash", - ], + ] + glob([ + "src/**/*.bash", + ]), visibility = ["//visibility:public"], ) """ @@ -85,10 +86,9 @@ filegroup( name = "load_files", srcs = [ "load.bash", - "src/error.bash", - "src/lang.bash", - "src/output.bash", - ], + ] + glob([ + "src/**/*.bash", + ]), visibility = ["//visibility:public"], ) """ diff --git a/rules.bzl b/rules.bzl index 9bc6e3a..fbd1e21 100644 --- a/rules.bzl +++ b/rules.bzl @@ -38,7 +38,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, ctx.attr)], @@ -102,7 +111,7 @@ _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"), ), @@ -110,7 +119,6 @@ _bats_with_bats_assert_test_attrs = { default = Label("@bats_assert//:load_files"), ), } -_bats_with_bats_assert_test_attrs.update(_bats_test_attrs) _bats_test = rule( _bats_test_impl, diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 08c56b4..4e50f4b 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -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", @@ -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( @@ -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", +) diff --git a/tests/hello_world.bats b/tests/hello_world.bats index c5682ce..b91867c 100644 --- a/tests/hello_world.bats +++ b/tests/hello_world.bats @@ -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" ] +}