google-nomulus/javatests/google/registry/builddefs/zip_contents_test.bzl
jianglai 8fac22f0f3 Fix .bzl files with buildifier
This CL fixes .bzl files to make them compatible with the next versions of Bazel. This is done by running the following command on all the .bzl files:

    buildifier --lint=fix --warnings=attr-non-empty,attr-single-file,ctx-actions,output-group

More information: []

Tested:
    tap_presubmit
    Some tests failed; test failures are believed to be unrelated to this CL

BEGIN_PUBLIC
Internal cleanup
END_PUBLIC

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=235192309
2019-02-25 11:13:08 -05:00

66 lines
2.3 KiB
Python

# Copyright 2017 The Nomulus Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Build rule for unit testing the zip_file() rule."""
load("//java/google/registry/builddefs:defs.bzl", "ZIPPER")
def _impl(ctx):
"""Implementation of zip_contents_test() rule."""
cmd = [
"set -e",
'repo="$(pwd)"',
'zipper="${repo}/%s"' % ctx.file._zipper.short_path,
'archive="${repo}/%s"' % ctx.file.src.short_path,
('listing="$("${zipper}" v "${archive}"' +
' | grep -v ^d | awk \'{print $3}\' | LC_ALL=C sort)"'),
'if [[ "${listing}" != "%s" ]]; then' % (
"\n".join(ctx.attr.contents.keys())
),
' echo "archive had different file listing:"',
' "${zipper}" v "${archive}" | grep -v ^d',
" exit 1",
"fi",
'tmp="$(mktemp -d "${TMPDIR:-/tmp}/zip_contents_test.XXXXXXXXXX")"',
'cd "${tmp}"',
'"${zipper}" x "${archive}"',
]
for path, data in ctx.attr.contents.items():
cmd += [
'if [[ "$(cat "%s")" != "%s" ]]; then' % (path, data),
' echo "%s had different contents:"' % path,
' cat "%s"' % path,
" exit 1",
"fi",
]
cmd += [
'cd "${repo}"',
'rm -rf "${tmp}"',
]
ctx.actions.write(
output = ctx.outputs.executable,
content = "\n".join(cmd),
is_executable = True,
)
return struct(runfiles = ctx.runfiles([ctx.file.src, ctx.file._zipper]))
zip_contents_test = rule(
implementation = _impl,
test = True,
attrs = {
"src": attr.label(allow_single_file = True),
"contents": attr.string_dict(),
"_zipper": attr.label(default = Label(ZIPPER), allow_single_file = True),
},
)