Skip to content

Commit

Permalink
[MNG-8487] Completely isolate UTs (#2021)
Browse files Browse the repository at this point in the history
UTs were using real user home, and in case user had user-wide extensions.xml, it resulted in failure. Goal: make sure all UTs are properly protected and isolated from user env, by providing "fake" user home (to not have user-wide extensions in real user home affect test outcome).

---

https://issues.apache.org/jira/browse/MNG-8487
  • Loading branch information
cstamas authored Jan 6, 2025
1 parent 0b7235c commit 0176ffb
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ protected Parser createParser() {
}

@Test
void defaultFs(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path tempDir) throws Exception {
invoke(tempDir, Arrays.asList("clean", "verify"));
void defaultFs(
@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path cwd,
@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path userHome)
throws Exception {
invoke(cwd, userHome, Arrays.asList("clean", "verify"));
}

@Disabled("Until we move off fully from File")
@Test
void jimFs() throws Exception {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
invoke(fs.getPath("/"), Arrays.asList("clean", "verify"));
invoke(fs.getPath("/cwd"), fs.getPath("/home"), Arrays.asList("clean", "verify"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void main(String... args) {
}
""";

protected void invoke(Path cwd, Collection<String> goals) throws Exception {
protected void invoke(Path cwd, Path userHome, Collection<String> goals) throws Exception {
// works only in recent Maven4
Assumptions.assumeTrue(
Files.isRegularFile(Paths.get(System.getProperty("maven.home"))
Expand All @@ -104,6 +104,7 @@ protected void invoke(Path cwd, Collection<String> goals) throws Exception {
new ProtoLogger(),
new JLineMessageBuilderFactory())
.cwd(cwd)
.userHome(userHome)
.build()));
String log = Files.readString(logFile);
System.out.println(log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ protected Parser createParser() {
}

@Test
void defaultFs(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path tempDir) throws Exception {
invoke(tempDir, Arrays.asList("clean", "verify"));
void defaultFs(
@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path cwd,
@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path userHome)
throws Exception {
invoke(cwd, userHome, Arrays.asList("clean", "verify"));
}

@Disabled("Until we move off fully from File")
@Test
void jimFs() throws Exception {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) {
invoke(fs.getPath("/"), Arrays.asList("clean", "verify"));
invoke(fs.getPath("/cwd"), fs.getPath("/home"), Arrays.asList("clean", "verify"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ static Builder mavenBuilder(@Nullable Path installationDirectory) {
MVN,
null,
getCanonicalPath(Paths.get(System.getProperty("user.dir"))),
installationDirectory != null ? getCanonicalPath(installationDirectory) : discoverMavenHome(),
installationDirectory != null
? getCanonicalPath(installationDirectory)
: discoverInstallationDirectory(),
getCanonicalPath(Paths.get(System.getProperty("user.home"))),
null,
null,
Expand Down Expand Up @@ -430,14 +432,23 @@ public String toString() {
}

@Nonnull
static Path discoverMavenHome() {
static Path discoverInstallationDirectory() {
String mavenHome = System.getProperty("maven.home");
if (mavenHome == null) {
throw new ExecutorException("requires maven.home Java System Property set");
}
return getCanonicalPath(Paths.get(mavenHome));
}

@Nonnull
static Path discoverUserHomeDirectory() {
String userHome = System.getProperty("user.home");
if (userHome == null) {
throw new ExecutorException("requires user.home Java System Property set");
}
return getCanonicalPath(Paths.get(userHome));
}

@Nonnull
static Path getCanonicalPath(Path path) {
requireNonNull(path, "path");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,25 @@
public class HelperImpl implements ExecutorHelper {
private final Mode defaultMode;
private final Path installationDirectory;
private final Path userHomeDirectory;
private final ExecutorTool executorTool;
private final HashMap<Mode, Executor> executors;

private final ConcurrentHashMap<String, String> cache;

public HelperImpl(Mode defaultMode, @Nullable Path installationDirectory, Executor embedded, Executor forked) {
public HelperImpl(
Mode defaultMode,
@Nullable Path installationDirectory,
@Nullable Path userHomeDirectory,
Executor embedded,
Executor forked) {
this.defaultMode = requireNonNull(defaultMode);
this.installationDirectory = installationDirectory != null
? ExecutorRequest.getCanonicalPath(installationDirectory)
: ExecutorRequest.discoverMavenHome();
: ExecutorRequest.discoverInstallationDirectory();
this.userHomeDirectory = userHomeDirectory != null
? ExecutorRequest.getCanonicalPath(userHomeDirectory)
: ExecutorRequest.discoverUserHomeDirectory();
this.executorTool = new ToolboxTool(this);
this.executors = new HashMap<>();

Expand All @@ -64,7 +73,7 @@ public Mode getDefaultMode() {

@Override
public ExecutorRequest.Builder executorRequest() {
return ExecutorRequest.mavenBuilder(installationDirectory);
return ExecutorRequest.mavenBuilder(installationDirectory).userHomeDirectory(userHomeDirectory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.maven.cling.executor.embedded.EmbeddedMavenExecutor;
import org.apache.maven.cling.executor.forked.ForkedMavenExecutor;
import org.apache.maven.cling.executor.internal.HelperImpl;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

Expand All @@ -40,12 +41,16 @@ public class HelperImplTest {
private static final EmbeddedMavenExecutor EMBEDDED_MAVEN_EXECUTOR = new EmbeddedMavenExecutor();
private static final ForkedMavenExecutor FORKED_MAVEN_EXECUTOR = new ForkedMavenExecutor();

@TempDir
private Path userHome;

@ParameterizedTest
@EnumSource(ExecutorHelper.Mode.class)
void dump3(ExecutorHelper.Mode mode) throws Exception {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
Map<String, String> dump = helper.dump(helper.executorRequest());
Expand All @@ -58,6 +63,7 @@ void dump4(ExecutorHelper.Mode mode) throws Exception {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
Map<String, String> dump = helper.dump(helper.executorRequest());
Expand All @@ -70,6 +76,7 @@ void version3(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
assertEquals(System.getProperty("maven3version"), helper.mavenVersion());
Expand All @@ -81,6 +88,7 @@ void version4(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
assertEquals(System.getProperty("maven4version"), helper.mavenVersion());
Expand All @@ -92,6 +100,7 @@ void localRepository3(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String localRepository = helper.localRepository(helper.executorRequest());
Expand All @@ -105,6 +114,7 @@ void localRepository4(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String localRepository = helper.localRepository(helper.executorRequest());
Expand All @@ -118,6 +128,7 @@ void artifactPath3(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.artifactPath(helper.executorRequest(), "aopalliance:aopalliance:1.0", "central");
Expand All @@ -133,6 +144,7 @@ void artifactPath4(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.artifactPath(helper.executorRequest(), "aopalliance:aopalliance:1.0", "central");
Expand All @@ -148,6 +160,7 @@ void metadataPath3(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.metadataPath(helper.executorRequest(), "aopalliance", "someremote");
Expand All @@ -160,6 +173,7 @@ void metadataPath4(ExecutorHelper.Mode mode) {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.metadataPath(helper.executorRequest(), "aopalliance", "someremote");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public Verifier(String basedir, List<String> defaultCliArguments) throws Verific
this.executorHelper = new HelperImpl(
VERIFIER_FORK_MODE,
Paths.get(System.getProperty("maven.home")),
this.userHomeDirectory,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
this.defaultCliArguments =
Expand Down

0 comments on commit 0176ffb

Please sign in to comment.