From e99759d701b0a4a9e27cf67d25ea48044d628e62 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:45:40 -0400 Subject: [PATCH] Fix test --- .../geyser/configuration/ConfigLoader.java | 25 +++++++++--- .../configuration/ConfigLoaderTest.java | 39 +++++++++++-------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/configuration/ConfigLoader.java b/core/src/main/java/org/geysermc/geyser/configuration/ConfigLoader.java index 634d827cc..2639672af 100644 --- a/core/src/main/java/org/geysermc/geyser/configuration/ConfigLoader.java +++ b/core/src/main/java/org/geysermc/geyser/configuration/ConfigLoader.java @@ -25,6 +25,7 @@ package org.geysermc.geyser.configuration; +import com.google.common.annotations.VisibleForTesting; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.common.returnsreceiver.qual.This; import org.geysermc.geyser.Constants; @@ -75,7 +76,10 @@ public final class ConfigLoader { -------------------------------- """; - private final GeyserBootstrap bootstrap; + /** + * Only nullable for testing. + */ + private final @Nullable GeyserBootstrap bootstrap; private @Nullable Consumer transformer; private File configFile; @@ -84,6 +88,12 @@ public final class ConfigLoader { configFile = new File(bootstrap.getConfigFolder().toFile(), "config.yml"); } + @VisibleForTesting + ConfigLoader(File file) { + this.bootstrap = null; + configFile = file; + } + /** * Creates the directory as indicated by {@link GeyserBootstrap#getConfigFolder()} */ @@ -236,7 +246,9 @@ public final class ConfigLoader { config.advanced(advancedConfig); - bootstrap.getGeyserLogger().setDebug(config.debugMode()); + if (this.bootstrap != null) { // Null for testing only. + this.bootstrap.getGeyserLogger().setDebug(config.debugMode()); + } return config; } @@ -292,9 +304,12 @@ public final class ConfigLoader { .file(file) .indent(2) .nodeStyle(NodeStyle.BLOCK) - .defaultOptions(options -> InterfaceDefaultOptions.addTo(options, builder -> - builder.addProcessor(ExcludePlatform.class, excludePlatform(bootstrap.platformType().platformName())) - .addProcessor(PluginSpecific.class, integrationSpecific(bootstrap.platformType() != PlatformType.STANDALONE))) + .defaultOptions(options -> InterfaceDefaultOptions.addTo(options, builder -> { + if (this.bootstrap != null) { // Testing only. + builder.addProcessor(ExcludePlatform.class, excludePlatform(bootstrap.platformType().platformName())) + .addProcessor(PluginSpecific.class, integrationSpecific(bootstrap.platformType() != PlatformType.STANDALONE)); + } + }) .shouldCopyDefaults(false) // If we use ConfigurationNode#get(type, default), do not write the default back to the node. .header(header) .serializers(builder -> builder.register(new LowercaseEnumSerializer()))) diff --git a/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java b/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java index d2f0cbbf3..098dce7dc 100644 --- a/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java +++ b/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java @@ -31,7 +31,12 @@ import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.util.CheckedConsumer; import java.io.File; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ConfigLoaderTest { @@ -49,23 +54,23 @@ public class ConfigLoaderTest { File file = tempDirectory.resolve("config.yml").toFile(); // Sorry Konicai... -// forAllConfigs(type -> { -// ConfigLoader.load(file, type, n -> this.config1 = n.copy()); -// long initialModification = file.lastModified(); -// assertTrue(file.exists()); // should have been created -// List firstContents = Files.readAllLines(file.toPath()); -// -// ConfigLoader.load(file, type, n -> this.config2 = n.copy()); -// List secondContents = Files.readAllLines(file.toPath()); -// -// assertEquals(initialModification, file.lastModified()); // should not have been touched -// assertEquals(firstContents, secondContents); -// -// // Must ignore this, as when the config is read back, the header is interpreted as a comment on the first node in the map -// config1.node("java").comment(null); -// config2.node("java").comment(null); -// assertEquals(config1, config2); -// }); + forAllConfigs(type -> { + new ConfigLoader(file).transformer(n -> this.config1 = n.copy()).load(type); + long initialModification = file.lastModified(); + assertTrue(file.exists()); // should have been created + List firstContents = Files.readAllLines(file.toPath()); + + new ConfigLoader(file).transformer(n -> this.config2 = n.copy()).load(type); + List secondContents = Files.readAllLines(file.toPath()); + + assertEquals(initialModification, file.lastModified()); // should not have been touched + assertEquals(firstContents, secondContents); + + // Must ignore this, as when the config is read back, the header is interpreted as a comment on the first node in the map + config1.node("java").comment(null); + config2.node("java").comment(null); + assertEquals(config1, config2); + }); } void forAllConfigs(CheckedConsumer, Exception> consumer) throws Exception {