Fix SongodaYamlConfig not creating parent directory when saving

Caused an IOException if the PluginDataDir didn't exist yet.
This commit is contained in:
Christian Koop 2022-06-26 13:26:13 +02:00
parent e7e3c3d21d
commit b0f006aed0
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
2 changed files with 14 additions and 1 deletions

View File

@ -28,7 +28,7 @@ import java.util.logging.Logger;
public class SongodaYamlConfig extends YamlConfiguration {
protected final String cannotCreateBackupCopyExceptionPrefix = "Unable to create backup copy of config file: ";
protected final @NotNull File file;
public final @NotNull File file;
protected final @NotNull Logger logger;
private int targetVersion;
@ -129,6 +129,8 @@ public class SongodaYamlConfig extends YamlConfiguration {
}
public void save() throws IOException {
Files.createDirectories(this.file.toPath().getParent());
try (Writer writer = new FileWriter(this.file)) {
super.save(writer);
} catch (IOException e) {

View File

@ -60,6 +60,17 @@ class SongodaYamlConfigTest {
assertEquals("test-key: bar\n", new String(Files.readAllBytes(this.cfg)));
}
@Test
void testSaveToNonExistingSubDirectory() throws IOException {
File configFile = new File(this.tmpDir.toFile(), "testSaveToNonExistingSubDirectory/config.yml");
SongodaYamlConfig cfg = new SongodaYamlConfig(configFile);
cfg.set("test-key", "bar");
cfg.save();
assertEquals("test-key: bar\n", new String(Files.readAllBytes(configFile.toPath())));
}
@Test
void testWithVersion() throws IOException {
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());