1*9bb1b549SSpandan Das# Copyright 2020 The Bazel Authors. All rights reserved. 2*9bb1b549SSpandan Das# 3*9bb1b549SSpandan Das# Licensed under the Apache License, Version 2.0 (the "License"); 4*9bb1b549SSpandan Das# you may not use this file except in compliance with the License. 5*9bb1b549SSpandan Das# You may obtain a copy of the License at 6*9bb1b549SSpandan Das# 7*9bb1b549SSpandan Das# http://www.apache.org/licenses/LICENSE-2.0 8*9bb1b549SSpandan Das# 9*9bb1b549SSpandan Das# Unless required by applicable law or agreed to in writing, software 10*9bb1b549SSpandan Das# distributed under the License is distributed on an "AS IS" BASIS, 11*9bb1b549SSpandan Das# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9bb1b549SSpandan Das# See the License for the specific language governing permissions and 13*9bb1b549SSpandan Das# limitations under the License. 14*9bb1b549SSpandan Das 15*9bb1b549SSpandan Das"""buildifier_tests.bzl provides the buildifier_test rule""" 16*9bb1b549SSpandan Das 17*9bb1b549SSpandan Dasload("@bazel_skylib//lib:shell.bzl", "shell") 18*9bb1b549SSpandan Das 19*9bb1b549SSpandan Dasdef _check_file(f): 20*9bb1b549SSpandan Das base = f.basename 21*9bb1b549SSpandan Das return base in ("WORKSPACE", "BUILD", "BUILD.bazel") or base.endswith(".bzl") 22*9bb1b549SSpandan Das 23*9bb1b549SSpandan Dasdef _buildifier_test_impl(ctx): 24*9bb1b549SSpandan Das files = [f for f in ctx.files.data if _check_file(f)] 25*9bb1b549SSpandan Das 26*9bb1b549SSpandan Das script = ctx.actions.declare_file(ctx.label.name + ".bash") 27*9bb1b549SSpandan Das content = """#!/usr/bin/env bash 28*9bb1b549SSpandan Das 29*9bb1b549SSpandan Dasset -uo pipefail 30*9bb1b549SSpandan Das 31*9bb1b549SSpandan Dasfiles=( 32*9bb1b549SSpandan Das {files} 33*9bb1b549SSpandan Das) 34*9bb1b549SSpandan Dasbuildifier={buildifier} 35*9bb1b549SSpandan Das 36*9bb1b549SSpandan Das# warnings is the default list of warnings with exclusions: 37*9bb1b549SSpandan Das# bzl-visibility: we reference symbols in //go/private outside of //go. 38*9bb1b549SSpandan Das# confusing-name: a good font makes these very clear. 39*9bb1b549SSpandan Das# function-docstring: too verbose. Many functions don't need docs. 40*9bb1b549SSpandan Das# function-docstring-header: too verbose for now. 41*9bb1b549SSpandan Das# function-docstring-args: too verbose. 42*9bb1b549SSpandan Das# function-docstring-return: too verbose. 43*9bb1b549SSpandan Das# module-docstring: doesn't seem useful for many private modules. 44*9bb1b549SSpandan Das# name-conventions: we have non-compliant providers. We might change them 45*9bb1b549SSpandan Das# eventually, but we'll need to keep the old symbols for compatibility. 46*9bb1b549SSpandan Das# print: used for warnings. 47*9bb1b549SSpandan Daswarnings=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*9bb1b549SSpandan Das 49*9bb1b549SSpandan Dasok=0 50*9bb1b549SSpandan Dasfor file in "${{files[@]}}"; do 51*9bb1b549SSpandan Das "$buildifier" -mode=check -lint=warn -warnings="$warnings" "$file" 52*9bb1b549SSpandan Das if [ $? -ne 0 ]; then 53*9bb1b549SSpandan Das ok=1 54*9bb1b549SSpandan Das fi 55*9bb1b549SSpandan Dasdone 56*9bb1b549SSpandan Dasexit $ok 57*9bb1b549SSpandan Das""".format( 58*9bb1b549SSpandan Das buildifier = shell.quote(ctx.executable._buildifier.short_path), 59*9bb1b549SSpandan Das files = "\n".join([shell.quote(f.path) for f in files]), 60*9bb1b549SSpandan Das ) 61*9bb1b549SSpandan Das ctx.actions.write(script, content, is_executable = True) 62*9bb1b549SSpandan Das 63*9bb1b549SSpandan Das return [DefaultInfo( 64*9bb1b549SSpandan Das executable = script, 65*9bb1b549SSpandan Das default_runfiles = ctx.runfiles( 66*9bb1b549SSpandan Das files = [script, ctx.executable._buildifier] + files, 67*9bb1b549SSpandan Das ), 68*9bb1b549SSpandan Das )] 69*9bb1b549SSpandan Das 70*9bb1b549SSpandan Dasbuildifier_test = rule( 71*9bb1b549SSpandan Das implementation = _buildifier_test_impl, 72*9bb1b549SSpandan Das attrs = { 73*9bb1b549SSpandan Das "data": attr.label_list( 74*9bb1b549SSpandan Das allow_files = True, 75*9bb1b549SSpandan Das ), 76*9bb1b549SSpandan Das "_buildifier": attr.label( 77*9bb1b549SSpandan Das default = "@com_github_bazelbuild_buildtools//buildifier", 78*9bb1b549SSpandan Das executable = True, 79*9bb1b549SSpandan Das cfg = "exec", 80*9bb1b549SSpandan Das ), 81*9bb1b549SSpandan Das }, 82*9bb1b549SSpandan Das test = True, 83*9bb1b549SSpandan Das) 84