Fix bugs in unit tests which occur on Windows only (#1927)

On Windows it is much more necessary to close files to avoid locked files which prevent deleting a temporary file or folder. Also paths need to be handled differently because of the drive letter and colon appearing on Windows.
This commit is contained in:
Matthias Thieroff
2022-01-20 08:48:13 +01:00
committed by GitHub
parent 63ec4e6172
commit 0f01bb82c6
10 changed files with 40 additions and 23 deletions

View File

@@ -160,11 +160,12 @@ class FullScmRepositoryImporterTest {
void shouldNotImportRepositoryIfFileNotExists(@TempDir Path temp) throws IOException {
Path emptyFile = temp.resolve("empty");
Files.createFile(emptyFile);
FileInputStream inputStream = new FileInputStream(emptyFile.toFile());
assertThrows(
ImportFailedException.class,
() -> fullImporter.importFromStream(REPOSITORY, inputStream, "")
);
try (FileInputStream inputStream = new FileInputStream(emptyFile.toFile())) {
assertThrows(
ImportFailedException.class,
() -> fullImporter.importFromStream(REPOSITORY, inputStream, "")
);
}
}
@Test

View File

@@ -24,6 +24,8 @@
package sonia.scm.lifecycle;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
@@ -61,6 +63,7 @@ class DefaultRestarterTest {
assertThat(restarter.isSupported()).isFalse();
}
@DisabledOnOs(OS.WINDOWS)
@Test
void shouldReturnTrueIfRestartStrategyIsAvailable() {
DefaultRestarter restarter = new DefaultRestarter();

View File

@@ -50,6 +50,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
@@ -60,6 +61,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
@@ -127,11 +129,10 @@ class I18nServletTest {
private Cache<String, JsonNode> cache;
@Test
void shouldNotHaveInvalidPluginsJsonFiles() throws IOException {
String path = getClass().getClassLoader().getResource("locales/en/plugins.json").getPath();
assertThat(path).isNotNull();
void shouldNotHaveInvalidPluginsJsonFiles() throws Exception {
URI uri = Objects.requireNonNull(getClass().getClassLoader().getResource("locales/en/plugins.json")).toURI();
Path filePath = Paths.get(path);
Path filePath = Paths.get(uri);
Path translationRootPath = filePath.getParent().getParent();
assertThat(translationRootPath).isDirectoryContaining("glob:**/en");