User: kyle wood Date: 24 Nov 26 06:48 Revision: 0417bb52644f8e8a16a769e119f5ec636b8e675b Summary: Add tests for bundled templates submodule TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=10823&personal=false Index: build.gradle.kts =================================================================== --- build.gradle.kts (revision e0069be60d2f270af6ea985afd8f533ef1d0a42e) +++ build.gradle.kts (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) @@ -340,6 +340,11 @@ // instead of reaching the real services systemProperty("mcdev.test.httpMock", "http://127.0.0.1:1") + val templatesDir = file("templates") + inputs.files(fileTree(templatesDir) { exclude(".git") }).withPropertyName("templates") + systemProperty("mcdev.test.templates", templatesDir.absolutePath) + systemProperty("mcdev.test.templatesFormatVersion", builtinTemplatesFormatVersion) + jvmArgs( "-Dsun.io.useCanonCaches=false", "-Dsun.io.useCanonPrefixCache=false", Index: src/test/kotlin/creator/BuiltinTemplateValidationTest.kt =================================================================== --- src/test/kotlin/creator/BuiltinTemplateValidationTest.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) +++ src/test/kotlin/creator/BuiltinTemplateValidationTest.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) @@ -0,0 +1,133 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2026 minecraft-dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3.0 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.demonwav.mcdev.creator + +import com.demonwav.mcdev.creator.custom.TemplateValidationReporterImpl +import com.demonwav.mcdev.creator.custom.providers.InvalidVersionTemplate +import com.demonwav.mcdev.framework.LogCapture +import com.demonwav.mcdev.framework.http.HttpMockExtension +import kotlin.time.Duration.Companion.seconds +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.RegisterExtension + +@DisplayName("Built-in Template Validation Tests") +class BuiltinTemplateValidationTest : CreatorTemplateProcessorTestBase() { + + @JvmField + @RegisterExtension + val http = HttpMockExtension() + + private val templateNames = listOf( + "architectury", + "bukkit/paper", + "bukkit/spigot", + "bungeecord", + "fabric", + "forge", + "multiloader", + "neoforge", + "sponge", + "velocity", + ) + + private fun test(block: suspend () -> Unit) = runBlocking { withTimeout(60.seconds) { block() } } + + private fun validate(name: String) = test { + LogCapture().use { log -> + val template = PinnedTemplates.load(wizardContext, name) + assertTrue(template.isValid, "$name isn't valid") + + val reporter = TemplateValidationReporterImpl() + processor.setupTemplate(template, reporter) + assertFalse(reporter.hasErrors, "Template errors: ${reporter.items}") + assertFalse(reporter.hasWarns, "Template warnings: ${reporter.items}") + + // Failed downloads only log a warning, so check those too + session.awaitIdle() + log.assertNoErrors() + assertEquals(emptyList(), log.warnings, "Warnings were logged") + } + } + + @Test + @DisplayName("The local template provider finds every built-in template") + fun loadAll() = test { + // A missing or mismatched checkout should fail with its own message, not as a list of names that differ + PinnedTemplates.requireExpectedFormat() + assertEquals(templateNames, PinnedTemplates.names(), "Each built-in template needs a validation test") + + LogCapture().use { log -> + val templates = PinnedTemplates.loadAll(wizardContext) + assertEquals(templateNames.size, templates.size, "Loaded templates: ${templates.map { it.label }}") + for (template in templates) { + assertFalse(template is InvalidVersionTemplate, "${template.label} has an unsupported format version") + assertTrue(template.isValid, "${template.label} isn't valid") + } + log.assertNoErrors() + } + } + + @Test + @DisplayName("Architectury") + fun architectury() = validate("architectury") + + @Test + @DisplayName("Paper") + fun paper() = validate("bukkit/paper") + + @Test + @DisplayName("Spigot") + fun spigot() = validate("bukkit/spigot") + + @Test + @DisplayName("BungeeCord") + fun bungeeCord() = validate("bungeecord") + + @Test + @DisplayName("Fabric") + fun fabric() = validate("fabric") + + @Test + @DisplayName("Forge") + fun forge() = validate("forge") + + @Test + @DisplayName("Multiloader") + fun multiloader() = validate("multiloader") + + @Test + @DisplayName("NeoForge") + fun neoForge() = validate("neoforge") + + @Test + @DisplayName("Sponge") + fun sponge() = validate("sponge") + + @Test + @DisplayName("Velocity") + fun velocity() = validate("velocity") +} Index: src/test/kotlin/creator/PinnedTemplates.kt =================================================================== --- src/test/kotlin/creator/PinnedTemplates.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) +++ src/test/kotlin/creator/PinnedTemplates.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) @@ -0,0 +1,161 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2026 minecraft-dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3.0 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.demonwav.mcdev.creator + +import com.demonwav.mcdev.MinecraftSettings +import com.demonwav.mcdev.creator.custom.TemplateDescriptor +import com.demonwav.mcdev.creator.custom.providers.LoadedTemplate +import com.demonwav.mcdev.creator.custom.providers.TemplateProvider +import com.demonwav.mcdev.creator.custom.providers.VfsLoadedTemplate +import com.demonwav.mcdev.util.refreshSync +import com.demonwav.mcdev.util.virtualFile +import com.google.gson.JsonParser +import com.intellij.ide.util.projectWizard.WizardContext +import java.nio.file.Path +import kotlin.io.path.Path +import kotlin.io.path.isDirectory +import kotlin.io.path.readText + +/** + * The built-in templates from the `templates` submodule, loaded through the same provider code the wizard uses. + * + * Templates are named by their descriptor's path in the checkout without the `.mcdev.template.json` suffix, for + * example `forge` or `bukkit/paper`. + * + * Loading fails with a message saying what to fix if the checkout, or the branch `.gitmodules` pins it to, isn't on the + * format version the tests expect. + */ +object PinnedTemplates { + + private const val ROOT_PROPERTY = "mcdev.test.templates" + private const val PINNED_VERSION_PROPERTY = "mcdev.test.templatesFormatVersion" + private const val DESCRIPTOR_SUFFIX = ".mcdev.template.json" + + val root: Path by lazy { Path(requireProperty(ROOT_PROPERTY)) } + + // The format version of the branch .gitmodules pins the templates submodule to + private val pinnedFormatVersion: Int by lazy { requireProperty(PINNED_VERSION_PROPERTY).toInt() } + + // A wrong pin also makes checkFormatVersion's advice wrong, so only report the pin in that case + private val checkoutProblem: String? by lazy { checkPinnedVersion(pinnedFormatVersion) ?: checkFormatVersion(root) } + + /** + * Throws [AssertionError] if the checkout or `.gitmodules` isn't on [TemplateDescriptor.LATEST_FORMAT_VERSION]. + */ + fun requireExpectedFormat() { + checkoutProblem?.let { throw AssertionError(it) } + } + + /** + * Loads one template the way [TemplateProvider.findTemplates] does. + */ + suspend fun load(context: WizardContext, name: String): VfsLoadedTemplate { + requireExpectedFormat() + val descriptorPath = descriptors(root)[name] ?: throw AssertionError("There's no template named $name in $root") + val modalityState = context.modalityState + val repoRoot = checkNotNull(root.virtualFile) { "Couldn't find the templates checkout at $root" } + repoRoot.refreshSync(modalityState) + val descriptorFile = checkNotNull(descriptorPath.virtualFile) { "Couldn't find $descriptorPath" } + val bundle = TemplateProvider.loadMessagesBundle(modalityState, repoRoot) + val template = TemplateProvider.createVfsLoadedTemplate( + modalityState, + descriptorFile.parent, + descriptorFile, + bundle = bundle, + ) + return template ?: throw AssertionError("The template $name is hidden") + } + + /** + * Loads every template in the checkout through the local template provider. + */ + suspend fun loadAll(context: WizardContext): Collection { + requireExpectedFormat() + val repo = MinecraftSettings.TemplateRepo("Built-in templates", "local", root.toString()) + return checkNotNull(TemplateProvider.get("local")).loadTemplates(context, repo) + } + + /** + * The names of the templates in the checkout, sorted. + */ + fun names(): List = descriptors(root).keys.toList() + + /** + * Returns a message explaining what's wrong if [root] has no templates or any template isn't on [expected], or + * null if every template is on it. + */ + fun checkFormatVersion(root: Path, expected: Int = TemplateDescriptor.LATEST_FORMAT_VERSION): String? { + val descriptors = descriptors(root) + if (descriptors.isEmpty()) { + return "There are no templates at $root. Check that the templates submodule is checked out." + } + + val mismatches = descriptors.mapNotNull { (name, file) -> + val version = runCatching { + JsonParser.parseString(file.readText()).asJsonObject.get("version")?.asInt + }.getOrNull() + when (version) { + expected -> null + null -> "$name: no readable format version" + else -> "$name: format version $version" + } + } + if (mismatches.isEmpty()) { + return null + } + + return buildString { + append("The templates at $root aren't all on format version $expected, which the creator tests expect:\n") + for (mismatch in mismatches) { + append(" ").append(mismatch).append('\n') + } + append("Check out the v$expected branch in the templates submodule, as pinned in .gitmodules.") + } + } + + private fun checkPinnedVersion(pinned: Int): String? { + val expected = TemplateDescriptor.LATEST_FORMAT_VERSION + if (pinned == expected) { + return null + } + return ".gitmodules pins the templates submodule to the v$pinned branch, but the plugin's latest template " + + "format version is $expected. Change them together." + } + + private fun requireProperty(name: String): String = + checkNotNull(System.getProperty(name)) { "The $name system property isn't set" } + + // Template names to descriptor files, sorted by name + private fun descriptors(root: Path): Map { + if (!root.isDirectory()) { + return emptyMap() + } + return root.toFile().walkTopDown() + .onEnter { it.name != ".git" } + .filter { it.isFile && it.name.endsWith(DESCRIPTOR_SUFFIX) } + .map { file -> + val relative = file.relativeTo(root.toFile()).invariantSeparatorsPath + relative.removeSuffix(DESCRIPTOR_SUFFIX).removeSuffix("/") to file.toPath() + } + .sortedBy { (name, _) -> name } + .toMap() + } +} Index: src/test/kotlin/creator/PinnedTemplatesTest.kt =================================================================== --- src/test/kotlin/creator/PinnedTemplatesTest.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) +++ src/test/kotlin/creator/PinnedTemplatesTest.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) @@ -0,0 +1,63 @@ +/* + * Minecraft Development for IntelliJ + * + * https://mcdev.io/ + * + * Copyright (C) 2026 minecraft-dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, version 3.0 only. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.demonwav.mcdev.creator + +import java.nio.file.Path +import kotlin.io.path.createDirectories +import kotlin.io.path.writeText +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Assertions.fail +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir + +@DisplayName("Pinned Templates Tests") +class PinnedTemplatesTest { + + @TempDir + lateinit var dir: Path + + private fun descriptor(path: String, json: String) { + val file = dir.resolve(path) + file.parent.createDirectories() + file.writeText(json) + } + + @Test + @DisplayName("The templates submodule and its .gitmodules branch are on the format version the tests expect") + fun checkoutFormatVersion() { + PinnedTemplates.requireExpectedFormat() + } + + @Test + @DisplayName("A checkout on another format version names each template that doesn't match") + fun wrongVersion() { + descriptor("forge/.mcdev.template.json", """{"version": 3}""") + descriptor("sponge/.mcdev.template.json", "not json") + descriptor("bukkit/paper.mcdev.template.json", """{"version": 4}""") + + val problem = PinnedTemplates.checkFormatVersion(dir, expected = 4) ?: fail("The mismatch wasn't caught") + assertTrue("forge" in problem, problem) + assertTrue("sponge" in problem, problem) + assertFalse("bukkit/paper" in problem, problem) + } +} Index: src/test/kotlin/framework/LogCapture.kt =================================================================== --- src/test/kotlin/framework/LogCapture.kt (revision e0069be60d2f270af6ea985afd8f533ef1d0a42e) +++ src/test/kotlin/framework/LogCapture.kt (revision 0417bb52644f8e8a16a769e119f5ec636b8e675b) @@ -23,9 +23,11 @@ import com.intellij.testFramework.LoggedErrorProcessor import java.util.concurrent.CopyOnWriteArrayList +private const val PLUGIN_CATEGORY = "#com.demonwav.mcdev." + /** - * Collects the errors and warnings logged from any thread until closed. While open, logged errors don't fail the test - * on their own, so check [errors] or call [assertNoErrors]. + * Collects the errors logged from any thread, and the warnings logged by the plugin, until closed. While open, logged + * errors don't fail the test on their own, so check [errors] or call [assertNoErrors]. */ class LogCapture : AutoCloseable { @@ -45,7 +47,9 @@ } override fun processWarn(category: String, message: String, t: Throwable?): Boolean { + if (category.startsWith(PLUGIN_CATEGORY)) { - warnings += describe(category, message, t) + warnings += describe(category, message, t) + } return true } },