From 2e88c6f70feff1edb9f386110a071df897f3b319 Mon Sep 17 00:00:00 2001
From: Ignat Beresnev <ignat.beresnev@jetbrains.com>
Date: Thu, 28 Apr 2022 13:59:57 +0300
Subject: Fix suddenly appearing all-modules-page/index.md file (#2475)

* Fix suddenly appearing all-modules-page/index.md file

* Add a kdoc for `useOutputLocationFromConfig` param
---
 .../main/kotlin/testApi/testRunner/TestRunner.kt   | 43 ++++++++++++++--------
 .../templates/MultiModuleDocumentationTest.kt      |  2 +-
 .../templates/ResolveLinkCommandResolutionTest.kt  |  4 +-
 .../ResolveLinkGfmCommandResolutionTest.kt         |  2 +-
 .../AddToNavigationCommandResolutionTest.kt        |  2 +-
 .../templates/AddToSearchCommandResolutionTest.kt  |  2 +-
 .../templates/SubstitutionCommandResolutionTest.kt |  2 +-
 7 files changed, 35 insertions(+), 22 deletions(-)

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 de0cd896..44353535 100644
--- a/core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt
+++ b/core/test-api/src/main/kotlin/testApi/testRunner/TestRunner.kt
@@ -28,22 +28,28 @@ abstract class AbstractTest<M : TestMethods, T : TestBuilder<M>, D : DokkaTestGe
         File("src/test/resources/$name").takeIf { it.exists() }?.toPath()
             ?: throw InvalidPathException(name, "Cannot be found")
 
+    /**
+     * @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.
+     */
     protected fun testFromData(
         configuration: DokkaConfigurationImpl,
         cleanupOutput: Boolean = true,
-        preserveOutputLocation: Boolean = false,
+        useOutputLocationFromConfig: Boolean = false,
         pluginOverrides: List<DokkaPlugin> = emptyList(),
         block: T.() -> Unit
     ) {
         val testMethods = testBuilder().apply(block).build()
-        val configurationToUse = if (!preserveOutputLocation) {
-            val tempDir = getTempDir(cleanupOutput)
-            if (!cleanupOutput)
-                logger.info("Output generated under: ${tempDir.root.absolutePath}")
-            configuration.copy(
-                outputDir = tempDir.root
-            )
-        } else configuration
+        val configurationToUse =
+            if (useOutputLocationFromConfig) {
+                configuration
+            } else {
+                val tempDir = getTempDir(cleanupOutput)
+                if (!cleanupOutput) {
+                    logger.info("Output generated under: ${tempDir.root.absolutePath}")
+                }
+                configuration.copy(outputDir = tempDir.root)
+            }
 
         dokkaTestGenerator(
             configurationToUse,
@@ -131,12 +137,19 @@ 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 {
-        object : TemporaryFolder() {
-            override fun after() {}
-        }.apply { create() }
+    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() { }
     }
 
     protected fun dokkaConfiguration(block: TestDokkaConfigurationBuilder.() -> Unit): DokkaConfigurationImpl =
diff --git a/plugins/all-modules-page/src/test/kotlin/templates/MultiModuleDocumentationTest.kt b/plugins/all-modules-page/src/test/kotlin/templates/MultiModuleDocumentationTest.kt
index 64948be8..525331fc 100644
--- a/plugins/all-modules-page/src/test/kotlin/templates/MultiModuleDocumentationTest.kt
+++ b/plugins/all-modules-page/src/test/kotlin/templates/MultiModuleDocumentationTest.kt
@@ -39,7 +39,7 @@ class MultiModuleDocumentationTest : MultiModuleAbstractTest() {
             includes = listOf(folder.root.resolve("README.md"))
         }
 
-        testFromData(configuration, preserveOutputLocation = true) {
+        testFromData(configuration) {
             allModulesPageCreationStage = { rootPage ->
                 (rootPage as? MultimoduleRootPageNode)?.content?.dfs { it.dci.kind == ContentKind.Cover }?.children?.firstOrNull()
                     ?.assertNode {
diff --git a/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkCommandResolutionTest.kt b/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkCommandResolutionTest.kt
index b7487c16..7ef8c3ea 100644
--- a/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkCommandResolutionTest.kt
+++ b/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkCommandResolutionTest.kt
@@ -59,7 +59,7 @@ class ResolveLinkCommandResolutionTest : MultiModuleAbstractTest() {
         val contentFile = setup(link)
         val configuration = configuration()
 
-        testFromData(configuration, preserveOutputLocation = true) {
+        testFromData(configuration, useOutputLocationFromConfig = true) {
             finishProcessingSubmodules = {
                 assertHtmlEqualsIgnoringWhitespace(expected, contentFile.readText())
             }
@@ -88,7 +88,7 @@ class ResolveLinkCommandResolutionTest : MultiModuleAbstractTest() {
         val contentFile = setup(link)
         val configuration = configuration()
 
-        testFromData(configuration, preserveOutputLocation = true) {
+        testFromData(configuration, useOutputLocationFromConfig = true) {
             finishProcessingSubmodules = {
                 assertHtmlEqualsIgnoringWhitespace(expected, contentFile.readText())
             }
diff --git a/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkGfmCommandResolutionTest.kt b/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkGfmCommandResolutionTest.kt
index 975d3183..b38fb03f 100644
--- a/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkGfmCommandResolutionTest.kt
+++ b/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkGfmCommandResolutionTest.kt
@@ -54,7 +54,7 @@ class ResolveLinkGfmCommandResolutionTest : MultiModuleAbstractTest() {
         val content = setup(link)
         val configuration = configuration()
 
-        testFromData(configuration, pluginOverrides = listOf(GfmTemplateProcessingPlugin(), GfmPlugin()), preserveOutputLocation = true) {
+        testFromData(configuration, pluginOverrides = listOf(GfmTemplateProcessingPlugin(), GfmPlugin()), useOutputLocationFromConfig = true) {
             finishProcessingSubmodules = {
                 assertEquals(expected, content.readText().trim())
             }
diff --git a/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
index fb229643..81d39752 100644
--- a/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
+++ b/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
@@ -126,7 +126,7 @@ class AddToNavigationCommandResolutionTest : TemplatingAbstractTest() {
         val module2Navigation = module2.resolve("navigation.html")
         module2Navigation.writeText(inputForModule("module2"))
 
-        testFromData(configuration, preserveOutputLocation = true) {
+        testFromData(configuration, useOutputLocationFromConfig = true) {
             finishProcessingSubmodules = { ctx ->
                 test(ctx)
             }
diff --git a/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt
index 96fcc8dd..d2ad7e0c 100644
--- a/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt
+++ b/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt
@@ -55,7 +55,7 @@ class AddToSearchCommandResolutionTest : TemplatingAbstractTest() {
             this.outputDir = outputDir
         }
 
-        testFromData(configuration, preserveOutputLocation = true) {
+        testFromData(configuration, useOutputLocationFromConfig = true) {
             finishProcessingSubmodules = { _ ->
                 val expected = elements.map { it.copy(location = "module1/${it.location}") } +
                         elements.map { it.copy(location = "module2/${it.location}") }
diff --git a/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt
index 44acf340..c31f63c7 100644
--- a/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt
+++ b/plugins/templating/src/test/kotlin/templates/SubstitutionCommandResolutionTest.kt
@@ -102,7 +102,7 @@ class SubstitutionCommandResolutionTest : TemplatingAbstractTest() {
             this.outputDir = folder.root
         }
 
-        testFromData(configuration, preserveOutputLocation = true){
+        testFromData(configuration, useOutputLocationFromConfig = true){
             finishProcessingSubmodules = {
                 assertHtmlEqualsIgnoringWhitespace(expected, testedFile.readText())
             }
-- 
cgit