mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-27 04:25:19 +01:00
Improve temporary file deletion in YamlConfig and FileManager tests
The SongodaYamlConfig might create a backup config file when upgrading a configuration into a newer version. This file would not get deleted in the old implementation
This commit is contained in:
parent
f8b3942de2
commit
02330b5ca7
@ -9,36 +9,41 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class SongodaYamlConfigRoundtripTest {
|
class SongodaYamlConfigRoundtripTest {
|
||||||
Path cfg;
|
private Path testDirectoryPath;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws IOException {
|
void setUp() throws IOException {
|
||||||
Path path = Files.createTempFile("SongodaYamlConfigTest", "yml");
|
this.testDirectoryPath = Files.createTempDirectory("SongodaCore-YamlConfigRoundtripTest");
|
||||||
File file = path.toFile();
|
this.testDirectoryPath.toFile().deleteOnExit();
|
||||||
file.deleteOnExit();
|
|
||||||
|
|
||||||
this.cfg = path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
void tearDown() throws IOException {
|
void tearDown() throws IOException {
|
||||||
Files.deleteIfExists(this.cfg);
|
try (Stream<Path> paths = Files.list(this.testDirectoryPath)) {
|
||||||
|
for (Path path : paths.toArray(Path[]::new)) {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Files.deleteIfExists(this.testDirectoryPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void roundtripTest() throws IOException {
|
void roundtripTest() throws IOException {
|
||||||
Files.write(this.cfg, ("# Don't touch this – it's used to track the version of the config.\n" +
|
Path testFilePath = this.testDirectoryPath.resolve("config.yml");
|
||||||
|
|
||||||
|
Files.write(testFilePath, ("# Don't touch this – it's used to track the version of the config.\n" +
|
||||||
"version: 1\n" +
|
"version: 1\n" +
|
||||||
"messages:\n" +
|
"messages:\n" +
|
||||||
" # This message is shown when the 'foo' command succeeds.\n" +
|
" # This message is shown when the 'foo' command succeeds.\n" +
|
||||||
" fooSuccess: Remastered success value\n" +
|
" fooSuccess: Remastered success value\n" +
|
||||||
"# This is the range of the 'foo' command\n").getBytes());
|
"# This is the range of the 'foo' command\n").getBytes());
|
||||||
|
|
||||||
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile())
|
SongodaYamlConfig cfg = new SongodaYamlConfig(testFilePath.toFile())
|
||||||
.withVersion(3);
|
.withVersion(3);
|
||||||
|
|
||||||
ConfigEntry cmdFooSuccess = cfg.createEntry("command.foo.success", "Default success value")
|
ConfigEntry cmdFooSuccess = cfg.createEntry("command.foo.success", "Default success value")
|
||||||
@ -94,6 +99,6 @@ class SongodaYamlConfigRoundtripTest {
|
|||||||
"# This is the incrementer of the 'foo' command\n" +
|
"# This is the incrementer of the 'foo' command\n" +
|
||||||
"incrementer: 0\n" +
|
"incrementer: 0\n" +
|
||||||
"# This is the entry without an upgrade step\n" +
|
"# This is the entry without an upgrade step\n" +
|
||||||
"entryWithoutUpgradeStep: Default value\n", new String(Files.readAllBytes(this.cfg)));
|
"entryWithoutUpgradeStep: Default value\n", new String(Files.readAllBytes(testFilePath)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,32 +13,35 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
class LocaleFileManagerTest {
|
class LocaleFileManagerTest {
|
||||||
private final byte[] validIndexFile = ("# This is a comment\n\nen_US.lang\nen.yml\nde.txt\n").getBytes(StandardCharsets.UTF_8);
|
private final byte[] validIndexFile = ("# This is a comment\n\nen_US.lang\nen.yml\nde.txt\n").getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
private File testDirectory;
|
private Path testDirectoryPath;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws IOException {
|
void setUp() throws IOException {
|
||||||
this.testDirectory = Files.createTempDirectory("SongodaCore-LocaleFileManagerTest").toFile();
|
this.testDirectoryPath = Files.createTempDirectory("SongodaCore-LocaleFileManagerTest");
|
||||||
this.testDirectory.deleteOnExit();
|
this.testDirectoryPath.toFile().deleteOnExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
void tearDown() throws IOException {
|
void tearDown() throws IOException {
|
||||||
for (File file : Objects.requireNonNull(this.testDirectory.listFiles())) {
|
try (Stream<Path> paths = Files.list(this.testDirectoryPath)) {
|
||||||
Files.deleteIfExists(file.toPath());
|
for (Path path : paths.toArray(Path[]::new)) {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
}
|
}
|
||||||
Files.deleteIfExists(this.testDirectory.toPath());
|
}
|
||||||
|
Files.deleteIfExists(this.testDirectoryPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void downloadMissingTranslations_EmptyTargetDir() throws IOException {
|
void downloadMissingTranslations_EmptyTargetDir() throws IOException {
|
||||||
Plugin plugin = Mockito.mock(Plugin.class);
|
Plugin plugin = Mockito.mock(Plugin.class);
|
||||||
Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectory);
|
Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectoryPath.toFile());
|
||||||
|
|
||||||
MockHttpClient httpClient = new MockHttpClient(new MockHttpResponse(200, this.validIndexFile));
|
MockHttpClient httpClient = new MockHttpClient(new MockHttpResponse(200, this.validIndexFile));
|
||||||
LocaleFileManager localeFileManager = new LocaleFileManager(httpClient, "test");
|
LocaleFileManager localeFileManager = new LocaleFileManager(httpClient, "test");
|
||||||
@ -64,7 +67,7 @@ class LocaleFileManagerTest {
|
|||||||
@Test
|
@Test
|
||||||
void downloadMissingTranslations() throws IOException {
|
void downloadMissingTranslations() throws IOException {
|
||||||
Plugin plugin = Mockito.mock(Plugin.class);
|
Plugin plugin = Mockito.mock(Plugin.class);
|
||||||
Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectory);
|
Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectoryPath.toFile());
|
||||||
|
|
||||||
Files.createDirectories(plugin.getDataFolder().toPath());
|
Files.createDirectories(plugin.getDataFolder().toPath());
|
||||||
Files.createFile(new File(plugin.getDataFolder(), "en_US.lang").toPath());
|
Files.createFile(new File(plugin.getDataFolder(), "en_US.lang").toPath());
|
||||||
|
Loading…
Reference in New Issue
Block a user