xref: /aosp_15_r20/external/bazelbuild-rules_go/tests/buildifier_test.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2020 The Bazel 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"""buildifier_tests.bzl provides the buildifier_test rule"""
16
17load("@bazel_skylib//lib:shell.bzl", "shell")
18
19def _check_file(f):
20    base = f.basename
21    return base in ("WORKSPACE", "BUILD", "BUILD.bazel") or base.endswith(".bzl")
22
23def _buildifier_test_impl(ctx):
24    files = [f for f in ctx.files.data if _check_file(f)]
25
26    script = ctx.actions.declare_file(ctx.label.name + ".bash")
27    content = """#!/usr/bin/env bash
28
29set -uo pipefail
30
31files=(
32    {files}
33)
34buildifier={buildifier}
35
36# warnings is the default list of warnings with exclusions:
37#   bzl-visibility: we reference symbols in //go/private outside of //go.
38#   confusing-name: a good font makes these very clear.
39#   function-docstring: too verbose. Many functions don't need docs.
40#   function-docstring-header: too verbose for now.
41#   function-docstring-args: too verbose.
42#   function-docstring-return: too verbose.
43#   module-docstring: doesn't seem useful for many private modules.
44#   name-conventions: we have non-compliant providers. We might change them
45#       eventually, but we'll need to keep the old symbols for compatibility.
46#   print: used for warnings.
47warnings=attr-cfg,attr-license,attr-non-empty,attr-output-default,attr-single-file,build-args-kwargs,constant-glob,ctx-actions,ctx-args,depset-iteration,depset-union,dict-concatenation,duplicated-name,filetype,git-repository,http-archive,integer-division,keyword-positional-params,load,load-on-top,native-android,native-build,native-cc,native-java,native-package,native-proto,native-py,no-effect,output-group,overly-nested-depset,package-name,package-on-top,positional-args,redefined-variable,repository-name,return-value,rule-impl-return,same-origin-load,string-iteration,uninitialized,unreachable,unused-variable
48
49ok=0
50for file in "${{files[@]}}"; do
51    "$buildifier" -mode=check -lint=warn -warnings="$warnings" "$file"
52    if [ $? -ne 0 ]; then
53        ok=1
54    fi
55done
56exit $ok
57""".format(
58        buildifier = shell.quote(ctx.executable._buildifier.short_path),
59        files = "\n".join([shell.quote(f.path) for f in files]),
60    )
61    ctx.actions.write(script, content, is_executable = True)
62
63    return [DefaultInfo(
64        executable = script,
65        default_runfiles = ctx.runfiles(
66            files = [script, ctx.executable._buildifier] + files,
67        ),
68    )]
69
70buildifier_test = rule(
71    implementation = _buildifier_test_impl,
72    attrs = {
73        "data": attr.label_list(
74            allow_files = True,
75        ),
76        "_buildifier": attr.label(
77            default = "@com_github_bazelbuild_buildtools//buildifier",
78            executable = True,
79            cfg = "exec",
80        ),
81    },
82    test = True,
83)
84