mirror of
https://github.com/bazel-contrib/bazel-lib
synced 2024-11-27 17:43:27 +00:00
Add unittest
This commit is contained in:
parent
bf35529fc9
commit
03eaca91f1
|
@ -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 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.
|
# the right version of Bazel is used to build and test this repo.
|
||||||
# This also defines which version is used on CI.
|
# This also defines which version is used on CI.
|
||||||
|
|
|
@ -17,7 +17,6 @@ gazelle(
|
||||||
pkg_tar(
|
pkg_tar(
|
||||||
name = "bazel_lib-" + VERSION,
|
name = "bazel_lib-" + VERSION,
|
||||||
srcs = [
|
srcs = [
|
||||||
"LICENSE",
|
|
||||||
"README.md",
|
"README.md",
|
||||||
"version.bzl",
|
"version.bzl",
|
||||||
"//lib:package_content",
|
"//lib:package_content",
|
||||||
|
|
|
@ -10,6 +10,11 @@ load(":internal_deps.bzl", "bazel_lib_internal_deps")
|
||||||
# Fetch deps needed only locally for development
|
# Fetch deps needed only locally for development
|
||||||
bazel_lib_internal_deps()
|
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
|
# Gazelle, for generating bzl_library targets
|
||||||
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
|
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
|
||||||
|
|
|
@ -2,6 +2,33 @@
|
||||||
|
|
||||||
Public API for expanding variables
|
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>
|
<a id="#expand_locations"></a>
|
||||||
|
|
||||||
## expand_locations
|
## expand_locations
|
||||||
|
|
|
@ -3,8 +3,10 @@
|
||||||
load(
|
load(
|
||||||
"//lib/private:expand_make_vars.bzl",
|
"//lib/private:expand_make_vars.bzl",
|
||||||
_expand_locations = "expand_locations",
|
_expand_locations = "expand_locations",
|
||||||
|
_expand_template = "expand_template",
|
||||||
_expand_variables = "expand_variables",
|
_expand_variables = "expand_variables",
|
||||||
)
|
)
|
||||||
|
|
||||||
expand_locations = _expand_locations
|
expand_locations = _expand_locations
|
||||||
expand_variables = _expand_variables
|
expand_variables = _expand_variables
|
||||||
|
expand_template = _expand_template
|
||||||
|
|
|
@ -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])
|
additional_substitutions["RULEDIR"] = "/".join([o for o in rule_dir if o])
|
||||||
|
|
||||||
return ctx.expand_make_variables(attribute_name, s, additional_substitutions)
|
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,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
|
|
||||||
load("//lib/private:expand_make_vars.bzl", "expand_locations")
|
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 = {
|
_ATTRS = {
|
||||||
"args": attr.string_list(),
|
"args": attr.string_list(),
|
||||||
"data": attr.label_list(allow_files = True),
|
"data": attr.label_list(allow_files = True),
|
||||||
|
@ -53,5 +50,4 @@ params_file = rule(
|
||||||
implementation = _impl,
|
implementation = _impl,
|
||||||
provides = [DefaultInfo],
|
provides = [DefaultInfo],
|
||||||
attrs = _ATTRS,
|
attrs = _ATTRS,
|
||||||
doc = _DOC,
|
|
||||||
)
|
)
|
||||||
|
|
3
lib/tests/BUILD.bazel
Normal file
3
lib/tests/BUILD.bazel
Normal 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")
|
29
lib/tests/expand_make_vars_test.bzl
Normal file
29
lib/tests/expand_make_vars_test.bzl
Normal 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)
|
Loading…
Reference in a new issue