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

@@ -0,0 +1,2 @@
- type: fixed
description: Fix bugs in unit tests which occur on Windows only ([#1927](https://github.com/scm-manager/scm-manager/pull/1927))

View File

@@ -45,7 +45,6 @@ import java.util.Collection;
public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
private static final String SCHEME = ScmTransportProtocol.NAME + "://";
private static final Logger LOG = LoggerFactory.getLogger(AbstractGitPushOrPullCommand.class);
protected GitRepositoryHandler handler;
@@ -100,7 +99,7 @@ public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
}
protected String getRemoteUrl(File directory) {
return SCHEME.concat(directory.getAbsolutePath());
return directory.toURI().toASCIIString();
}
protected String getRemoteUrl(sonia.scm.repository.Repository repository) {

View File

@@ -74,7 +74,7 @@ public class GitPushCommandTest extends AbstractRemoteCommandTestBase
PushResponse response = cmd.push(request);
assertNotNull(response);
assertEquals(2l, response.getChangesetCount());
assertEquals(2L, response.getChangesetCount());
Iterator<RevCommit> commits = incoming.log().call().iterator();

View File

@@ -24,8 +24,9 @@
package sonia.scm.repository;
import org.javahg.Repository;
import com.google.common.collect.ImmutableMap;
import org.javahg.Repository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -57,6 +58,7 @@ class HgRepositoryFactoryTest {
private HgRepositoryFactory factory;
private sonia.scm.repository.Repository heartOfGold;
private Repository repository;
@BeforeEach
void setUpFactory(@TempDir Path directory) {
@@ -68,9 +70,16 @@ class HgRepositoryFactoryTest {
heartOfGold = createRepository();
}
@AfterEach
void tearDown() {
if (repository != null) {
repository.close();
}
}
@Test
void shouldOpenRepositoryForRead() {
Repository repository = factory.openForRead(heartOfGold);
repository = factory.openForRead(heartOfGold);
assertThat(repository).isNotNull();
verify(environmentBuilder).read(heartOfGold);
@@ -78,7 +87,7 @@ class HgRepositoryFactoryTest {
@Test
void shouldOpenRepositoryForWrite() {
Repository repository = factory.openForWrite(heartOfGold);
repository = factory.openForWrite(heartOfGold);
assertThat(repository).isNotNull();
verify(environmentBuilder).write(heartOfGold);
@@ -88,7 +97,7 @@ class HgRepositoryFactoryTest {
void shouldFallbackToUTF8OnUnknownEncoding() {
handler.getConfig().setEncoding("unknown");
Repository repository = factory.openForRead(heartOfGold);
repository = factory.openForRead(heartOfGold);
assertThat(repository.getBaseRepository().getConfiguration().getEncoding()).isEqualTo(StandardCharsets.UTF_8);
}
@@ -97,7 +106,7 @@ class HgRepositoryFactoryTest {
void shouldSetPendingChangesetState() {
when(hookEnvironment.isPending()).thenReturn(true);
Repository repository = factory.openForRead(heartOfGold);
repository = factory.openForRead(heartOfGold);
assertThat(repository.getBaseRepository().getConfiguration().isEnablePendingChangesets())
.isTrue();
@@ -107,7 +116,7 @@ class HgRepositoryFactoryTest {
void shouldPassEnvironment() {
when(environmentBuilder.read(heartOfGold)).thenReturn(ImmutableMap.of("spaceship", "heartOfGold"));
Repository repository = factory.openForRead(heartOfGold);
repository = factory.openForRead(heartOfGold);
assertThat(repository.getBaseRepository().getConfiguration().getEnvironment())
.containsEntry("spaceship", "heartOfGold");
@@ -122,5 +131,4 @@ class HgRepositoryFactoryTest {
return heartOfGold;
}
}

View File

@@ -29,6 +29,8 @@ import org.assertj.core.util.Strings;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Nested;
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.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -70,6 +72,7 @@ class HgVerifierTest {
}
@Test
@DisabledOnOs(OS.WINDOWS)
void shouldReturnFalseIfFileIsNotExecutable(@TempDir Path directory) throws IOException {
Path hg = directory.resolve("hg");
Files.createFile(hg);
@@ -115,7 +118,7 @@ class HgVerifierTest {
}
@ParameterizedTest
@ValueSource(strings = { "3-2-1", "x.y.z" })
@ValueSource(strings = {"3-2-1", "x.y.z"})
void shouldReturnInvalidVersions(String version, @TempDir Path directory) throws IOException {
HgVerifier verifier = new HgVerifier(hg -> version);

View File

@@ -40,6 +40,7 @@ import sonia.scm.config.ScmConfiguration;
import sonia.scm.net.GlobalProxyConfiguration;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.MirrorCommandResult;
import sonia.scm.util.SystemUtil;
import javax.net.ssl.X509TrustManager;
import java.io.File;
@@ -115,7 +116,7 @@ public class SvnMirrorCommandTest extends AbstractSvnCommandTestBase {
private MirrorCommandRequest createRequest(File source) {
MirrorCommandRequest request = new MirrorCommandRequest();
request.setSourceUrl("file://" + source.getAbsolutePath());
request.setSourceUrl("file://" + (SystemUtil.isWindows() ? "/" : "") + source.getAbsolutePath());
return request;
}

View File

@@ -34,7 +34,6 @@ import sonia.scm.store.StoreEntryMetaData;
import sonia.scm.store.StoreType;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -51,7 +50,7 @@ public class TarArchiveRepositoryStoreImporter {
try (TarArchiveInputStream tais = new NoneClosingTarArchiveInputStream(inputStream)) {
ArchiveEntry entry = tais.getNextEntry();
while (entry != null) {
String[] entryPathParts = entry.getName().split(File.separator);
String[] entryPathParts = entry.getName().split("/");
validateStorePath(repository, entryPathParts);
importStoreByType(repository, tais, entryPathParts, logger);
entry = tais.getNextEntry();

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");