|
| 1 | +"""Rules for writing tests with JUnit""" |
| 2 | + |
| 3 | +load( |
| 4 | + "@io_bazel_rules_scala//scala/private:common_attributes.bzl", |
| 5 | + "common_attrs", |
| 6 | + "implicit_deps", |
| 7 | + "launcher_template", |
| 8 | +) |
| 9 | +load("@io_bazel_rules_scala//scala/private:common_outputs.bzl", "common_outputs") |
| 10 | +load( |
| 11 | + "@io_bazel_rules_scala//scala/private:rule_impls.bzl", |
| 12 | + "collect_jars_from_common_ctx", |
| 13 | + "declare_executable", |
| 14 | + "get_scalac_provider", |
| 15 | + "get_unused_dependency_checker_mode", |
| 16 | + "scala_binary_common", |
| 17 | + "write_executable", |
| 18 | + "write_java_wrapper", |
| 19 | +) |
| 20 | + |
| 21 | +def _gen_test_suite_flags_based_on_prefixes_and_suffixes(ctx, archives): |
| 22 | + return struct( |
| 23 | + archiveFlag = "-Dbazel.discover.classes.archives.file.paths=%s" % |
| 24 | + archives, |
| 25 | + prefixesFlag = "-Dbazel.discover.classes.prefixes=%s" % ",".join( |
| 26 | + ctx.attr.prefixes, |
| 27 | + ), |
| 28 | + printFlag = "-Dbazel.discover.classes.print.discovered=%s" % |
| 29 | + ctx.attr.print_discovered_classes, |
| 30 | + suffixesFlag = "-Dbazel.discover.classes.suffixes=%s" % ",".join( |
| 31 | + ctx.attr.suffixes, |
| 32 | + ), |
| 33 | + testSuiteFlag = "-Dbazel.test_suite=%s" % ctx.attr.suite_class, |
| 34 | + ) |
| 35 | + |
| 36 | +def _serialize_archives_short_path(archives): |
| 37 | + archives_short_path = "" |
| 38 | + for archive in archives: |
| 39 | + archives_short_path += archive.short_path + "," |
| 40 | + return archives_short_path[:-1] #remove redundant comma |
| 41 | + |
| 42 | +def _get_test_archive_jars(ctx, test_archives): |
| 43 | + flattened_list = [] |
| 44 | + for archive in test_archives: |
| 45 | + class_jars = [java_output.class_jar for java_output in archive[JavaInfo].outputs.jars] |
| 46 | + flattened_list.extend(class_jars) |
| 47 | + return flattened_list |
| 48 | + |
| 49 | +def _scala_junit_test_impl(ctx): |
| 50 | + if (not (ctx.attr.prefixes) and not (ctx.attr.suffixes)): |
| 51 | + fail( |
| 52 | + "Setting at least one of the attributes ('prefixes','suffixes') is required", |
| 53 | + ) |
| 54 | + scalac_provider = get_scalac_provider(ctx) |
| 55 | + |
| 56 | + unused_dependency_checker_mode = get_unused_dependency_checker_mode(ctx) |
| 57 | + unused_dependency_checker_ignored_targets = [ |
| 58 | + target.label |
| 59 | + for target in scalac_provider.default_classpath + |
| 60 | + ctx.attr.unused_dependency_checker_ignored_targets |
| 61 | + ] + [ |
| 62 | + ctx.attr._junit.label, |
| 63 | + ctx.attr._hamcrest.label, |
| 64 | + ctx.attr.suite_label.label, |
| 65 | + ctx.attr._bazel_test_runner.label, |
| 66 | + ] |
| 67 | + unused_dependency_checker_is_off = unused_dependency_checker_mode == "off" |
| 68 | + |
| 69 | + jars = collect_jars_from_common_ctx( |
| 70 | + ctx, |
| 71 | + scalac_provider.default_classpath, |
| 72 | + extra_deps = [ |
| 73 | + ctx.attr._junit, |
| 74 | + ctx.attr._hamcrest, |
| 75 | + ctx.attr.suite_label, |
| 76 | + ctx.attr._bazel_test_runner, |
| 77 | + ], |
| 78 | + unused_dependency_checker_is_off = unused_dependency_checker_is_off, |
| 79 | + ) |
| 80 | + (cjars, transitive_rjars) = (jars.compile_jars, jars.transitive_runtime_jars) |
| 81 | + implicit_junit_deps_needed_for_java_compilation = [ |
| 82 | + ctx.attr._junit, |
| 83 | + ctx.attr._hamcrest, |
| 84 | + ] |
| 85 | + |
| 86 | + executable = declare_executable(ctx) |
| 87 | + |
| 88 | + wrapper = write_java_wrapper(ctx, "", "") |
| 89 | + out = scala_binary_common( |
| 90 | + ctx, |
| 91 | + executable, |
| 92 | + cjars, |
| 93 | + transitive_rjars, |
| 94 | + jars.transitive_compile_jars, |
| 95 | + jars.jars2labels, |
| 96 | + wrapper, |
| 97 | + implicit_junit_deps_needed_for_java_compilation = |
| 98 | + implicit_junit_deps_needed_for_java_compilation, |
| 99 | + unused_dependency_checker_ignored_targets = |
| 100 | + unused_dependency_checker_ignored_targets, |
| 101 | + unused_dependency_checker_mode = unused_dependency_checker_mode, |
| 102 | + deps_providers = jars.deps_providers, |
| 103 | + ) |
| 104 | + |
| 105 | + if ctx.attr.tests_from: |
| 106 | + archives = _get_test_archive_jars(ctx, ctx.attr.tests_from) |
| 107 | + else: |
| 108 | + archives = [archive.class_jar for archive in out.scala.outputs.jars] |
| 109 | + |
| 110 | + serialized_archives = _serialize_archives_short_path(archives) |
| 111 | + test_suite = _gen_test_suite_flags_based_on_prefixes_and_suffixes( |
| 112 | + ctx, |
| 113 | + serialized_archives, |
| 114 | + ) |
| 115 | + launcherJvmFlags = [ |
| 116 | + "-ea", |
| 117 | + test_suite.archiveFlag, |
| 118 | + test_suite.prefixesFlag, |
| 119 | + test_suite.suffixesFlag, |
| 120 | + test_suite.printFlag, |
| 121 | + test_suite.testSuiteFlag, |
| 122 | + ] |
| 123 | + write_executable( |
| 124 | + ctx = ctx, |
| 125 | + executable = executable, |
| 126 | + jvm_flags = launcherJvmFlags + ctx.attr.jvm_flags, |
| 127 | + main_class = "com.google.testing.junit.runner.BazelTestRunner", |
| 128 | + rjars = out.transitive_rjars, |
| 129 | + use_jacoco = False, |
| 130 | + wrapper = wrapper, |
| 131 | + ) |
| 132 | + |
| 133 | + return out |
| 134 | + |
| 135 | +_scala_junit_test_attrs = { |
| 136 | + "prefixes": attr.string_list(default = []), |
| 137 | + "suffixes": attr.string_list(default = []), |
| 138 | + "suite_label": attr.label( |
| 139 | + default = Label( |
| 140 | + "//src/java/io/bazel/rulesscala/test_discovery:test_discovery", |
| 141 | + ), |
| 142 | + ), |
| 143 | + "suite_class": attr.string( |
| 144 | + default = "io.bazel.rulesscala.test_discovery.DiscoveredTestSuite", |
| 145 | + ), |
| 146 | + "print_discovered_classes": attr.bool( |
| 147 | + default = False, |
| 148 | + mandatory = False, |
| 149 | + ), |
| 150 | + "_junit": attr.label( |
| 151 | + default = Label( |
| 152 | + "//external:io_bazel_rules_scala/dependency/junit/junit", |
| 153 | + ), |
| 154 | + ), |
| 155 | + "_hamcrest": attr.label( |
| 156 | + default = Label( |
| 157 | + "//external:io_bazel_rules_scala/dependency/hamcrest/hamcrest_core", |
| 158 | + ), |
| 159 | + ), |
| 160 | + "_bazel_test_runner": attr.label( |
| 161 | + default = Label( |
| 162 | + "@io_bazel_rules_scala//scala:bazel_test_runner_deploy", |
| 163 | + ), |
| 164 | + allow_files = True, |
| 165 | + ), |
| 166 | +} |
| 167 | + |
| 168 | +_junit_resolve_deps = { |
| 169 | + "_scala_toolchain": attr.label_list( |
| 170 | + default = [ |
| 171 | + Label( |
| 172 | + "//external:io_bazel_rules_scala/dependency/scala/scala_library", |
| 173 | + ), |
| 174 | + Label("//external:io_bazel_rules_scala/dependency/junit/junit"), |
| 175 | + Label( |
| 176 | + "//external:io_bazel_rules_scala/dependency/hamcrest/hamcrest_core", |
| 177 | + ), |
| 178 | + ], |
| 179 | + allow_files = False, |
| 180 | + ), |
| 181 | +} |
| 182 | + |
| 183 | +_scala_junit_test_attrs.update(launcher_template) |
| 184 | + |
| 185 | +_scala_junit_test_attrs.update(implicit_deps) |
| 186 | + |
| 187 | +_scala_junit_test_attrs.update(common_attrs) |
| 188 | + |
| 189 | +_scala_junit_test_attrs.update(_junit_resolve_deps) |
| 190 | + |
| 191 | +_scala_junit_test_attrs.update({ |
| 192 | + "tests_from": attr.label_list(providers = [[JavaInfo]]), |
| 193 | +}) |
| 194 | + |
| 195 | +scala_junit_test = rule( |
| 196 | + attrs = _scala_junit_test_attrs, |
| 197 | + fragments = ["java"], |
| 198 | + outputs = common_outputs, |
| 199 | + test = True, |
| 200 | + toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"], |
| 201 | + implementation = _scala_junit_test_impl, |
| 202 | +) |
0 commit comments