Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for missing bzl_library declarations #795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load(":private_deps_tests.bzl", "private_deps_test_suite")
load(":swift_through_non_swift_tests.bzl", "swift_through_non_swift_test_suite")
load(":split_derived_files_tests.bzl", "split_derived_files_test_suite")
load(":pch_output_dir_tests.bzl", "pch_output_dir_test_suite")
load(":bzl_test.bzl", "bzl_test")

licenses(["notice"])

Expand Down Expand Up @@ -47,3 +48,9 @@ bzl_library(
"//test/rules:starlark_tests_bzls",
],
)

bzl_test(
name = "swift_bzl_test",
src = "swift_bzl_macro.bzl",
deps = ["//swift"],
)
42 changes: 42 additions & 0 deletions test/bzl_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Macro for Ensuring Starlark Dependencies are Specified Properly"""

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")

def bzl_test(name, src, deps):
"""Provides build-time assurances that `bzl_library` declarations exist and are referenced properly.

This macro relies upon Stardoc's ability to traverse `bzl_library`
dependencies. If a Starlark dependency is loaded, but not specified as a
dependency, the Stardoc utility will fail with a reasonably helpful error
message. Interestingly, the Stardoc utility does not apply the same rigor
to files that are directly specifed to it.

Another point worth metioning is that the `src` file cannot be generated
for this macro to work. If one tries to use a generated file, the `input`
for the `stardoc` rule will resolve to the label for the generated file
which will cause the Stardoc utility to not find the file. Specifying the
input in different ways (i.e. filename vs target name) did not seem to
affect this behavior.

Args:
name: The name of the build target.
src: A non-generated Starlark file that loads the `bzl_library` that is
being checked.
deps: A `list` of deps for the Starlark file.

Returns:
"""
macro_lib_name = name + "_macro_lib"
bzl_library(
name = macro_lib_name,
srcs = [src],
deps = deps,
)

stardoc(
name = name,
out = macro_lib_name + ".md_",
input = src,
deps = [macro_lib_name],
)
16 changes: 16 additions & 0 deletions test/swift_bzl_macro.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Macro used with bzl_test

For more information, please see `bzl_test.bzl`.
"""

load("//swift:swift.bzl", "SwiftInfo")

def macro_with_doc(name):
"""This macro does nothing.

Args:
name: A `string` value.
"""
if name == None:
return None
return SwiftInfo