aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/expect/ExpectTest.kt
blob: 3a85db176f390915f8331543c9ded98b6ac45056 (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
package expect

import org.junit.Test
import testApi.testRunner.AbstractCoreTest
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.TimeUnit

class ExpectTest : AbstractCoreTest() {

    private fun generateOutput(path: Path): Path? {
        val config = dokkaConfiguration {
            passes {
                pass {
                    sourceRoots = listOf(path.asString())
                }
            }
        }

        var result: Path? = null
        testFromData(config, cleanupOutput = false) {
            renderingStage = { _, context -> result = Paths.get(context.configuration.outputDir) }
        }
        return result
    }

    private fun compareOutput(expected: Path, obtained: Path?, gitTimeout: Long = 500) {
        obtained?.let { path ->
            val gitCompare = ProcessBuilder(
                "git",
                "--no-pager",
                "diff",
                expected.asString(),
                path.asString()
            ).also { logger.info("git diff command: ${it.command().joinToString(" ")}") }
                .start()

            assert(gitCompare.waitFor(gitTimeout, TimeUnit.MILLISECONDS)) { "Git timed out after $gitTimeout" }
            gitCompare.inputStream.bufferedReader().lines().forEach { logger.info(it) }
            gitCompare.errorStream.bufferedReader().lines().forEach { logger.info(it) }
            assert(gitCompare.exitValue() == 0) { "${path.fileName}: outputs don't match" }
        } ?: throw AssertionError("obtained path is null")
    }

    @Test
    fun expectTest() {
        val sources = Paths.get("src/test", "resources", "expect")

        Files.list(sources).forEach { p ->
            val expectOut = p.resolve("out")
            val testOut = generateOutput(p.resolve("src"))
                .also { logger.info("Test out: ${it?.asString()}") }

            compareOutput(expectOut, testOut)
            testOut?.toFile()?.deleteRecursively()
        }
    }

    fun Path.asString() = toAbsolutePath().normalize().toString()

}