File: proto_common_should_generate_tests.bzl

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (110 lines) | stat: -rw-r--r-- 3,556 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Protocol Buffers - Google's data interchange format
# Copyright 2024 Google Inc.  All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
#
"""Tests for `proto_common.compile` function."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching", "subjects", "truth")
load("@rules_testing//lib:util.bzl", "util")
load("//bazel:proto_library.bzl", "proto_library")
load("//bazel/tests/testdata:should_generate.bzl", "BoolInfo", "should_generate_rule")

def proto_common_should_generate_test_suite(name):
    util.helper_target(
        proto_library,
        name = "test_proto",
        srcs = ["A.proto"],
    )
    test_suite(
        name = name,
        tests = [
            _test_should_generate_basic,
            _test_should_generate_dont_generate,
            _test_should_generate_mixed,
        ],
    )

# Verifies `proto_common.should_generate_code` call.
def _test_should_generate_basic(name):
    util.helper_target(
        should_generate_rule,
        name = name + "_should_generate",
        proto_dep = ":test_proto",
    )

    analysis_test(
        name = name,
        target = name + "_should_generate",
        impl = _test_should_generate_basic_impl,
    )

def _test_should_generate_basic_impl(env, target):
    bool_info.from_target(env, target).value().equals(True)

# Verifies `proto_common.should_generate_code` call.
def _test_should_generate_dont_generate(name):
    util.helper_target(
        should_generate_rule,
        name = name + "_should_generate",
        proto_dep = "//bazel/tests/testdata:denied",
    )

    analysis_test(
        name = name,
        target = name + "_should_generate",
        impl = _test_should_generate_dont_generate_impl,
    )

def _test_should_generate_dont_generate_impl(env, target):
    bool_info.from_target(env, target).value().equals(False)

# Verifies `proto_common.should_generate_code` call.
def _test_should_generate_mixed(name):
    util.helper_target(
        should_generate_rule,
        name = name + "_should_generate",
        proto_dep = "//bazel/tests/testdata:mixed",
    )

    analysis_test(
        name = name,
        target = name + "_should_generate",
        impl = _test_should_generate_mixed_impl,
        expect_failure = True,
    )

def _test_should_generate_mixed_impl(env, target):
    failures = env.expect.that_target(target).failures()
    failures.contains_predicate(
        matching.str_matches(
            "The 'srcs' attribute of '*:mixed' contains protos for which 'MyRule' " +
            "shouldn't generate code (*/descriptor.proto, */metadata.proto)," +
            " in addition to protos for which it should (*/something.proto).\n" +
            "Separate '*:mixed' into 2 proto_library rules.",
        ),
    )

# Utility functions

def _new_bool_info_subject(bool_info, meta):
    self = struct(actual = bool_info, meta = meta.derive("BoolInfo"))
    public = struct(
        value = lambda: subjects.bool(getattr(bool_info, "value", False), self.meta.derive("value")),
    )
    return public

def _bool_info_from_target(env, target):
    return _new_bool_info_subject(target[BoolInfo], meta = truth.expect(env).meta.derive(
        format_str_kwargs = {
            "name": target.label.name,
            "package": target.label.package,
        },
    ))

bool_info = struct(
    from_target = _bool_info_from_target,
)