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:
Christian Koop 2022-11-16 02:14:45 +01:00
parent f8b3942de2
commit 02330b5ca7
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
2 changed files with 27 additions and 19 deletions

View File

@ -9,36 +9,41 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
class SongodaYamlConfigRoundtripTest {
Path cfg;
private Path testDirectoryPath;
@BeforeEach
void setUp() throws IOException {
Path path = Files.createTempFile("SongodaYamlConfigTest", "yml");
File file = path.toFile();
file.deleteOnExit();
this.cfg = path;
this.testDirectoryPath = Files.createTempDirectory("SongodaCore-YamlConfigRoundtripTest");
this.testDirectoryPath.toFile().deleteOnExit();
}
@AfterEach
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
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" +
"messages:\n" +
" # This message is shown when the 'foo' command succeeds.\n" +
" fooSuccess: Remastered success value\n" +
"# 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);
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" +
"incrementer: 0\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)));
}
}

View File

@ -13,32 +13,35 @@ import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
class LocaleFileManagerTest {
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
void setUp() throws IOException {
this.testDirectory = Files.createTempDirectory("SongodaCore-LocaleFileManagerTest").toFile();
this.testDirectory.deleteOnExit();
this.testDirectoryPath = Files.createTempDirectory("SongodaCore-LocaleFileManagerTest");
this.testDirectoryPath.toFile().deleteOnExit();
}
@AfterEach
void tearDown() throws IOException {
for (File file : Objects.requireNonNull(this.testDirectory.listFiles())) {
Files.deleteIfExists(file.toPath());
try (Stream<Path> paths = Files.list(this.testDirectoryPath)) {
for (Path path : paths.toArray(Path[]::new)) {
Files.deleteIfExists(path);
}
Files.deleteIfExists(this.testDirectory.toPath());
}
Files.deleteIfExists(this.testDirectoryPath);
}
@Test
void downloadMissingTranslations_EmptyTargetDir() throws IOException {
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));
LocaleFileManager localeFileManager = new LocaleFileManager(httpClient, "test");
@ -64,7 +67,7 @@ class LocaleFileManagerTest {
@Test
void downloadMissingTranslations() throws IOException {
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.createFile(new File(plugin.getDataFolder(), "en_US.lang").toPath());