diff --git a/.bazelrc b/.bazelrc index 4375f437..f6b338c5 100644 --- a/.bazelrc +++ b/.bazelrc @@ -4,6 +4,9 @@ test --test_output=errors +# Define value used by tests +build --define=SOME_VAR=SOME_VALUE + # Load any settings specific to the current user. # .bazelrc.user should appear in .gitignore so that settings are not shared with team members # This needs to be last statement in this diff --git a/BUILD.bazel b/BUILD.bazel index cfda797e..82eed49b 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -3,6 +3,7 @@ load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary") # gazelle:exclude internal_python_deps.bzl # gazelle:exclude internal_deps.bzl +# gazelle:exclude py/tests/ gazelle_binary( name = "gazelle_bin", diff --git a/py/private/BUILD.bazel b/py/private/BUILD.bazel index 8fb7be54..5c466675 100644 --- a/py/private/BUILD.bazel +++ b/py/private/BUILD.bazel @@ -23,6 +23,7 @@ bzl_library( ":py_library", ":utils", "//py/private/venv", + "@aspect_bazel_lib//lib:expand_make_vars", "@aspect_bazel_lib//lib:paths", ], ) diff --git a/py/private/py_binary.bzl b/py/private/py_binary.bzl index d6cd97ee..14616ad7 100644 --- a/py/private/py_binary.bzl +++ b/py/private/py_binary.bzl @@ -1,6 +1,7 @@ "Implementation for the py_binary and py_test rules." load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_manifest_path") +load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables") load("//py/private:py_library.bzl", _py_library = "py_library_utils") load("//py/private:providers.bzl", "PyWheelInfo") load("//py/private:utils.bzl", "PY_TOOLCHAIN", "SH_TOOLCHAIN", "dict_to_exports", "resolve_toolchain") @@ -18,11 +19,18 @@ def _py_binary_rule_imp(ctx): ) env = dict({ - "BAZEL_TARGET": ctx.label, + "BAZEL_TARGET": str(ctx.label), "BAZEL_WORKSPACE": ctx.workspace_name, "BAZEL_TARGET_NAME": ctx.attr.name, }, **ctx.attr.env) + for k, v in env.items(): + env[k] = expand_variables( + ctx, + expand_locations(ctx, v, ctx.attr.data), + attribute_name = "env", + ) + python_interpreter_path = interpreter.python.path if interpreter.uses_interpreter_path else to_manifest_path(ctx, interpreter.python) common_substitutions = { diff --git a/py/private/toolchain/BUILD.bazel b/py/private/toolchain/BUILD.bazel index e15a82f4..84703704 100644 --- a/py/private/toolchain/BUILD.bazel +++ b/py/private/toolchain/BUILD.bazel @@ -9,4 +9,5 @@ bzl_library( name = "autodetecting", srcs = ["autodetecting.bzl"], visibility = ["//py:__subpackages__"], + deps = ["//py/private:utils"], ) diff --git a/py/tests/py-test/BUILD.bazel b/py/tests/py-test/BUILD.bazel new file mode 100644 index 00000000..ba24dc68 --- /dev/null +++ b/py/tests/py-test/BUILD.bazel @@ -0,0 +1,16 @@ +load("//py:defs.bzl", "py_test") + +py_test( + name = "test_env_vars", + srcs = ["test_env_vars.py"], + data = [ + "test_env_vars.py", + ], + env = { + "ONE": "un", + "TWO": "deux", + "RULEDIR": "$(RULEDIR)", + "LOCATION": "$(location :test_env_vars.py)", + "DEFINE": "$(SOME_VAR)", + }, +) diff --git a/py/tests/py-test/test_env_vars.py b/py/tests/py-test/test_env_vars.py new file mode 100644 index 00000000..0941b999 --- /dev/null +++ b/py/tests/py-test/test_env_vars.py @@ -0,0 +1,17 @@ +import os + + +def test_env(env, expected): + assert env in os.environ, f"Expected environ to have key '{env}'" + + _actual = os.environ.get(env) + assert _actual == expected, f"Expected environ key '{env}' to equal '{expected}', but got '{_actual}'" + + +test_env('ONE', 'un') +test_env('TWO', 'deux') +test_env('LOCATION', "py/tests/py-test/test_env_vars.py") +test_env('DEFINE', "SOME_VALUE") +test_env('BAZEL_TARGET', "//py/tests/py-test:test_env_vars") +test_env('BAZEL_WORKSPACE', "aspect_rules_py") +test_env('BAZEL_TARGET_NAME', "test_env_vars")