-
-
Notifications
You must be signed in to change notification settings - Fork 72
Rework plugin config system #2 #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af9c53d
A few fixes
DerEchtePilz 456c5a4
Add some tests
DerEchtePilz 36c6b4b
Pass a YamlConfigurationLoader into VelocityConfigurationAdapter#crea…
DerEchtePilz 54437c3
Generate default config correctly again
DerEchtePilz 95a831d
Another test
DerEchtePilz f50a549
Extract saveDefaultConfig to ConfigurationAdapter
DerEchtePilz 51eddbd
Update config test validation methods. Add config modules to code cov…
DerEchtePilz c07b4b1
Code review
DerEchtePilz f21647e
Pass config file to the ConfigurationAdapter impls
DerEchtePilz ed82a36
createDummyInstance -> createMinimalInstance
DerEchtePilz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
...dapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/ConfigGenerationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package dev.jorel.commandapi.test; | ||
|
||
import dev.jorel.commandapi.config.BukkitConfigurationAdapter; | ||
import dev.jorel.commandapi.config.CommentedConfigOption; | ||
import dev.jorel.commandapi.config.CommentedSection; | ||
import dev.jorel.commandapi.config.ConfigGenerator; | ||
import dev.jorel.commandapi.config.ConfigurationAdapter; | ||
import dev.jorel.commandapi.config.DefaultBukkitConfig; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ConfigGenerationTests { | ||
|
||
private CommentedConfigOption<Boolean> silentLogs; | ||
private CommentedConfigOption<Boolean> verboseOutputs; | ||
private CommentedConfigOption<String> missingExecutorImplementation; | ||
|
||
private CommentedSection messages; | ||
|
||
private ConfigGenerator generator; | ||
private DefaultBukkitConfig bukkitConfig; | ||
private BukkitConfigurationAdapter adapter; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
silentLogs = new CommentedConfigOption<>(new String[]{ | ||
"Silent logs (default: false)", | ||
"If \"true\", turns off all logging from the CommandAPI, except for errors." | ||
}, false); | ||
verboseOutputs = new CommentedConfigOption<>(new String[]{ | ||
"Verbose outputs (default: false)", | ||
"If \"true\", outputs command registration and unregistration logs in the console" | ||
}, false); | ||
missingExecutorImplementation = new CommentedConfigOption<>(new String[]{ | ||
"Missing executor implementation (default: \"This command has no implementations for %s\")", | ||
"The message to display to senders when a command has no executor. Available", | ||
"parameters are:", | ||
" %s - the executor class (lowercase)", | ||
" %S - the executor class (normal case)" | ||
}, "This command has no implementations for %s"); | ||
|
||
messages = new CommentedSection(new String[]{ | ||
"Messages", | ||
"Controls messages that the CommandAPI displays to players" | ||
}); | ||
|
||
Map<String, CommentedConfigOption<?>> options = new LinkedHashMap<>(); | ||
options.put("silent-logs", silentLogs); | ||
options.put("verbose-outputs", verboseOutputs); | ||
options.put("messages.missing-executor-implementation", missingExecutorImplementation); | ||
|
||
Map<String, CommentedSection> sections = new LinkedHashMap<>(); | ||
sections.put("messages", messages); | ||
|
||
ConfigurationAdapter<YamlConfiguration, DefaultBukkitConfig> adapter = new BukkitConfigurationAdapter(new YamlConfiguration()); | ||
bukkitConfig = DefaultBukkitConfig.create(options, sections); | ||
generator = ConfigGenerator.createNew(bukkitConfig); | ||
this.adapter = (BukkitConfigurationAdapter) generator.generate(adapter); | ||
} | ||
|
||
@AfterEach | ||
public void reset() { | ||
this.silentLogs = null; | ||
this.verboseOutputs = null; | ||
this.missingExecutorImplementation = null; | ||
this.messages = null; | ||
this.generator = null; | ||
this.bukkitConfig = null; | ||
this.adapter = null; | ||
} | ||
|
||
// Test methods | ||
private void validateConfigOptions(Set<String> options, BukkitConfigurationAdapter adapter) { | ||
for (String option : options) { | ||
assertTrue(adapter.contains(option), "Config option '" + option + "' does not exist!"); | ||
} | ||
} | ||
|
||
private void validateConfigOptionComments(Map<String, String[]> comments, BukkitConfigurationAdapter adapter) { | ||
for (String option : comments.keySet()) { | ||
String[] expectedComment = comments.get(option); | ||
String[] generatedComment = adapter.getComment(option); | ||
assertArrayEquals(expectedComment, generatedComment, "Config option comment for option '" + option + "' does not exist or was incorrect!"); | ||
} | ||
} | ||
|
||
private void validateConfigOptionsAbsent(Set<String> options, BukkitConfigurationAdapter adapter) { | ||
for (String option : options) { | ||
assertFalse(adapter.contains(option), "Config option '" + option + "' does still exist!"); | ||
} | ||
} | ||
|
||
private void validateSections(List<String> sections, String expectedOption, YamlConfiguration config) { | ||
ConfigurationSection root = config.getConfigurationSection(sections.get(0)); | ||
assertNotNull(root, "Section '" + sections.get(0) + "' does not exist!"); | ||
|
||
for (int i = 1; i < sections.size(); i++) { | ||
root = root.getConfigurationSection(sections.get(i)); | ||
assertNotNull(root, "Section '" + sections.get(i) + "' does not exist!"); | ||
} | ||
Object expectedValue = root.get(expectedOption); | ||
assertNotNull(expectedValue, "Expected option '" + expectedOption + "' was not found in section '" + root.getName() + "'!"); | ||
} | ||
|
||
@Test | ||
void testDefaultConfigOptionGeneration() { | ||
validateConfigOptions(Set.of( | ||
"silent-logs", "verbose-outputs", "messages.missing-executor-implementation" | ||
), adapter); | ||
} | ||
|
||
@Test | ||
void testDefaultConfigOptionCommentGeneration() { | ||
validateConfigOptionComments(Map.ofEntries( | ||
Map.entry("silent-logs", silentLogs.comment()), | ||
Map.entry("verbose-outputs", verboseOutputs.comment()), | ||
Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()), | ||
Map.entry("messages", messages.comment()) | ||
), adapter); | ||
} | ||
|
||
@Test | ||
void testConfigOptionAddition() { | ||
CommentedConfigOption<Boolean> createDispatcherJson = new CommentedConfigOption<>(new String[] { | ||
"Create dispatcher JSON (default: false)", | ||
"If \"true\", the CommandAPI creates a command_registration.json file showing the", | ||
"mapping of registered commands. This is designed to be used by developers -", | ||
"setting this to \"false\" will improve command registration performance." | ||
}, false); | ||
|
||
bukkitConfig.getAllOptions().put("create-dispatcher-json", createDispatcherJson); | ||
< 8000 td id="diff-21dd1ae62f0e252febd3dc5b0a5ce9ae1d559924ffbf21fada0cbb50be47d5a9R147" data-line-number="147" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum"> | generator = ConfigGenerator.createNew(bukkitConfig); | |
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); | ||
|
||
validateConfigOptions(Set.of( | ||
"silent-logs", "verbose-outputs", "messages.missing-executor-implementation", "create-dispatcher-json" | ||
), updatedAdapter); | ||
|
||
validateConfigOptionComments(Map.ofEntries( | ||
Map.entry("silent-logs", silentLogs.comment()), | ||
Map.entry("verbose-outputs", verboseOutputs.comment()), | ||
Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()), | ||
Map.entry("create-dispatcher-json", createDispatcherJson.comment()), | ||
Map.entry("messages", messages.comment()) | ||
), updatedAdapter); | ||
} | ||
|
||
@Test | ||
void testConfigOptionDeletion() { | ||
bukkitConfig.getAllOptions().remove("silent-logs"); | ||
generator = ConfigGenerator.createNew(bukkitConfig); | ||
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); | ||
|
||
validateConfigOptionsAbsent(Set.of("silent-logs"), updatedAdapter); | ||
} | ||
|
||
@Test | ||
void testConfigOptionCommentUpdate() { | ||
silentLogs = new CommentedConfigOption<>(new String[] { | ||
"Defines if silent logs should happen" | ||
}, false); | ||
|
||
bukkitConfig.getAllOptions().put("silent-logs", silentLogs); | ||
generator = ConfigGenerator.createNew(bukkitConfig); | ||
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); | ||
|
||
validateConfigOptionComments(Map.ofEntries( | ||
Map.entry("silent-logs", silentLogs.comment()) | ||
), updatedAdapter); | ||
} | ||
|
||
@Test | ||
void testNestedSections() { | ||
CommentedConfigOption<Boolean> subSubOption = new CommentedConfigOption<>(new String[0], false); | ||
|
||
bukkitConfig.getAllOptions().put("root.nested.option", subSubOption); | ||
generator = ConfigGenerator.createNew(bukkitConfig); | ||
BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); | ||
|
||
validateSections(List.of("root", "nested"), "option", updatedAdapter.config()); | ||
} | ||
|
||
@Test | ||
void testConfigUpdateNotNeeded() { | ||
assertNull(generator.generate(adapter)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.