• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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")
17load("@rules_python//python:py_info.bzl", "PyInfo")
18
19def _assert_in(env, item, container):
20    asserts.true(
21        env,
22        item in container,
23        "Expected " + str(item) + " to be in " + str(container),
24    )
25
26# Tests the declared outputs of the 'py_proto_library' rule and, indirectly, also  tests that
27# these outputs are actually generated (building ":helloworld_py_pb2" will fail if not all of
28# the declared output files are actually generated).
29def _py_proto_library_provider_contents_test_impl(ctx):
30    env = analysistest.begin(ctx)
31
32    target = analysistest.target_under_test(env)
33
34    files = [file.short_path for file in target.files.to_list()]
35    runfiles = [file.short_path for file in target.default_runfiles.files.to_list()]
36    py_info_transitive_sources = [
37        file.short_path
38        for file in target[PyInfo].transitive_sources.to_list()
39    ]
40
41    _assert_in(env, "helloworld_pb2.py", files)
42    _assert_in(env, "helloworld_pb2.pyi", files)
43    _assert_in(env, "subdir/hello_dep_pb2.py", files)
44    _assert_in(env, "subdir/hello_dep_pb2.pyi", files)
45
46    _assert_in(env, "helloworld_pb2.py", runfiles)
47    _assert_in(env, "helloworld_pb2.pyi", runfiles)
48    _assert_in(env, "subdir/hello_dep_pb2.py", runfiles)
49    _assert_in(env, "subdir/hello_dep_pb2.pyi", runfiles)
50
51    _assert_in(env, "helloworld_pb2.py", py_info_transitive_sources)
52    _assert_in(env, "helloworld_pb2.pyi", py_info_transitive_sources)
53    _assert_in(env, "subdir/hello_dep_pb2.py", py_info_transitive_sources)
54    _assert_in(env, "subdir/hello_dep_pb2.pyi", py_info_transitive_sources)
55
56    return analysistest.end(env)
57
58_py_proto_library_provider_contents_test = analysistest.make(_py_proto_library_provider_contents_test_impl)
59
60def python_rules_test_suite(name):
61    _py_proto_library_provider_contents_test(
62        name = "py_proto_library_provider_contents_test",
63        target_under_test = ":helloworld_py_pb2",
64    )
65
66    native.test_suite(
67        name = name,
68        tests = [
69            "py_proto_library_provider_contents_test",
70        ],
71    )
72