diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2023-08-30 15:58:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-30 15:58:46 +0200 |
commit | c63ea36637ce956029fb15b1482c0683ecb8a587 (patch) | |
tree | 2b75a8a976b43530820e73dc60cce4b10d9fc005 /core | |
parent | 0e00edc6fcd406fcf38673ef6a2f8f59e8374de2 (diff) | |
download | dokka-c63ea36637ce956029fb15b1482c0683ecb8a587.tar.gz dokka-c63ea36637ce956029fb15b1482c0683ecb8a587.tar.bz2 dokka-c63ea36637ce956029fb15b1482c0683ecb8a587.zip |
Migrate to JUnit 5 and unify used test API (#3138)
Diffstat (limited to 'core')
-rw-r--r-- | core/build.gradle.kts | 2 | ||||
-rw-r--r-- | core/content-matcher-test-utils/build.gradle.kts | 2 | ||||
-rw-r--r-- | core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt | 28 | ||||
-rw-r--r-- | core/test-api/build.gradle.kts | 1 | ||||
-rw-r--r-- | core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt | 130 |
5 files changed, 91 insertions, 72 deletions
diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 2a5b668c..24e8ed5a 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -17,8 +17,8 @@ dependencies { } } + testImplementation(kotlin("test")) testImplementation(projects.core.testApi) - testImplementation(kotlin("test-junit")) } tasks { diff --git a/core/content-matcher-test-utils/build.gradle.kts b/core/content-matcher-test-utils/build.gradle.kts index 75602c64..f469c03d 100644 --- a/core/content-matcher-test-utils/build.gradle.kts +++ b/core/content-matcher-test-utils/build.gradle.kts @@ -6,5 +6,5 @@ dependencies { implementation(projects.core.testApi) implementation(kotlin("reflect")) - implementation(libs.assertk) + implementation(kotlin("test")) } diff --git a/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt b/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt index 6e05c11a..962841ba 100644 --- a/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt +++ b/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt @@ -1,12 +1,11 @@ package matchers.content -import assertk.assertThat -import assertk.assertions.contains -import assertk.assertions.isEqualTo import org.jetbrains.dokka.model.withDescendants import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.test.tools.matchers.content.* import kotlin.reflect.KClass +import kotlin.test.assertEquals +import kotlin.test.asserter // entry point: fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) { @@ -48,7 +47,7 @@ private val ContentComposite.extractedText fun <T : ContentComposite> ContentMatcherBuilder<T>.hasExactText(expected: String) { assertions += { - assertThat(this::extractedText).isEqualTo(expected) + assertEquals(expected, this.extractedText) } } @@ -74,7 +73,7 @@ fun ContentMatcherBuilder<*>.tabbedGroup( block: ContentMatcherBuilder<ContentGroup>.() -> Unit ) = composite<ContentGroup> { block() - check { assertThat(this::style).transform { style -> style.contains(ContentStyle.TabbedContent) }.isEqualTo(true) } + check { assertContains(this.style, ContentStyle.TabbedContent) } } fun ContentMatcherBuilder<*>.tab( @@ -82,21 +81,20 @@ fun ContentMatcherBuilder<*>.tab( ) = composite<ContentGroup> { block() check { - assertThat(this::extra).transform { extra -> extra[TabbedContentTypeExtra]?.value } - .isEqualTo(tabbedContentType) + assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) } } fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder<ContentHeader>.() -> Unit) = composite<ContentHeader> { block() - check { if (expectedLevel != null) assertThat(this::level).isEqualTo(expectedLevel) } + check { if (expectedLevel != null) assertEquals(expectedLevel, this.level) } } fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = composite<ContentGroup> { block() - check { assertThat(this::style).contains(TextStyle.Paragraph) } + check { assertContains(this.style, TextStyle.Paragraph) } } fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder<ContentLink>.() -> Unit) = composite(block) @@ -114,7 +112,7 @@ fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder<ContentCode fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = composite<ContentGroup> { block() - check { assertThat(this::style).contains(ContentStyle.Caption) } + check { assertContains(this.style, ContentStyle.Caption) } } fun ContentMatcherBuilder<*>.br() = node<ContentBreakLine>() @@ -139,3 +137,13 @@ fun ContentMatcherBuilder<ContentDivergentInstance>.divergent(block: ContentMatc fun ContentMatcherBuilder<ContentDivergentInstance>.after(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = composite(block) + +/* + * TODO replace with kotlin.test.assertContains after migrating to Kotlin language version 1.5+ + */ +private fun <T> assertContains(iterable: Iterable<T>, element: T) { + asserter.assertTrue( + { "Expected the collection to contain the element.\nCollection <$iterable>, element <$element>." }, + iterable.contains(element) + ) +} diff --git a/core/test-api/build.gradle.kts b/core/test-api/build.gradle.kts index 6b4b4d17..8db554b5 100644 --- a/core/test-api/build.gradle.kts +++ b/core/test-api/build.gradle.kts @@ -8,7 +8,6 @@ plugins { dependencies { api(projects.core) - implementation("junit:junit:4.13.2") // TODO: remove dependency to junit implementation(kotlin("reflect")) } diff --git a/core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt b/core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt index cfb809ea..80c486b7 100644 --- a/core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt +++ b/core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt @@ -9,7 +9,6 @@ import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.testApi.logger.TestLogger import org.jetbrains.dokka.utilities.DokkaLogger -import org.junit.rules.TemporaryFolder import testApi.testRunner.TestDokkaConfigurationBuilder import java.io.File import java.net.URL @@ -30,6 +29,9 @@ abstract class AbstractTest<M : TestMethods, T : TestBuilder<M>, D : DokkaTestGe ?: throw InvalidPathException(name, "Cannot be found") /** + * @param cleanupOutput if set to true, any temporary files will be cleaned up after execution. If set to false, + * it will be left to the user or the OS to delete it. Has no effect if [useOutputLocationFromConfig] + * is also set to true. * @param useOutputLocationFromConfig if set to true, output location specified in [DokkaConfigurationImpl.outputDir] * will be used. If set to false, a temporary folder will be used instead. */ @@ -40,24 +42,26 @@ abstract class AbstractTest<M : TestMethods, T : TestBuilder<M>, D : DokkaTestGe pluginOverrides: List<DokkaPlugin> = emptyList(), block: T.() -> Unit, ) { - val testMethods = testBuilder().apply(block).build() - val configurationToUse = - if (useOutputLocationFromConfig) { - configuration - } else { - val tempDir = getTempDir(cleanupOutput) + if (useOutputLocationFromConfig) { + runTests( + configuration = configuration, + pluginOverrides = pluginOverrides, + testLogger = logger, + block = block + ) + } else { + withTempDirectory(cleanUpAfterUse = cleanupOutput) { tempDir -> if (!cleanupOutput) { - logger.info("Output generated under: ${tempDir.root.absolutePath}") + logger.info("Output will be generated under: ${tempDir.absolutePath}") } - configuration.copy(outputDir = tempDir.root) + runTests( + configuration = configuration.copy(outputDir = tempDir), + pluginOverrides = pluginOverrides, + testLogger = logger, + block = block + ) } - - dokkaTestGenerator( - configurationToUse, - logger, - testMethods, - pluginOverrides - ).generate() + } } protected fun testInline( @@ -68,42 +72,62 @@ abstract class AbstractTest<M : TestMethods, T : TestBuilder<M>, D : DokkaTestGe loggerForTest: DokkaLogger = logger, block: T.() -> Unit, ) { - val testMethods = testBuilder().apply(block).build() - val testDirPath = getTempDir(cleanupOutput).root.toPath().toAbsolutePath() - val fileMap = query.toFileMap() - fileMap.materializeFiles(testDirPath.toAbsolutePath()) - if (!cleanupOutput) - loggerForTest.info("Output generated under: ${testDirPath.toAbsolutePath()}") - val newConfiguration = configuration.copy( - outputDir = testDirPath.toFile(), - sourceSets = configuration.sourceSets.map { sourceSet -> - sourceSet.copy( - sourceRoots = sourceSet.sourceRoots.map { file -> - testDirPath.toFile().resolve(file) - }.toSet(), - suppressedFiles = sourceSet.suppressedFiles.map { file -> - testDirPath.toFile().resolve(file) - }.toSet(), - sourceLinks = sourceSet.sourceLinks.map { link -> - link.copy( - localDirectory = testDirPath.toFile().resolve(link.localDirectory).absolutePath - ) - }.toSet(), - includes = sourceSet.includes.map { file -> - testDirPath.toFile().resolve(file) - }.toSet() - ) + withTempDirectory(cleanUpAfterUse = cleanupOutput) { tempDir -> + if (!cleanupOutput) { + loggerForTest.info("Output will be generated under: ${tempDir.absolutePath}") } - ) + + val fileMap = query.toFileMap() + fileMap.materializeFiles(tempDir.toPath().toAbsolutePath()) + + val newConfiguration = configuration.copy( + outputDir = tempDir, + sourceSets = configuration.sourceSets.map { sourceSet -> + sourceSet.copy( + sourceRoots = sourceSet.sourceRoots.map { file -> tempDir.resolve(file) }.toSet(), + suppressedFiles = sourceSet.suppressedFiles.map { file -> tempDir.resolve(file) }.toSet(), + sourceLinks = sourceSet.sourceLinks.map { + link -> link.copy(localDirectory = tempDir.resolve(link.localDirectory).absolutePath) + }.toSet(), + includes = sourceSet.includes.map { file -> tempDir.resolve(file) }.toSet() + ) + } + ) + runTests( + configuration = newConfiguration, + pluginOverrides = pluginOverrides, + testLogger = loggerForTest, + block = block + ) + } + } + + private fun withTempDirectory(cleanUpAfterUse: Boolean, block: (tempDirectory: File) -> Unit) { + val tempDir = this.createTempDir() + try { + block(tempDir) + } finally { + if (cleanUpAfterUse) { + tempDir.delete() + } + } + } + + private fun runTests( + configuration: DokkaConfiguration, + pluginOverrides: List<DokkaPlugin>, + testLogger: DokkaLogger = logger, + block: T.() -> Unit + ) { + val testMethods = testBuilder().apply(block).build() dokkaTestGenerator( - newConfiguration, - loggerForTest, + configuration, + testLogger, testMethods, pluginOverrides ).generate() } - private fun String.toFileMap(): Map<String, String> { return this.trimIndent().trimMargin() .replace("\r\n", "\n") @@ -141,20 +165,8 @@ abstract class AbstractTest<M : TestMethods, T : TestBuilder<M>, D : DokkaTestGe Files.write(file, content.toByteArray(charset)) } - private fun getTempDir(cleanupOutput: Boolean) = - if (cleanupOutput) { - TemporaryFolder().apply { create() } - } else { - TemporaryFolderWithoutCleanup().apply { create() } - } - - /** - * Creates a temporary folder, but doesn't delete files - * right after it's been used, delegating it to the OS - */ - private class TemporaryFolderWithoutCleanup : TemporaryFolder() { - override fun after() { } - } + @Suppress("DEPRECATION") // TODO migrate to kotlin.io.path.createTempDirectory with languageVersion >= 1.5 + private fun createTempDir(): File = kotlin.io.createTempDir() protected fun dokkaConfiguration(block: TestDokkaConfigurationBuilder.() -> Unit): DokkaConfigurationImpl = testApi.testRunner.dokkaConfiguration(block) |