xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/bazel/deploy_jar_freshness_golden_test.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1# Copyright 2022 Google LLC. 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"""Test on *_deploy.jar freshness"""
16
17load("//:visibility.bzl", "RULES_KOTLIN")
18
19visibility(RULES_KOTLIN)
20
21def _deploy_jar_freshness_golden_test_impl(ctx):
22    test_command = """
23      if ! cmp $1 $2 ; then
24          echo "$1 needs to be rebuilt"
25          echo "exit 1" > $3
26          exit 0
27      fi
28
29      # Always passes
30      echo "#!/bin/bash" > $3
31    """
32
33    dummy_test_script = ctx.actions.declare_file(ctx.label.name + ".sh")
34    ctx.actions.run_shell(
35        inputs = [ctx.file.current_jar, ctx.file.newly_built_jar],
36        outputs = [dummy_test_script],
37        arguments = [
38            ctx.file.current_jar.path,
39            ctx.file.newly_built_jar.path,
40            dummy_test_script.path,
41        ],
42        command = test_command,
43    )
44
45    return [DefaultInfo(executable = dummy_test_script)]
46
47deploy_jar_freshness_golden_test = rule(
48    implementation = _deploy_jar_freshness_golden_test_impl,
49    attrs = dict(
50        newly_built_jar = attr.label(
51            doc = "Newly built target deploy.jar",
52            mandatory = True,
53            allow_single_file = [".jar"],
54        ),
55        current_jar = attr.label(
56            doc = "Prebuilt jar to verify",
57            allow_single_file = [".jar"],
58            mandatory = True,
59        ),
60    ),
61    test = True,
62)
63