test: add test coverage for docs rules

This commit is contained in:
Alex Eagle 2022-04-27 17:22:30 -07:00
parent 8138d7f6c3
commit 626af72fe8
9 changed files with 41 additions and 25 deletions

View File

@ -1,4 +1,5 @@
docs/*.md docs/*.md
e2e/*/docs.md
lib/tests/jq/*.json lib/tests/jq/*.json
lib/tests/yq/empty.yaml lib/tests/yq/empty.yaml
lib/lib/tests/write_source_files/*.js lib/lib/tests/write_source_files/*.js

View File

@ -12,6 +12,7 @@ module(
bazel_dep(name = "bazel_skylib", version = "1.1.1") bazel_dep(name = "bazel_skylib", version = "1.1.1")
bazel_dep(name = "platforms", version = "0.0.4") bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "stardoc", version = "0.5.0", repo_name = "io_bazel_stardoc")
ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "ext") ext = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "ext")
use_repo(ext, "jq_toolchains") use_repo(ext, "jq_toolchains")

9
docs/docs.md generated
View File

@ -7,8 +7,7 @@ Public API for docs helpers
## stardoc_with_diff_test ## stardoc_with_diff_test
<pre> <pre>
stardoc_with_diff_test(<a href="#stardoc_with_diff_test-name">name</a>, <a href="#stardoc_with_diff_test-bzl_library_target">bzl_library_target</a>, <a href="#stardoc_with_diff_test-aspect_template">aspect_template</a>, <a href="#stardoc_with_diff_test-func_template">func_template</a>, <a href="#stardoc_with_diff_test-header_template">header_template</a>, stardoc_with_diff_test(<a href="#stardoc_with_diff_test-name">name</a>, <a href="#stardoc_with_diff_test-bzl_library_target">bzl_library_target</a>, <a href="#stardoc_with_diff_test-suggested_update_target">suggested_update_target</a>, <a href="#stardoc_with_diff_test-kwargs">kwargs</a>)
<a href="#stardoc_with_diff_test-provider_template">provider_template</a>, <a href="#stardoc_with_diff_test-rule_template">rule_template</a>, <a href="#stardoc_with_diff_test-suggested_update_target">suggested_update_target</a>)
</pre> </pre>
Creates a stardoc target, diff test, and an executable to rule to write the generated doc to the source tree and test that it's up to date. Creates a stardoc target, diff test, and an executable to rule to write the generated doc to the source tree and test that it's up to date.
@ -23,12 +22,8 @@ This is helpful for minimizing boilerplate in repos wih lots of stardoc targets.
| :------------- | :------------- | :------------- | | :------------- | :------------- | :------------- |
| <a id="stardoc_with_diff_test-name"></a>name | the name of the stardoc file to be written to the current source directory (.md will be appended to the name). Call bazel run on this target to update the file. | none | | <a id="stardoc_with_diff_test-name"></a>name | the name of the stardoc file to be written to the current source directory (.md will be appended to the name). Call bazel run on this target to update the file. | none |
| <a id="stardoc_with_diff_test-bzl_library_target"></a>bzl_library_target | the label of the <code>bzl_library</code> target to generate documentation for | none | | <a id="stardoc_with_diff_test-bzl_library_target"></a>bzl_library_target | the label of the <code>bzl_library</code> target to generate documentation for | none |
| <a id="stardoc_with_diff_test-aspect_template"></a>aspect_template | the label or path to the Velocity aspect template to use with stardoc | <code>"@io_bazel_stardoc//stardoc:templates/markdown_tables/aspect.vm"</code> |
| <a id="stardoc_with_diff_test-func_template"></a>func_template | the label or path to the Velocity function/macro template to use with stardoc | <code>"@io_bazel_stardoc//stardoc:templates/markdown_tables/func.vm"</code> |
| <a id="stardoc_with_diff_test-header_template"></a>header_template | the label or path to the Velocity header template to use with stardoc | <code>"@io_bazel_stardoc//stardoc:templates/markdown_tables/header.vm"</code> |
| <a id="stardoc_with_diff_test-provider_template"></a>provider_template | the label or path to the Velocity provider template to use with stardoc | <code>"@io_bazel_stardoc//stardoc:templates/markdown_tables/provider.vm"</code> |
| <a id="stardoc_with_diff_test-rule_template"></a>rule_template | the label or path to the Velocity rule template to use with stardoc | <code>"@io_bazel_stardoc//stardoc:templates/markdown_tables/rule.vm"</code> |
| <a id="stardoc_with_diff_test-suggested_update_target"></a>suggested_update_target | the target suggested to be run when a doc is out of date (should be the label for [update_docs](#update_docs)) | <code>"//docs:update"</code> | | <a id="stardoc_with_diff_test-suggested_update_target"></a>suggested_update_target | the target suggested to be run when a doc is out of date (should be the label for [update_docs](#update_docs)) | <code>"//docs:update"</code> |
| <a id="stardoc_with_diff_test-kwargs"></a>kwargs | additional attributes passed to the stardoc() rule, such as for overriding the templates | none |
<a id="#update_docs"></a> <a id="#update_docs"></a>

View File

@ -1,2 +1 @@
common --experimental_enable_bzlmod common --experimental_enable_bzlmod
common --registry=https://raw.githubusercontent.com/aspect-build/bazel-central-registry/main/

View File

@ -1,7 +1,32 @@
"""Integration testing that aspect_bazel_lib works when used via bzlmod.
NB: We don't use yq, so we can confirm that Bazel never fetches it.
You can manually verify this after testing this repo with
`ls $(bazel info output_base)/external | grep yq`
You'll see a aspect_bazel_lib.ext.yq_toolchains repo, but no downloaded yq binary.
"""
load("@aspect_bazel_lib//lib:jq.bzl", "jq") load("@aspect_bazel_lib//lib:jq.bzl", "jq")
load("@aspect_bazel_lib//lib:docs.bzl", "stardoc_with_diff_test")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test") load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
# No sources produces null (equivalent to --null-input) bzl_library(
name = "defs",
srcs = ["defs.bzl"],
)
# Validate that stardoc dependency works.
# Note, stardoc is generally broken under bzlmod, see
# https://github.com/bazelbuild/stardoc/issues/117
# This happens to work because we don't reference any external repos
# from defs.bzl.
stardoc_with_diff_test(
name = "docs",
bzl_library_target = "//:defs",
)
# Validate that JQ works and resolves its toolchain
jq( jq(
name = "case_no_sources", name = "case_no_sources",
srcs = [], srcs = [],

View File

@ -0,0 +1,2 @@
# Marker file that this folder is the root of a Bazel workspace.
# See MODULE.bazel for dependencies and setup.

1
e2e/bzlmod/defs.bzl Normal file
View File

@ -0,0 +1 @@
# no rules

4
e2e/bzlmod/docs.md Normal file
View File

@ -0,0 +1,4 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->

View File

@ -6,12 +6,8 @@ load("//lib:write_source_files.bzl", "write_source_files")
def stardoc_with_diff_test( def stardoc_with_diff_test(
name, name,
bzl_library_target, bzl_library_target,
aspect_template = "@io_bazel_stardoc//stardoc:templates/markdown_tables/aspect.vm", suggested_update_target = "//docs:update",
func_template = "@io_bazel_stardoc//stardoc:templates/markdown_tables/func.vm", **kwargs):
header_template = "@io_bazel_stardoc//stardoc:templates/markdown_tables/header.vm",
provider_template = "@io_bazel_stardoc//stardoc:templates/markdown_tables/provider.vm",
rule_template = "@io_bazel_stardoc//stardoc:templates/markdown_tables/rule.vm",
suggested_update_target = "//docs:update"):
"""Creates a stardoc target, diff test, and an executable to rule to write the generated doc to the source tree and test that it's up to date. """Creates a stardoc target, diff test, and an executable to rule to write the generated doc to the source tree and test that it's up to date.
This is helpful for minimizing boilerplate in repos wih lots of stardoc targets. This is helpful for minimizing boilerplate in repos wih lots of stardoc targets.
@ -19,12 +15,8 @@ def stardoc_with_diff_test(
Args: Args:
name: the name of the stardoc file to be written to the current source directory (.md will be appended to the name). Call bazel run on this target to update the file. name: the name of the stardoc file to be written to the current source directory (.md will be appended to the name). Call bazel run on this target to update the file.
bzl_library_target: the label of the `bzl_library` target to generate documentation for bzl_library_target: the label of the `bzl_library` target to generate documentation for
aspect_template: the label or path to the Velocity aspect template to use with stardoc
func_template: the label or path to the Velocity function/macro template to use with stardoc
header_template: the label or path to the Velocity header template to use with stardoc
provider_template: the label or path to the Velocity provider template to use with stardoc
rule_template: the label or path to the Velocity rule template to use with stardoc
suggested_update_target: the target suggested to be run when a doc is out of date (should be the label for [update_docs](#update_docs)) suggested_update_target: the target suggested to be run when a doc is out of date (should be the label for [update_docs](#update_docs))
**kwargs: additional attributes passed to the stardoc() rule, such as for overriding the templates
""" """
stardoc_label = name + "-docgen" stardoc_label = name + "-docgen"
@ -36,12 +28,8 @@ def stardoc_with_diff_test(
out = name + "-docgen.md", out = name + "-docgen.md",
input = bzl_library_target + ".bzl", input = bzl_library_target + ".bzl",
deps = [bzl_library_target], deps = [bzl_library_target],
aspect_template = aspect_template,
func_template = func_template,
header_template = header_template,
provider_template = provider_template,
rule_template = rule_template,
tags = ["package:" + native.package_name()], # Tag the package name which will help us reconstruct the write_source_files label in update_docs tags = ["package:" + native.package_name()], # Tag the package name which will help us reconstruct the write_source_files label in update_docs
**kwargs
) )
write_source_files( write_source_files(