• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2009-2021, Google LLC
2# All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7"""Bazel support functions related to CMake support."""
8
9load("@rules_python//python:py_test.bzl", "py_test")
10
11def staleness_test(name, outs, generated_pattern, target_files = None, tags = [], **kwargs):
12    """Tests that checked-in file(s) match the contents of generated file(s).
13
14    The resulting test will verify that all output files exist and have the
15    correct contents.  If the test fails, it can be invoked with --fix to
16    bring the checked-in files up to date.
17
18    Args:
19      name: Name of the rule.
20      outs: the checked-in files that are copied from generated files.
21      generated_pattern: the pattern for transforming each "out" file into a
22        generated file.  For example, if generated_pattern="generated/%s" then
23        a file foo.txt will look for generated file generated/foo.txt.
24      target_files: A glob representing all of the files that should be
25      covered by this rule.  Files in this glob but not generated will
26      be deleted.  (Not currently implemented in OSS).
27      **kwargs: Additional keyword arguments to pass through to py_test().
28    """
29
30    script_name = name + ".py"
31    script_src = Label("//upb/cmake:staleness_test.py")
32
33    # Filter out non-existing rules so Blaze doesn't error out before we even
34    # run the test.
35    existing_outs = native.glob(include = outs, allow_empty = True)
36
37    # The file list contains a few extra bits of information at the end.
38    # These get unpacked by the Config class in staleness_test_lib.py.
39    file_list = outs + [generated_pattern, native.package_name() or ".", name]
40
41    native.genrule(
42        name = name + "_makescript",
43        outs = [script_name],
44        srcs = [script_src],
45        testonly = 1,
46        cmd = "cp $< $@; " +
47              "sed -i.bak -e 's|INSERT_FILE_LIST_HERE|" + "\\\n  ".join(file_list) + "|' $@",
48    )
49
50    py_test(
51        name = name,
52        srcs = [script_name],
53        data = existing_outs + [generated_pattern % file for file in outs],
54        python_version = "PY3",
55        deps = [
56            Label("//upb/cmake:staleness_test_lib"),
57        ],
58        tags = ["staleness_test"] + tags,
59        **kwargs
60    )
61