Add unittest

This commit is contained in:
Alex Eagle 2021-11-08 07:22:47 -08:00
parent bf35529fc9
commit 03eaca91f1
9 changed files with 111 additions and 6 deletions

View File

@ -1,4 +1,4 @@
4.2.0
5.0.0-pre.20211011.2
# The first line of this file is used by Bazelisk and Bazel to be sure
# the right version of Bazel is used to build and test this repo.
# This also defines which version is used on CI.

View File

@ -17,7 +17,6 @@ gazelle(
pkg_tar(
name = "bazel_lib-" + VERSION,
srcs = [
"LICENSE",
"README.md",
"version.bzl",
"//lib:package_content",

View File

@ -10,6 +10,11 @@ load(":internal_deps.bzl", "bazel_lib_internal_deps")
# Fetch deps needed only locally for development
bazel_lib_internal_deps()
# For running our own unit tests
load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")
register_unittest_toolchains()
############################################
# Gazelle, for generating bzl_library targets
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

View File

@ -2,6 +2,33 @@
Public API for expanding variables
<a id="#expand_template"></a>
## expand_template
<pre>
expand_template(<a href="#expand_template-name">name</a>, <a href="#expand_template-data">data</a>, <a href="#expand_template-is_executable">is_executable</a>, <a href="#expand_template-out">out</a>, <a href="#expand_template-substitutions">substitutions</a>, <a href="#expand_template-template">template</a>)
</pre>
Template expansion
This performs a simple search over the template file for the keys in substitutions,
and replaces them with the corresponding values.
**ATTRIBUTES**
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="expand_template-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
| <a id="expand_template-data"></a>data | List of targets for additional lookup information. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
| <a id="expand_template-is_executable"></a>is_executable | Whether to mark the output file as executable. | Boolean | optional | False |
| <a id="expand_template-out"></a>out | Where to write the expanded file. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
| <a id="expand_template-substitutions"></a>substitutions | Mapping of strings to substitutions. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
| <a id="expand_template-template"></a>template | The template file to expand. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
<a id="#expand_locations"></a>
## expand_locations

View File

@ -3,8 +3,10 @@
load(
"//lib/private:expand_make_vars.bzl",
_expand_locations = "expand_locations",
_expand_template = "expand_template",
_expand_variables = "expand_variables",
)
expand_locations = _expand_locations
expand_variables = _expand_variables
expand_template = _expand_template

View File

@ -162,3 +162,47 @@ def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "ar
additional_substitutions["RULEDIR"] = "/".join([o for o in rule_dir if o])
return ctx.expand_make_variables(attribute_name, s, additional_substitutions)
def _expand_template_impl(ctx):
ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.out,
substitutions = {
k: expand_locations(v, ctx.attr.data)
for k, v in ctx.attr.substitutions.items()
},
is_executable = ctx.attr.is_executable,
)
expand_template = rule(
doc = """Template expansion
This performs a simple search over the template file for the keys in substitutions,
and replaces them with the corresponding values.
""",
implementation = _expand_template_impl,
attrs = {
"template": attr.label(
doc = "The template file to expand.",
mandatory = True,
allow_single_file = True,
),
"substitutions": attr.string_dict(
doc = "Mapping of strings to substitutions.",
mandatory = True,
),
"out": attr.output(
doc = "Where to write the expanded file.",
mandatory = True,
),
"is_executable": attr.bool(
doc = "Whether to mark the output file as executable.",
default = False,
mandatory = False,
),
"data": attr.label_list(
doc = "List of targets for additional lookup information.",
allow_files = True,
),
},
)

View File

@ -2,9 +2,6 @@
load("//lib/private:expand_make_vars.bzl", "expand_locations")
_DOC = """Generates a params file from a list of arguments."""
# See params_file macro below for docstrings
_ATTRS = {
"args": attr.string_list(),
"data": attr.label_list(allow_files = True),
@ -53,5 +50,4 @@ params_file = rule(
implementation = _impl,
provides = [DefaultInfo],
attrs = _ATTRS,
doc = _DOC,
)

3
lib/tests/BUILD.bazel Normal file
View File

@ -0,0 +1,3 @@
load(":expand_make_vars_test.bzl", "expand_make_vars_test_suite")
expand_make_vars_test_suite(name = "expand_make_vars_test")

View File

@ -0,0 +1,29 @@
"""Unit tests for starlark helpers
See https://docs.bazel.build/versions/main/skylark/testing.html#for-testing-starlark-utilities
"""
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//lib/private:expand_make_vars.bzl", "expand_variables")
def _variables_test_impl(ctx):
env = unittest.begin(ctx)
capture_subs = {}
fake_ctx = struct(
bin_dir = struct(path = "bazel-bin"),
label = struct(
workspace_root = "my-wksp",
package = "path/to",
),
expand_make_variables = lambda attr, expr, subs: capture_subs.update(subs),
)
expand_variables(fake_ctx, "output=$(@D)")
expected = {"@D": "bazel-bin/my-wksp/path/to", "RULEDIR": "bazel-bin/my-wksp/path/to"}
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)
def expand_make_vars_test_suite(name):
unittest.suite(name, t0_test)