• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
28def staleness_test(name, outs, generated_pattern, target_files = None, tags = [], **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      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).
44      **kwargs: Additional keyword arguments to pass through to py_test().
45    """
46
47    script_name = name + ".py"
48    script_src = Label("//cmake:staleness_test.py")
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,
63        cmd = "cp $< $@; " +
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],
71        python_version = "PY3",
72        deps = [
73            Label("//cmake:staleness_test_lib"),
74        ],
75        tags = ["staleness_test"] + tags,
76        **kwargs
77    )
78