Skip to content

Commit 03eaca9

Browse files
committed
Add unittest
1 parent bf35529 commit 03eaca9

9 files changed

+111
-6
lines changed

.bazelversion

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

BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ gazelle(
1717
pkg_tar(
1818
name = "bazel_lib-" + VERSION,
1919
srcs = [
20-
"LICENSE",
2120
"README.md",
2221
"version.bzl",
2322
"//lib:package_content",

WORKSPACE

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ load(":internal_deps.bzl", "bazel_lib_internal_deps")
1010
# Fetch deps needed only locally for development
1111
bazel_lib_internal_deps()
1212

13+
# For running our own unit tests
14+
load("@bazel_skylib//lib:unittest.bzl", "register_unittest_toolchains")
15+
16+
register_unittest_toolchains()
17+
1318
############################################
1419
# Gazelle, for generating bzl_library targets
1520
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

docs/expand_make_vars.md

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22

33
Public API for expanding variables
44

5+
<a id="#expand_template"></a>
6+
7+
## expand_template
8+
9+
<pre>
10+
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>)
11+
</pre>
12+
13+
Template expansion
14+
15+
This performs a simple search over the template file for the keys in substitutions,
16+
and replaces them with the corresponding values.
17+
18+
19+
**ATTRIBUTES**
20+
21+
22+
| Name | Description | Type | Mandatory | Default |
23+
| :------------- | :------------- | :------------- | :------------- | :------------- |
24+
| <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 | |
25+
| <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 | [] |
26+
| <a id="expand_template-is_executable"></a>is_executable | Whether to mark the output file as executable. | Boolean | optional | False |
27+
| <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 | |
28+
| <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 | |
29+
| <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 | |
30+
31+
532
<a id="#expand_locations"></a>
633

734
## expand_locations

lib/expand_make_vars.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
load(
44
"//lib/private:expand_make_vars.bzl",
55
_expand_locations = "expand_locations",
6+
_expand_template = "expand_template",
67
_expand_variables = "expand_variables",
78
)
89

910
expand_locations = _expand_locations
1011
expand_variables = _expand_variables
12+
expand_template = _expand_template

lib/private/expand_make_vars.bzl

+44
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,47 @@ def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "ar
162162
additional_substitutions["RULEDIR"] = "/".join([o for o in rule_dir if o])
163163

164164
return ctx.expand_make_variables(attribute_name, s, additional_substitutions)
165+
166+
def _expand_template_impl(ctx):
167+
ctx.actions.expand_template(
168+
template = ctx.file.template,
169+
output = ctx.outputs.out,
170+
substitutions = {
171+
k: expand_locations(v, ctx.attr.data)
172+
for k, v in ctx.attr.substitutions.items()
173+
},
174+
is_executable = ctx.attr.is_executable,
175+
)
176+
177+
expand_template = rule(
178+
doc = """Template expansion
179+
180+
This performs a simple search over the template file for the keys in substitutions,
181+
and replaces them with the corresponding values.
182+
""",
183+
implementation = _expand_template_impl,
184+
attrs = {
185+
"template": attr.label(
186+
doc = "The template file to expand.",
187+
mandatory = True,
188+
allow_single_file = True,
189+
),
190+
"substitutions": attr.string_dict(
191+
doc = "Mapping of strings to substitutions.",
192+
mandatory = True,
193+
),
194+
"out": attr.output(
195+
doc = "Where to write the expanded file.",
196+
mandatory = True,
197+
),
198+
"is_executable": attr.bool(
199+
doc = "Whether to mark the output file as executable.",
200+
default = False,
201+
mandatory = False,
202+
),
203+
"data": attr.label_list(
204+
doc = "List of targets for additional lookup information.",
205+
allow_files = True,
206+
),
207+
},
208+
)

lib/private/params_file.bzl

-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
load("//lib/private:expand_make_vars.bzl", "expand_locations")
44

5-
_DOC = """Generates a params file from a list of arguments."""
6-
7-
# See params_file macro below for docstrings
85
_ATTRS = {
96
"args": attr.string_list(),
107
"data": attr.label_list(allow_files = True),
@@ -53,5 +50,4 @@ params_file = rule(
5350
implementation = _impl,
5451
provides = [DefaultInfo],
5552
attrs = _ATTRS,
56-
doc = _DOC,
5753
)

lib/tests/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":expand_make_vars_test.bzl", "expand_make_vars_test_suite")
2+
3+
expand_make_vars_test_suite(name = "expand_make_vars_test")

lib/tests/expand_make_vars_test.bzl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Unit tests for starlark helpers
2+
See https://docs.bazel.build/versions/main/skylark/testing.html#for-testing-starlark-utilities
3+
"""
4+
5+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
6+
load("//lib/private:expand_make_vars.bzl", "expand_variables")
7+
8+
def _variables_test_impl(ctx):
9+
env = unittest.begin(ctx)
10+
capture_subs = {}
11+
fake_ctx = struct(
12+
bin_dir = struct(path = "bazel-bin"),
13+
label = struct(
14+
workspace_root = "my-wksp",
15+
package = "path/to",
16+
),
17+
expand_make_variables = lambda attr, expr, subs: capture_subs.update(subs),
18+
)
19+
expand_variables(fake_ctx, "output=$(@D)")
20+
expected = {"@D": "bazel-bin/my-wksp/path/to", "RULEDIR": "bazel-bin/my-wksp/path/to"}
21+
asserts.equals(env, expected, capture_subs)
22+
return unittest.end(env)
23+
24+
# The unittest library requires that we export the test cases as named test rules,
25+
# but their names are arbitrary and don't appear anywhere.
26+
t0_test = unittest.make(_variables_test_impl)
27+
28+
def expand_make_vars_test_suite(name):
29+
unittest.suite(name, t0_test)

0 commit comments

Comments
 (0)