diff --git a/README.md b/README.md index 33b04d0..adf8794 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Please open a bug report [here](https://github.com/Dans-Plugins/FlyCommand/issue ## Testing -`mvn clean package` compiles the plugin and runs its test suite. The suite checks the `plugin.yml` packed into the jar: that `main` names the plugin class, that the build filled in the version, and that the `Fly` command and the `FlyCommand.fly` permission are declared. It also runs the `/fly` handler against stand-in senders, checking the toggle and its confirmations (including when the command is typed as `/flycommand:fly`), the permission-denied alert, and the console alert. The [Build](.github/workflows/build.yml) workflow runs the same command on every push to `main` and every pull request against it, then the Bukkit API check described under [Supported Minecraft Versions](#supported-minecraft-versions), which `mvn clean package` does not run. +`mvn clean package` compiles the plugin and runs its test suite. The suite reads the `plugin.yml` packed into the jar with Bukkit's `PluginDescriptionFile`, the parser a server uses, and checks that `main` names the plugin class, that the build filled in the version, that the name and `api-version` are `FlyCommand` and `1.13`, and that the `Fly` command is declared. The `op` default of the `FlyCommand.fly` permission is read as plain YAML instead, because that parser needs a running server to build a permission. The suite also runs the `/fly` handler against stand-in senders, checking the toggle and its confirmations (including when the command is typed as `/flycommand:fly`), the permission-denied alert, and the console alert. The [Build](.github/workflows/build.yml) workflow runs the same command on every push to `main` and every pull request against it, then the Bukkit API check described under [Supported Minecraft Versions](#supported-minecraft-versions), which `mvn clean package` does not run. The tests do not start a server, so the plugin loading and `/fly` being registered are still verified by hand on a Spigot or Paper server, as described under [Manual Validation](#manual-validation-on-a-test-server). diff --git a/src/test/java/me/Daniel/FlyCommand/PluginManifestTest.java b/src/test/java/me/Daniel/FlyCommand/PluginManifestTest.java index 684f7a6..f039c61 100644 --- a/src/test/java/me/Daniel/FlyCommand/PluginManifestTest.java +++ b/src/test/java/me/Daniel/FlyCommand/PluginManifestTest.java @@ -1,6 +1,7 @@ package me.Daniel.FlyCommand; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import org.junit.jupiter.api.Test; @@ -11,37 +12,64 @@ import static org.junit.jupiter.api.Assertions.*; /** - * Checks the plugin.yml that ends up in the jar: a wrong {@code main} or an - * unfiltered version would stop the plugin loading, and nothing else in this - * repository would notice before a server did. + * Checks the plugin.yml that ends up in the jar by reading it with + * {@link PluginDescriptionFile}, the parser a server runs before it loads a + * plugin: a manifest it rejects, a wrong {@code main} or an unfiltered version + * would stop the plugin loading, and nothing else in this repository would + * notice before a server did. */ class PluginManifestTest { - private YamlConfiguration manifest() throws Exception { - try (InputStream in = getClass().getClassLoader().getResourceAsStream("plugin.yml")) { - assertNotNull(in, "plugin.yml is not on the classpath"); - return YamlConfiguration.loadConfiguration(new InputStreamReader(in, StandardCharsets.UTF_8)); + private InputStream manifestFile() { + InputStream in = getClass().getClassLoader().getResourceAsStream("plugin.yml"); + assertNotNull(in, "plugin.yml is not on the classpath"); + return in; + } + + private PluginDescriptionFile manifest() throws Exception { + try (InputStream in = manifestFile()) { + return new PluginDescriptionFile(in); } } @Test void mainNamesThePluginClass() throws Exception { - Class main = Class.forName(manifest().getString("main")); + Class main = Class.forName(manifest().getMain()); assertTrue(JavaPlugin.class.isAssignableFrom(main)); } @Test void versionIsFilledInByTheBuild() throws Exception { - String version = manifest().getString("version"); - assertNotNull(version); + String version = manifest().getVersion(); assertFalse(version.contains("${"), "plugin.yml version was not filtered: " + version); } + /** The name is also the prefix of /flycommand:fly, which the docs promise always reaches this plugin. */ + @Test + void nameIsTheOneTheDocsUse() throws Exception { + assertEquals("FlyCommand", manifest().getName()); + } + + @Test + void apiVersionIsTheOneTheDocsState() throws Exception { + assertEquals("1.13", manifest().getAPIVersion()); + } + + @Test + void declaresTheFlyCommand() throws Exception { + assertTrue(manifest().getCommands().containsKey("Fly")); + } + + /** + * PluginDescriptionFile cannot be asked for this: building a Permission + * needs a running server, so the block is read back as plain YAML. + */ @Test - void declaresTheFlyCommandAndItsPermission() throws Exception { - YamlConfiguration yml = manifest(); - assertTrue(yml.isConfigurationSection("commands.Fly")); - // Bukkit splits the node name on its dot when loading, which is also how it reads it back. - assertEquals("op", yml.getString("permissions.FlyCommand.fly.default")); + void declaresTheFlyPermission() throws Exception { + try (InputStream in = manifestFile()) { + YamlConfiguration yml = YamlConfiguration.loadConfiguration(new InputStreamReader(in, StandardCharsets.UTF_8)); + // Bukkit splits the node name on its dot when loading, which is also how it reads it back. + assertEquals("op", yml.getString("permissions.FlyCommand.fly.default")); + } } }