| 1 |
# Copyright (c) 2009-2021, Google LLC |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# * Redistributions of source code must retain the above copyright |
| 7 |
# notice, this list of conditions and the following disclaimer. |
| 8 |
# * Redistributions in binary form must reproduce the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer in the |
| 10 |
# documentation and/or other materials provided with the distribution. |
| 11 |
# * Neither the name of Google LLC nor the |
| 12 |
# names of its contributors may be used to endorse or promote products |
| 13 |
# derived from this software without specific prior written permission. |
| 14 |
# |
| 15 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 |
# DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY |
| 19 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
|
| 26 |
"""Bazel support functions related to CMake support.""" |
| 27 |
|
| 28 |
def generated_file_staleness_test(name, outs, generated_pattern, **kwargs): |
| 29 |
"""Tests that checked-in file(s) match the contents of generated file(s). |
| 30 |
|
| 31 |
The resulting test will verify that all output files exist and have the |
| 32 |
correct contents. If the test fails, it can be invoked with --fix to |
| 33 |
bring the checked-in files up to date. |
| 34 |
|
| 35 |
Args: |
| 36 |
name: Name of the rule. |
| 37 |
outs: the checked-in files that are copied from generated files. |
| 38 |
generated_pattern: the pattern for transforming each "out" file into a |
| 39 |
generated file. For example, if generated_pattern="generated/%s" then |
| 40 |
a file foo.txt will look for generated file generated/foo.txt. |
| 41 |
**kwargs: Additional keyword arguments to pass through to py_test(). |
| 42 |
""" |
| 43 |
|
| 44 |
script_name = name + ".py" |
| 45 |
script_src = ":staleness_test.py" |
| 46 |
|
| 47 |
# Filter out non-existing rules so Blaze doesn't error out before we even |
| 48 |
# run the test. |
| 49 |
existing_outs = native.glob(include = outs) |
| 50 |
|
| 51 |
# The file list contains a few extra bits of information at the end. |
| 52 |
# These get unpacked by the Config class in staleness_test_lib.py. |
| 53 |
file_list = outs + [generated_pattern, native.package_name() or ".", name] |
| 54 |
|
| 55 |
native.genrule( |
| 56 |
name = name + "_makescript", |
| 57 |
outs = [script_name], |
| 58 |
srcs = [script_src], |
| 59 |
testonly = 1, |
| 60 |
cmd = "cat $(location " + script_src + ") > $@; " + |
| 61 |
"sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@", |
| 62 |
) |
| 63 |
|
| 64 |
native.py_test( |
| 65 |
name = name, |
| 66 |
srcs = [script_name], |
| 67 |
data = existing_outs + [generated_pattern % file for file in outs], |
| 68 |
python_version = "PY3", |
| 69 |
deps = [ |
| 70 |
":staleness_test_lib", |
| 71 |
], |
| 72 |
**kwargs |
| 73 |
) |