Skip to content
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
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions py/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bzl_library(
":py_library",
":utils",
"//py/private/venv",
"@aspect_bazel_lib//lib:expand_make_vars",
"@aspect_bazel_lib//lib:paths",
],
)
Expand Down
10 changes: 9 additions & 1 deletion py/private/py_binary.bzl
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions py/private/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ bzl_library(
name = "autodetecting",
srcs = ["autodetecting.bzl"],
visibility = ["//py:__subpackages__"],
deps = ["//py/private:utils"],
)
16 changes: 16 additions & 0 deletions py/tests/py-test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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)",
},
)
17 changes: 17 additions & 0 deletions py/tests/py-test/test_env_vars.py
Original file line number Diff line number Diff line change
@@ -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")