1"""Test for py_library.""" 2 3load("@rules_testing//lib:analysis_test.bzl", "analysis_test") 4load("@rules_testing//lib:truth.bzl", "matching") 5load("@rules_testing//lib:util.bzl", rt_util = "util") 6load("//python:defs.bzl", "PyRuntimeInfo", "py_library") 7load("//tests/base_rules:base_tests.bzl", "create_base_tests") 8load("//tests/base_rules:util.bzl", pt_util = "util") 9 10_tests = [] 11 12def _test_py_runtime_info_not_present(name, config): 13 rt_util.helper_target( 14 config.rule, 15 name = name + "_subject", 16 srcs = ["lib.py"], 17 ) 18 analysis_test( 19 name = name, 20 target = name + "_subject", 21 impl = _test_py_runtime_info_not_present_impl, 22 ) 23 24def _test_py_runtime_info_not_present_impl(env, target): 25 env.expect.that_bool(PyRuntimeInfo in target).equals(False) 26 27_tests.append(_test_py_runtime_info_not_present) 28 29def _test_files_to_build(name, config): 30 rt_util.helper_target( 31 config.rule, 32 name = name + "_subject", 33 srcs = ["lib.py"], 34 ) 35 analysis_test( 36 name = name, 37 target = name + "_subject", 38 impl = _test_files_to_build_impl, 39 ) 40 41def _test_files_to_build_impl(env, target): 42 env.expect.that_target(target).default_outputs().contains_exactly([ 43 "{package}/lib.py", 44 ]) 45 46_tests.append(_test_files_to_build) 47 48def _test_srcs_can_contain_rule_generating_py_and_nonpy_files(name, config): 49 rt_util.helper_target( 50 config.rule, 51 name = name + "_subject", 52 srcs = ["lib.py", name + "_gensrcs"], 53 ) 54 rt_util.helper_target( 55 native.genrule, 56 name = name + "_gensrcs", 57 cmd = "touch $(OUTS)", 58 outs = [name + "_gen.py", name + "_gen.cc"], 59 ) 60 analysis_test( 61 name = name, 62 target = name + "_subject", 63 impl = _test_srcs_can_contain_rule_generating_py_and_nonpy_files_impl, 64 ) 65 66def _test_srcs_can_contain_rule_generating_py_and_nonpy_files_impl(env, target): 67 env.expect.that_target(target).default_outputs().contains_exactly([ 68 "{package}/{test_name}_gen.py", 69 "{package}/lib.py", 70 ]) 71 72_tests.append(_test_srcs_can_contain_rule_generating_py_and_nonpy_files) 73 74def _test_srcs_generating_no_py_files_is_error(name, config): 75 rt_util.helper_target( 76 config.rule, 77 name = name + "_subject", 78 srcs = [name + "_gen"], 79 ) 80 rt_util.helper_target( 81 native.genrule, 82 name = name + "_gen", 83 cmd = "touch $(OUTS)", 84 outs = [name + "_gen.cc"], 85 ) 86 analysis_test( 87 name = name, 88 target = name + "_subject", 89 impl = _test_srcs_generating_no_py_files_is_error_impl, 90 expect_failure = True, 91 ) 92 93def _test_srcs_generating_no_py_files_is_error_impl(env, target): 94 env.expect.that_target(target).failures().contains_predicate( 95 matching.str_matches("does not produce*srcs files"), 96 ) 97 98_tests.append(_test_srcs_generating_no_py_files_is_error) 99 100def _test_files_to_compile(name, config): 101 rt_util.helper_target( 102 config.rule, 103 name = name + "_subject", 104 srcs = ["lib1.py"], 105 deps = [name + "_lib2"], 106 ) 107 rt_util.helper_target( 108 config.rule, 109 name = name + "_lib2", 110 srcs = ["lib2.py"], 111 deps = [name + "_lib3"], 112 ) 113 rt_util.helper_target( 114 config.rule, 115 name = name + "_lib3", 116 srcs = ["lib3.py"], 117 ) 118 analysis_test( 119 name = name, 120 target = name + "_subject", 121 impl = _test_files_to_compile_impl, 122 ) 123 124def _test_files_to_compile_impl(env, target): 125 target = env.expect.that_target(target) 126 target.output_group( 127 "compilation_prerequisites_INTERNAL_", 128 ).contains_exactly([ 129 "{package}/lib1.py", 130 "{package}/lib2.py", 131 "{package}/lib3.py", 132 ]) 133 target.output_group( 134 "compilation_outputs", 135 ).contains_exactly([ 136 "{package}/lib1.py", 137 "{package}/lib2.py", 138 "{package}/lib3.py", 139 ]) 140 141_tests.append(_test_files_to_compile) 142 143def py_library_test_suite(name): 144 config = struct(rule = py_library, base_test_rule = py_library) 145 native.test_suite( 146 name = name, 147 tests = pt_util.create_tests(_tests, config = config) + create_base_tests(config), 148 ) 149