Joshua Haberman | 823eb09 | 2021-04-05 12:26:41 -0700 | [diff] [blame] | 1 | # Copyright (c) 2009-2021, Google LLC |
Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 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 | |
Joshua Haberman | 7ff1662 | 2022-04-01 12:07:07 -0700 | [diff] [blame] | 26 | """Bazel support functions related to CMake support.""" |
| 27 | |
Mike Kruskal | 65e7d4a | 2023-02-03 14:49:40 -0800 | [diff] [blame] | 28 | def staleness_test(name, outs, generated_pattern, target_files = None, tags = [], **kwargs): |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 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. |
Joshua Haberman | e41a2d7 | 2023-01-11 21:25:34 -0800 | [diff] [blame] | 41 | target_files: A glob representing all of the files that should be |
| 42 | covered by this rule. Files in this glob but not generated will |
| 43 | be deleted. (Not currently implemented in OSS). |
Joshua Haberman | 7ff1662 | 2022-04-01 12:07:07 -0700 | [diff] [blame] | 44 | **kwargs: Additional keyword arguments to pass through to py_test(). |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 45 | """ |
| 46 | |
| 47 | script_name = name + ".py" |
Adam Cozzette | 4a9e1ba | 2022-09-29 22:14:05 +0000 | [diff] [blame] | 48 | script_src = Label("//cmake:staleness_test.py") |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 49 | |
| 50 | # Filter out non-existing rules so Blaze doesn't error out before we even |
| 51 | # run the test. |
| 52 | existing_outs = native.glob(include = outs) |
| 53 | |
| 54 | # The file list contains a few extra bits of information at the end. |
| 55 | # These get unpacked by the Config class in staleness_test_lib.py. |
| 56 | file_list = outs + [generated_pattern, native.package_name() or ".", name] |
| 57 | |
| 58 | native.genrule( |
| 59 | name = name + "_makescript", |
| 60 | outs = [script_name], |
| 61 | srcs = [script_src], |
| 62 | testonly = 1, |
Adam Cozzette | 4a9e1ba | 2022-09-29 22:14:05 +0000 | [diff] [blame] | 63 | cmd = "cp $< $@; " + |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 64 | "sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n ".join(file_list) + "|' $@", |
| 65 | ) |
| 66 | |
| 67 | native.py_test( |
| 68 | name = name, |
| 69 | srcs = [script_name], |
| 70 | data = existing_outs + [generated_pattern % file for file in outs], |
Joshua Haberman | 7183780 | 2021-08-24 07:58:37 -0700 | [diff] [blame] | 71 | python_version = "PY3", |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 72 | deps = [ |
Adam Cozzette | 4a9e1ba | 2022-09-29 22:14:05 +0000 | [diff] [blame] | 73 | Label("//cmake:staleness_test_lib"), |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 74 | ], |
Mike Kruskal | 65e7d4a | 2023-02-03 14:49:40 -0800 | [diff] [blame] | 75 | tags = ["staleness_test"] + tags, |
Joshua Haberman | 7ff1662 | 2022-04-01 12:07:07 -0700 | [diff] [blame] | 76 | **kwargs |
Joshua Haberman | e3f41de | 2020-10-17 13:15:58 -0700 | [diff] [blame] | 77 | ) |