Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[testing] Switch to junit5 #1128

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,34 @@ maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
name = "kotlin_rules_maven",
artifacts = [
"com.google.code.findbugs:jsr305:3.0.2",
"junit:junit:4.13-beta-3",
"com.google.protobuf:protobuf-java:3.6.0",
"com.google.protobuf:protobuf-java-util:3.6.0",
"com.google.guava:guava:27.1-jre",
"com.google.truth:truth:0.45",
"com.google.auto.service:auto-service:1.0.1",
"org.junit.jupiter:junit-jupiter-api:5.4.2",
"org.junit.jupiter:junit-jupiter-engine:5.4.2",
"org.junit.jupiter:junit-jupiter-params:5.4.2",
"org.junit.platform:junit-platform-console:1.4.2",
"com.google.auto.service:auto-service-annotations:1.0.1",
"com.google.auto.value:auto-value:1.10.1",
"com.google.auto.service:auto-service-annotations:jar:1.1.1",
"com.google.auto.service:auto-service:1.0.1",
"com.google.auto.value:auto-value-annotations:1.10.1",
"com.google.dagger:dagger:2.43.2",
"com.google.auto.value:auto-value:1.10.1",
"com.google.code.findbugs:jsr305:3.0.2",
"com.google.dagger:dagger-compiler:2.43.2",
"com.google.dagger:dagger-producers:2.43.2",
"com.google.dagger:dagger:2.43.2",
"com.google.guava:guava:27.1-jre",
"com.google.protobuf:protobuf-java-util:3.6.0",
"com.google.protobuf:protobuf-java:3.6.0",
"com.google.truth:truth:0.45",
"com.squareup.moshi:moshi-kotlin-codegen:1.15.0",
"com.squareup.moshi:moshi-kotlin:1.15.0",
"com.squareup.moshi:moshi:1.15.0",
"commons-lang:commons-lang:2.6",
"dev.zacsweers.autoservice:auto-service-ksp:jar:1.1.0",
"javax.annotation:javax.annotation-api:1.3.2",
"javax.inject:javax.inject:1",
"org.pantsbuild:jarjar:1.7.2",
"junit:junit:4.13-beta-3",
"org.jetbrains.kotlinx:atomicfu-js:0.15.2",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
"dev.zacsweers.autoservice:auto-service-ksp:jar:1.1.0",
"com.squareup.moshi:moshi:1.15.0",
"com.squareup.moshi:moshi-kotlin:1.15.0",
"com.squareup.moshi:moshi-kotlin-codegen:1.15.0",
"com.google.auto.service:auto-service-annotations:jar:1.1.1",
"org.pantsbuild:jarjar:1.7.2",
],
fetch_sources = True,
repositories = [
Expand Down
3 changes: 2 additions & 1 deletion docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ kt_jvm_library(<a href="#kt_jvm_library-name">name</a>, <a href="#kt_jvm_library

kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associates">associates</a>, <a href="#kt_jvm_test-data">data</a>, <a href="#kt_jvm_test-deps">deps</a>, <a href="#kt_jvm_test-env">env</a>, <a href="#kt_jvm_test-javac_opts">javac_opts</a>, <a href="#kt_jvm_test-jvm_flags">jvm_flags</a>, <a href="#kt_jvm_test-kotlinc_opts">kotlinc_opts</a>, <a href="#kt_jvm_test-main_class">main_class</a>,
<a href="#kt_jvm_test-module_name">module_name</a>, <a href="#kt_jvm_test-plugins">plugins</a>, <a href="#kt_jvm_test-resource_jars">resource_jars</a>, <a href="#kt_jvm_test-resource_strip_prefix">resource_strip_prefix</a>, <a href="#kt_jvm_test-resources">resources</a>, <a href="#kt_jvm_test-runtime_deps">runtime_deps</a>, <a href="#kt_jvm_test-srcs">srcs</a>,
<a href="#kt_jvm_test-test_class">test_class</a>)
<a href="#kt_jvm_test-test_class">test_class</a>, <a href="#kt_jvm_test-use_testrunner">use_testrunner</a>)


Setup a simple kotlin_test.
Expand Down Expand Up @@ -238,6 +238,7 @@ kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associat
|<a id="kt_jvm_test-runtime_deps"></a>runtime_deps | Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
|<a id="kt_jvm_test-srcs"></a>srcs | The list of source files that are processed to create the target, this can contain both Java and Kotlin files. Java analysis occurs first so Kotlin classes may depend on Java classes in the same compilation unit. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
|<a id="kt_jvm_test-test_class"></a>test_class | The Java class to be loaded by the test runner. | String | optional | "" |
|<a id="kt_jvm_test-use_testrunner"></a>use_testrunner | Use bazel test runner | Boolean | optional | True |


