2021-11-08 15:22:47 +00:00
|
|
|
"""Unit tests for starlark helpers
|
|
|
|
See https://docs.bazel.build/versions/main/skylark/testing.html#for-testing-starlark-utilities
|
|
|
|
"""
|
|
|
|
|
2022-08-20 19:53:12 +00:00
|
|
|
load("@bazel_skylib//lib:partial.bzl", "partial")
|
2021-11-08 15:22:47 +00:00
|
|
|
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
|
2022-08-12 02:30:45 +00:00
|
|
|
load("//lib:expand_make_vars.bzl", "expand_variables")
|
2021-11-08 15:22:47 +00:00
|
|
|
|
|
|
|
def _variables_test_impl(ctx):
|
|
|
|
env = unittest.begin(ctx)
|
|
|
|
capture_subs = {}
|
|
|
|
fake_ctx = struct(
|
|
|
|
bin_dir = struct(path = "bazel-bin"),
|
2022-04-14 14:05:28 +00:00
|
|
|
label = struct(workspace_name = "my-wksp", workspace_root = "my-wksp", package = "path/to", name = "target"),
|
2021-11-08 15:22:47 +00:00
|
|
|
expand_make_variables = lambda attr, expr, subs: capture_subs.update(subs),
|
2022-04-14 14:05:28 +00:00
|
|
|
build_file_path = "some/path/BUILD.bazel",
|
|
|
|
version_file = struct(path = "bazel-out/volatile-status.txt"),
|
|
|
|
info_file = struct(path = "bazel-out/stable-status.txt"),
|
|
|
|
workspace_name = "my-wksp",
|
2021-11-08 15:22:47 +00:00
|
|
|
)
|
|
|
|
expand_variables(fake_ctx, "output=$(@D)")
|
2022-04-14 14:05:28 +00:00
|
|
|
expected = {
|
|
|
|
"@D": "bazel-bin/my-wksp/path/to",
|
|
|
|
"RULEDIR": "bazel-bin/my-wksp/path/to",
|
|
|
|
"BUILD_FILE_PATH": "some/path/BUILD.bazel",
|
|
|
|
"VERSION_FILE": "bazel-out/volatile-status.txt",
|
|
|
|
"INFO_FILE": "bazel-out/stable-status.txt",
|
|
|
|
"TARGET": "@my-wksp//path/to:target",
|
|
|
|
"WORKSPACE": "my-wksp",
|
|
|
|
}
|
2021-11-08 15:22:47 +00:00
|
|
|
asserts.equals(env, expected, capture_subs)
|
|
|
|
return unittest.end(env)
|
|
|
|
|
|
|
|
# The unittest library requires that we export the test cases as named test rules,
|
|
|
|
# but their names are arbitrary and don't appear anywhere.
|
|
|
|
t0_test = unittest.make(_variables_test_impl)
|
|
|
|
|
2021-11-09 23:13:30 +00:00
|
|
|
def expand_make_vars_test_suite():
|
2022-08-20 19:53:12 +00:00
|
|
|
unittest.suite("make_vars_tests", partial.make(t0_test, timeout = "short"))
|