Fix SongodaYamlConfig not creating parent directory when saving
Caused an IOException if the PluginDataDir didn't exist yet.
This commit is contained in:
parent
e7e3c3d21d
commit
b0f006aed0
|
@ -28,7 +28,7 @@ import java.util.logging.Logger;
|
||||||
public class SongodaYamlConfig extends YamlConfiguration {
|
public class SongodaYamlConfig extends YamlConfiguration {
|
||||||
protected final String cannotCreateBackupCopyExceptionPrefix = "Unable to create backup copy of config file: ";
|
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;
|
protected final @NotNull Logger logger;
|
||||||
|
|
||||||
private int targetVersion;
|
private int targetVersion;
|
||||||
|
@ -129,6 +129,8 @@ public class SongodaYamlConfig extends YamlConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() throws IOException {
|
public void save() throws IOException {
|
||||||
|
Files.createDirectories(this.file.toPath().getParent());
|
||||||
|
|
||||||
try (Writer writer = new FileWriter(this.file)) {
|
try (Writer writer = new FileWriter(this.file)) {
|
||||||
super.save(writer);
|
super.save(writer);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -60,6 +60,17 @@ class SongodaYamlConfigTest {
|
||||||
assertEquals("test-key: bar\n", new String(Files.readAllBytes(this.cfg)));
|
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
|
@Test
|
||||||
void testWithVersion() throws IOException {
|
void testWithVersion() throws IOException {
|
||||||
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());
|
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());
|
||||||
|
|
Loading…
Reference in New Issue