File: proto_common_declare_generated_files_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 (95 lines) | stat: -rw-r--r-- 2,952 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
# Protocol Buffers - Google's data interchange format
# Copyright 2025 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.declare_generated_files` function."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:util.bzl", "util")
load("//bazel:proto_library.bzl", "proto_library")
load("//bazel/common:proto_common.bzl", "proto_common")
load("//bazel/common:proto_info.bzl", "ProtoInfo")

def proto_common_declare_generated_files_test_suite(name):
    test_suite(
        name = name,
        tests = [
            _test_declare_generated_files_basic,
            _test_declare_generated_files_python,
        ],
    )

def _impl(ctx):
    files = proto_common.declare_generated_files(
        ctx.actions,
        ctx.attr.proto_dep[ProtoInfo],
        ctx.attr.extension,
        (lambda s: s.replace("-", "_").replace(".", "/")) if ctx.attr.python_names else None,
    )
    for f in files:
        ctx.actions.write(f, "")
    return [DefaultInfo(files = depset(files))]

_declare_generated_files = rule(
    _impl,
    attrs = {
        "proto_dep": attr.label(),
        "extension": attr.string(),
        "python_names": attr.bool(default = False),
    },
)

# Verifies `proto_common.declare_generated_files` call.
def _test_declare_generated_files_basic(name):
    util.helper_target(
        proto_library,
        name = name + "_proto",
        srcs = ["A.proto", "b/B.proto"],
    )
    util.helper_target(
        _declare_generated_files,
        name = name + "_declare_generated_files",
        proto_dep = name + "_proto",
        extension = ".cc",
    )

    analysis_test(
        name = name,
        target = name + "_declare_generated_files",
        impl = _test_declare_generated_files_basic_impl,
    )

def _test_declare_generated_files_basic_impl(env, target):
    env.expect.that_target(target).default_outputs().contains_exactly([
        "{package}/A.cc",
        "{package}/b/B.cc",
    ])

# Verifies `proto_common.declare_generated_files` for Python.
def _test_declare_generated_files_python(name):
    util.helper_target(
        proto_library,
        name = name + "_proto",
        srcs = ["my-proto.gen.proto"],
    )
    util.helper_target(
        _declare_generated_files,
        name = name + "_declare_generated_files_python",
        proto_dep = name + "_proto",
        extension = "_pb2.py",
        python_names = True,
    )

    analysis_test(
        name = name,
        target = name + "_declare_generated_files_python",
        impl = _test_declare_generated_files_python_impl,
    )

def _test_declare_generated_files_python_impl(env, target):
    env.expect.that_target(target).default_outputs().contains_exactly([
        "{package}/my_proto/gen_pb2.py",
    ])