aboutsummaryrefslogtreecommitdiff
path: root/tools/build_defs/python/tests/py_library/py_library_tests.bzl
blob: 1fcb0c19b9c71162d228445f2e2a0a0534857989 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Test for py_library."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:defs.bzl", "PyRuntimeInfo", "py_library")
load("//tools/build_defs/python/tests:base_tests.bzl", "create_base_tests")
load("//tools/build_defs/python/tests:util.bzl", pt_util = "util")

_tests = []

def _test_py_runtime_info_not_present(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["lib.py"],
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_py_runtime_info_not_present_impl,
    )

def _test_py_runtime_info_not_present_impl(env, target):
    env.expect.that_bool(PyRuntimeInfo in target).equals(False)

_tests.append(_test_py_runtime_info_not_present)

def _test_files_to_build(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["lib.py"],
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_files_to_build_impl,
    )

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

_tests.append(_test_files_to_build)

def _test_srcs_can_contain_rule_generating_py_and_nonpy_files(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["lib.py", name + "_gensrcs"],
    )
    rt_util.helper_target(
        native.genrule,
        name = name + "_gensrcs",
        cmd = "touch $(OUTS)",
        outs = [name + "_gen.py", name + "_gen.cc"],
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_srcs_can_contain_rule_generating_py_and_nonpy_files_impl,
    )

def _test_srcs_can_contain_rule_generating_py_and_nonpy_files_impl(env, target):
    env.expect.that_target(target).default_outputs().contains_exactly([
        "{package}/{test_name}_gen.py",
        "{package}/lib.py",
    ])

_tests.append(_test_srcs_can_contain_rule_generating_py_and_nonpy_files)

def _test_srcs_generating_no_py_files_is_error(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = [name + "_gen"],
    )
    rt_util.helper_target(
        native.genrule,
        name = name + "_gen",
        cmd = "touch $(OUTS)",
        outs = [name + "_gen.cc"],
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_srcs_generating_no_py_files_is_error_impl,
        expect_failure = True,
    )

def _test_srcs_generating_no_py_files_is_error_impl(env, target):
    env.expect.that_target(target).failures().contains_predicate(
        matching.str_matches("does not produce*srcs files"),
    )

_tests.append(_test_srcs_generating_no_py_files_is_error)

def _test_files_to_compile(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["lib1.py"],
        deps = [name + "_lib2"],
    )
    rt_util.helper_target(
        config.rule,
        name = name + "_lib2",
        srcs = ["lib2.py"],
        deps = [name + "_lib3"],
    )
    rt_util.helper_target(
        config.rule,
        name = name + "_lib3",
        srcs = ["lib3.py"],
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_files_to_compile_impl,
    )

def _test_files_to_compile_impl(env, target):
    target = env.expect.that_target(target)
    target.output_group(
        "compilation_prerequisites_INTERNAL_",
    ).contains_exactly([
        "{package}/lib1.py",
        "{package}/lib2.py",
        "{package}/lib3.py",
    ])
    target.output_group(
        "compilation_outputs",
    ).contains_exactly([
        "{package}/lib1.py",
        "{package}/lib2.py",
        "{package}/lib3.py",
    ])

_tests.append(_test_files_to_compile)

def py_library_test_suite(name):
    config = struct(rule = py_library, base_test_rule = py_library)
    native.test_suite(
        name = name,
        tests = pt_util.create_tests(_tests, config = config) + create_base_tests(config),
    )