github.com/bazelbuild/rules_go@v0.47.2-0.20240515105122-e7ddb9ea474e/go/private/tools/single_output_test.bzl (about)

     1  # Copyright 2017 The Bazel Go Rules Authors. All rights reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #    http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  def _impl(ctx):
    16      exe = ctx.outputs.out
    17      ctx.actions.write(
    18          output = exe,
    19          # The file must not be empty because running an empty .bat file as a
    20          # subprocess fails on Windows, so we write one space to it.
    21          content = " ",
    22          is_executable = True,
    23      )
    24      return [DefaultInfo(files = depset([exe]), executable = exe)]
    25  
    26  _single_output_test = rule(
    27      implementation = _impl,
    28      attrs = {
    29          "dep": attr.label(allow_single_file = True),
    30          "out": attr.output(),
    31      },
    32      test = True,
    33  )
    34  
    35  def single_output_test(name, dep, **kwargs):
    36      """Checks that a dependency produces a single output file.
    37  
    38      This test works by setting `allow_single_file = True` on the `dep` attribute.
    39      If `dep` provides zero or multiple files in its `files` provider, Bazel will
    40      fail to build this rule during analysis. The actual test does nothing.
    41  
    42      Args:
    43        name: The name of the test rule.
    44        dep: a label for the rule to check.
    45        **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
    46      """
    47  
    48      _single_output_test(
    49          name = name,
    50          dep = dep,
    51          # On Windows we need the ".bat" extension.
    52          # On other platforms the extension doesn't matter.
    53          # Therefore we can use ".bat" on every platform.
    54          out = name + ".bat",
    55          **kwargs
    56      )