<!-- Generated with Stardoc: http://skydoc.bazel.build -->
Expand Down
6 changes: 5 additions & 1 deletion kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ _SPLIT_STRINGS = [

def kt_jvm_junit_test_impl(ctx):
providers = _kt_jvm_produce_jar_actions(ctx, "kt_jvm_test")
runtime_jars = depset(ctx.files._bazel_test_runner, transitive = [providers.java.transitive_runtime_jars])
runtime_jars = depset(
ctx.files._bazel_test_runner if ctx.attr.use_testrunner else [],
transitive = [providers.java.transitive_runtime_jars],
)

coverage_runfiles = []
if ctx.configuration.coverage_enabled:
Expand Down Expand Up @@ -293,6 +296,7 @@ def kt_jvm_junit_test_impl(ctx):
main_class = ctx.attr.main_class,
jvm_flags = [
"-ea",
"-Djava.security.manager=allow",
"-Dbazel.test_suite=%s" % test_class,
] + jvm_flags,
)
Expand Down
5 changes: 5 additions & 0 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ Setup a simple kotlin_test.
default = Label("@bazel_tools//tools/jdk:TestRunner_deploy.jar"),
allow_files = True,
),
"use_testrunner": attr.bool(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this flag solving for and when would you use it externally vs. setting it to False?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It matches java_test.use_testrunner. It allows customizing the test execution environment. In this case, we replace the bazel test runner with JUnit5.

doc = "Use bazel test runner. When disabled, an alternative test runner " +
"implementation should be provided and selected via `main_class`.",
default = True,
),
"test_class": attr.string(
doc = "The Java class to be loaded by the test runner.",
default = "",
Expand Down
5 changes: 5 additions & 0 deletions src/main/starlark/core/repositories/setup.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def kt_configure():
"com.squareup.moshi:moshi-kotlin:1.15.0",
"com.squareup.moshi:moshi-kotlin-codegen:1.15.0",
"com.google.auto.service:auto-service-annotations:jar:1.1.1",
"commons-lang:commons-lang:2.6",
"org.junit.jupiter:junit-jupiter-api:5.4.2",
"org.junit.jupiter:junit-jupiter-params:5.4.2",
"org.junit.jupiter:junit-jupiter-engine:5.4.2",
"org.junit.platform:junit-platform-console:1.4.2",
],
repositories = [
"https://maven-central.storage.googleapis.com/repos/central/data/",
Expand Down
Empty file added src/test/kotlin/BUILD.bazel
Empty file.
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/kotlin/KotlinJvm13Test.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.bazel.kotlin

import org.junit.Test
import org.junit.jupiter.api.Test;

class KotlinJvm13Test {
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.bazel.kotlin

import org.junit.jupiter.api.Test
import test.DEFAULT_FRIEND
import test.Service

Expand All @@ -10,7 +11,7 @@ import test.Service
class KotlinJvmAssociatesBasicVisibilityTest {
val service: Service = Service()

@org.junit.Test
@Test
fun testCanAccessFriendMembers() {
println(service.value)
println(service.iSayHolla(DEFAULT_FRIEND))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.bazel.kotlin

import org.junit.Test
import org.junit.jupiter.api.Test;

class KotlinJvmBasicAssertionTest : KotlinAssertionTestCase("src/test/data/jvm/basic") {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.bazel.kotlin

import org.junit.Test
import org.junit.jupiter.api.Test;

class KotlinJvmKaptAssertionTest : KotlinAssertionTestCase("src/test/data/jvm/kapt") {
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.bazel.kotlin

import org.junit.Test
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.bazel.kotlin

import org.junit.Ignore
import org.junit.Test
import org.junit.jupiter.api.Test;

class KotlinNormalizationAssertionTest : KotlinAssertionTestCase("src/test/data/jvm/basic") {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.bazel.kotlin.builder.Deps.Dep;
import io.bazel.kotlin.builder.KotlinJsTestBuilder;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.bazel.kotlin.builder.utils.jars.JarCreator
import io.bazel.worker.Status
import io.bazel.worker.Status.SUCCESS
import io.bazel.worker.WorkerContext
import org.junit.Test
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.io.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.google.devtools.build.lib.view.proto.Deps;
import com.google.devtools.build.lib.view.proto.Deps.Dependency.Kind;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.bazel.kotlin.builder.Deps;
import io.bazel.kotlin.builder.KotlinJvmTestBuilder;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.bazel.kotlin.builder.Deps;
import io.bazel.kotlin.builder.DirectoryType;
import io.bazel.kotlin.builder.KotlinJvmTestBuilder;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.bazel.kotlin.builder.tasks.jvm;

import io.bazel.kotlin.builder.DirectoryType
import io.bazel.kotlin.builder.KotlinJvmTestBuilder
import org.junit.Test
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.util.function.Consumer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.google.common.truth.Truth.assertThat
import com.google.devtools.build.lib.view.proto.Deps
import io.bazel.kotlin.builder.Deps.*
import io.bazel.kotlin.builder.KotlinJvmTestBuilder
import org.junit.Test
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.io.BufferedInputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import io.bazel.kotlin.builder.Deps.Dep;
import io.bazel.kotlin.builder.DirectoryType;
import io.bazel.kotlin.builder.KotlinJvmTestBuilder;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package io.bazel.kotlin.builder.tasks.jvm;
import com.google.common.truth.Truth.assertThat
import io.bazel.kotlin.builder.KotlinJvmTestBuilder
import org.junit.After
import org.junit.Test
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.util.function.Consumer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.bazel.kotlin.builder.tasks.jvm

import io.bazel.kotlin.builder.KotlinJvmTestBuilder
import org.junit.Test
import org.junit.jupiter.api.Test;
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package io.bazel.kotlin.builder.utils

import com.google.common.truth.Truth
import org.junit.Assert
import org.junit.Test
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//kotlin:jvm.bzl", "kt_jvm_test")
load("//src/test/kotlin:kt_junit5_test.bzl", "kt_jvm_test")
load("//src/test/kotlin/io/bazel/kotlin:defs.bzl", "kt_rules_test")

# Copyright 2020 The Bazel Authors. All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.bazel.kotlin.builder.utils.jars

import com.google.common.truth.Truth.assertThat
import io.bazel.testing.Temporary
import org.junit.Test
import org.junit.jupiter.api.Test;
import java.util.zip.ZipFile

class JarCreatorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.bazel.kotlin.builder.utils.jars;

import com.google.common.truth.StandardSubjectBuilder;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
Expand Down
9 changes: 2 additions & 7 deletions src/test/kotlin/io/bazel/kotlin/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@rules_java//java:defs.bzl", "java_test")
load("//kotlin:jvm.bzl", "kt_jvm_test")
load("//src/test/kotlin:kt_junit5_test.bzl", "kt_jvm_test")

def _get_class_name(kwargs):
if len(kwargs.get("srcs", [])) == 1:
Expand Down Expand Up @@ -50,11 +49,7 @@ def kt_rules_test(name, **kwargs):
args["jvm_flags"].append("-D%s=$(rlocationpath %s)" % (dep.replace("/", ".").replace(":", "."), dep))

args.setdefault("test_class", _get_class_name(kwargs))
for f in args.get("srcs"):
if f.endswith(".kt"):
kt_jvm_test(name = name, **args)
return
java_test(name = name, **args)
kt_jvm_test(name = name, **args)

def kt_rules_e2e_test(name, **kwargs):
kwargs.setdefault("size", "small")
Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/io/bazel/kotlin/integration/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")
load("//kotlin:jvm.bzl", "kt_jvm_library")
load("//src/test/kotlin:kt_junit5_test.bzl", "kt_jvm_test")

kt_jvm_library(
name = "lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.bazel.kotlin.integration
import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertWithMessage
import io.bazel.kotlin.integration.RulesKotlinWorkspace.Companion.build
import org.junit.Test
import org.junit.jupiter.api.Test;
import java.nio.file.Files

class MixedSourceCompileTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.bazel.kotlin.integration
import com.google.common.truth.Truth.assertThat
import io.bazel.kotlin.integration.WriteWorkspace.Indenting
import io.bazel.kotlin.integration.WriteWorkspace.Text
import org.junit.Test
import org.junit.jupiter.api.Test;
import java.nio.file.Files.exists

class WriteWorkspaceTest {
Expand Down
3 changes: 2 additions & 1 deletion src/test/kotlin/io/bazel/worker/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")
load("//kotlin:jvm.bzl", "kt_jvm_library")
load("//src/test/kotlin:kt_junit5_test.bzl", "kt_jvm_test")

kt_jvm_test(
name = "IOTest",
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/worker/IOTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package io.bazel.worker
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Test;
import java.io.BufferedInputStream
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/worker/InvocationWorkerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package io.bazel.worker

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.jupiter.api.Test;

class InvocationWorkerTest {
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package io.bazel.worker
import com.google.common.truth.Truth.assertThat
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse
import org.junit.Test
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets.UTF_8
import java.util.concurrent.Executors

Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/worker/WorkerContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package io.bazel.worker
import com.google.common.truth.Truth.assertThat
import io.bazel.worker.ContextLog.Granularity.DEBUG
import io.bazel.worker.Status.SUCCESS
import org.junit.Test
import org.junit.jupiter.api.Test;

class WorkerContextTest {
@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/worker/WorkerEnvironmentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package io.bazel.worker
import com.google.common.truth.Truth.assertThat
import com.google.devtools.build.lib.worker.WorkerProtocol
import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest
import org.junit.Test
import org.junit.jupiter.api.Test;

class WorkerEnvironmentTest {

Expand Down
Loading