allow to override scm version to simplify testing

This commit is contained in:
Sebastian Sdorra
2020-02-05 11:04:18 +01:00
parent 7f471168b6
commit 53dc34f7b4
2 changed files with 66 additions and 23 deletions

View File

@@ -36,6 +36,7 @@ package sonia.scm;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------
@@ -57,7 +58,13 @@ public class BasicContextProvider implements SCMContextProvider
{
/** Default version {@link String} */
public static final String DEFAULT_VERSION = "unknown";
public static final String VERSION_DEFAULT = "unknown";
/**
* System property to override scm-manager version.
* <b>Note</b>: This should only be used for testing and never in production.
**/
public static final String VERSION_PROPERTY = "sonia.scm.version.override";
/** Default name of the SCM-Manager base directory */
public static final String DIRECTORY_DEFAULT = ".scm";
@@ -95,7 +102,7 @@ public class BasicContextProvider implements SCMContextProvider
try
{
baseDirectory = findBaseDirectory();
version = loadVersion();
version = determineVersion();
stage = loadProjectStage();
}
catch (Exception ex)
@@ -157,7 +164,7 @@ public class BasicContextProvider implements SCMContextProvider
/**
* Returns the version of the SCM-Manager. If the version is not set, the
* {@link #DEFAULT_VERSION} is returned.
* {@link #VERSION_DEFAULT} is returned.
*
*
* @return the version of the SCM-Manager
@@ -257,6 +264,14 @@ public class BasicContextProvider implements SCMContextProvider
return s;
}
private String determineVersion() {
String version = System.getProperty(VERSION_PROPERTY);
if (Strings.isNullOrEmpty(version)) {
version = loadVersion();
}
return version;
}
/**
* Loads the version of the SCM-Manager from maven properties file.
*
@@ -295,7 +310,7 @@ public class BasicContextProvider implements SCMContextProvider
}
}
return properties.getProperty(MAVEN_PROPERTY_VERSION, DEFAULT_VERSION);
return properties.getProperty(MAVEN_PROPERTY_VERSION, VERSION_DEFAULT);
}
//~--- get methods ----------------------------------------------------------

View File

@@ -1,6 +1,7 @@
package sonia.scm;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
@@ -13,32 +14,59 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(TempDirectory.class)
class BasicContextProviderTest {
private Path baseDirectory;
@Nested
class VersionTests {
private BasicContextProvider context;
@Test
void shouldReturnVersionFromSystemProperty() {
System.setProperty(BasicContextProvider.VERSION_PROPERTY, "3.0.0");
try {
SCMContextProvider context = new BasicContextProvider();
assertThat(context.getVersion()).isEqualTo("3.0.0");
} finally {
System.clearProperty(BasicContextProvider.VERSION_PROPERTY);
}
}
@Test
void shouldReturnDefaultVersion() {
SCMContextProvider context = new BasicContextProvider();
assertThat(context.getVersion()).isEqualTo(BasicContextProvider.VERSION_DEFAULT);
}
@BeforeEach
void setUpContext(@TempDirectory.TempDir Path baseDirectory) {
this.baseDirectory = baseDirectory;
context = new BasicContextProvider(baseDirectory.toFile(), "x.y.z", Stage.PRODUCTION);
}
@Test
void shouldReturnAbsolutePathAsIs(@TempDirectory.TempDir Path path) {
Path absolutePath = path.toAbsolutePath();
Path resolved = context.resolve(absolutePath);
@Nested
class PathTests {
assertThat(resolved).isSameAs(absolutePath);
}
private Path baseDirectory;
@Test
void shouldResolveRelatePath() {
Path path = Paths.get("repos", "42");
Path resolved = context.resolve(path);
private BasicContextProvider context;
@BeforeEach
void setUpContext(@TempDirectory.TempDir Path baseDirectory) {
this.baseDirectory = baseDirectory;
context = new BasicContextProvider(baseDirectory.toFile(), "x.y.z", Stage.PRODUCTION);
}
@Test
void shouldReturnAbsolutePathAsIs(@TempDirectory.TempDir Path path) {
Path absolutePath = path.toAbsolutePath();
Path resolved = context.resolve(absolutePath);
assertThat(resolved).isSameAs(absolutePath);
}
@Test
void shouldResolveRelatePath() {
Path path = Paths.get("repos", "42");
Path resolved = context.resolve(path);
assertThat(resolved).isAbsolute();
assertThat(resolved).startsWithRaw(baseDirectory);
assertThat(resolved).endsWithRaw(path);
}
assertThat(resolved).isAbsolute();
assertThat(resolved).startsWithRaw(baseDirectory);
assertThat(resolved).endsWithRaw(path);
}
}