xref: /aosp_15_r20/external/grpc-grpc/test/distrib/bazel/python/python_rules_test.bzl (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2021 The gRPC Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Bazel rule tests of bazel/python_rules.bzl"""
15
16load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
17
18def _assert_in(env, item, container):
19    asserts.true(
20        env,
21        item in container,
22        "Expected " + str(item) + " to be in " + str(container),
23    )
24
25# Tests the declared outputs of the 'py_proto_library' rule and, indirectly, also  tests that
26# these outputs are actually generated (building ":helloworld_py_pb2" will fail if not all of
27# the declared output files are actually generated).
28def _py_proto_library_provider_contents_test_impl(ctx):
29    env = analysistest.begin(ctx)
30
31    target = analysistest.target_under_test(env)
32
33    files = [file.short_path for file in target.files.to_list()]
34    runfiles = [file.short_path for file in target.default_runfiles.files.to_list()]
35    py_info_transitive_sources = [
36        file.short_path
37        for file in target[PyInfo].transitive_sources.to_list()
38    ]
39
40    _assert_in(env, "helloworld_pb2.py", files)
41    _assert_in(env, "helloworld_pb2.pyi", files)
42    _assert_in(env, "subdir/hello_dep_pb2.py", files)
43    _assert_in(env, "subdir/hello_dep_pb2.pyi", files)
44
45    _assert_in(env, "helloworld_pb2.py", runfiles)
46    _assert_in(env, "helloworld_pb2.pyi", runfiles)
47    _assert_in(env, "subdir/hello_dep_pb2.py", runfiles)
48    _assert_in(env, "subdir/hello_dep_pb2.pyi", runfiles)
49
50    _assert_in(env, "helloworld_pb2.py", py_info_transitive_sources)
51    _assert_in(env, "helloworld_pb2.pyi", py_info_transitive_sources)
52    _assert_in(env, "subdir/hello_dep_pb2.py", py_info_transitive_sources)
53    _assert_in(env, "subdir/hello_dep_pb2.pyi", py_info_transitive_sources)
54
55    return analysistest.end(env)
56
57_py_proto_library_provider_contents_test = analysistest.make(_py_proto_library_provider_contents_test_impl)
58
59def python_rules_test_suite(name):
60    _py_proto_library_provider_contents_test(
61        name = "py_proto_library_provider_contents_test",
62        target_under_test = ":helloworld_py_pb2",
63    )
64
65    native.test_suite(
66        name = name,
67        tests = [
68            "py_proto_library_provider_contents_test",
69        ],
70    )
71