diff --git a/test/BUILD b/test/BUILD index 2a68496ee..98ccc5fce 100644 --- a/test/BUILD +++ b/test/BUILD @@ -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"]) @@ -47,3 +48,9 @@ bzl_library( "//test/rules:starlark_tests_bzls", ], ) + +bzl_test( + name = "swift_bzl_test", + src = "swift_bzl_macro.bzl", + deps = ["//swift"], +) diff --git a/test/bzl_test.bzl b/test/bzl_test.bzl new file mode 100644 index 000000000..42dc4da6b --- /dev/null +++ b/test/bzl_test.bzl @@ -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], + ) diff --git a/test/swift_bzl_macro.bzl b/test/swift_bzl_macro.bzl new file mode 100644 index 000000000..77cb11abb --- /dev/null +++ b/test/swift_bzl_macro.bzl @@ -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