This is an automated email from the ASF dual-hosted git repository.
cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new 0176ffb825 [MNG-8487] Completely isolate UTs (#2021)
0176ffb825 is described below
commit 0176ffb825175f8a0ce124b1de287e527dab50e0
Author: Tamas Cservenak <[email protected]>
AuthorDate: Mon Jan 6 11:32:18 2025 +0100
[MNG-8487] Completely isolate UTs (#2021)
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
---
.../apache/maven/cling/invoker/mvn/MavenInvokerTest.java | 9 ++++++---
.../maven/cling/invoker/mvn/MavenInvokerTestSupport.java | 3 ++-
.../invoker/mvn/resident/ResidentMavenInvokerTest.java | 9 ++++++---
.../java/org/apache/maven/api/cli/ExecutorRequest.java | 15 +++++++++++++--
.../apache/maven/cling/executor/internal/HelperImpl.java | 15 ++++++++++++---
.../apache/maven/cling/executor/impl/HelperImplTest.java | 14 ++++++++++++++
.../src/main/java/org/apache/maven/it/Verifier.java | 1 +
7 files changed, 54 insertions(+), 12 deletions(-)
diff --git
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTest.java
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTest.java
index 54d6fb147b..bae4f7015f 100644
---
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTest.java
+++
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTest.java
@@ -52,15 +52,18 @@ public class MavenInvokerTest extends
MavenInvokerTestSupport {
}
@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"));
}
}
}
diff --git
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTestSupport.java
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTestSupport.java
index 4f3f46d4a0..89b33df73f 100644
---
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTestSupport.java
+++
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/MavenInvokerTestSupport.java
@@ -80,7 +80,7 @@ public abstract class MavenInvokerTestSupport {
}
""";
- 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"))
@@ -104,6 +104,7 @@ public abstract class MavenInvokerTestSupport {
new ProtoLogger(),
new JLineMessageBuilderFactory())
.cwd(cwd)
+ .userHome(userHome)
.build()));
String log = Files.readString(logFile);
System.out.println(log);
diff --git
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvokerTest.java
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvokerTest.java
index 004b234de6..a9d017c96c 100644
---
a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvokerTest.java
+++
b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvn/resident/ResidentMavenInvokerTest.java
@@ -55,15 +55,18 @@ public class ResidentMavenInvokerTest extends
MavenInvokerTestSupport {
}
@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"));
}
}
}
diff --git
a/impl/maven-executor/src/main/java/org/apache/maven/api/cli/ExecutorRequest.java
b/impl/maven-executor/src/main/java/org/apache/maven/api/cli/ExecutorRequest.java
index d13fa7ee51..494cb06e1b 100644
---
a/impl/maven-executor/src/main/java/org/apache/maven/api/cli/ExecutorRequest.java
+++
b/impl/maven-executor/src/main/java/org/apache/maven/api/cli/ExecutorRequest.java
@@ -163,7 +163,9 @@ public interface ExecutorRequest {
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,
@@ -430,7 +432,7 @@ public interface ExecutorRequest {
}
@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");
@@ -438,6 +440,15 @@ public interface ExecutorRequest {
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");
diff --git
a/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/internal/HelperImpl.java
b/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/internal/HelperImpl.java
index e33af152bc..4304f6d871 100644
---
a/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/internal/HelperImpl.java
+++
b/impl/maven-executor/src/main/java/org/apache/maven/cling/executor/internal/HelperImpl.java
@@ -39,16 +39,25 @@ import static java.util.Objects.requireNonNull;
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<>();
@@ -64,7 +73,7 @@ public class HelperImpl implements ExecutorHelper {
@Override
public ExecutorRequest.Builder executorRequest() {
- return ExecutorRequest.mavenBuilder(installationDirectory);
+ return
ExecutorRequest.mavenBuilder(installationDirectory).userHomeDirectory(userHomeDirectory);
}
@Override
diff --git
a/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java
b/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java
index 452ab530f8..3e21b631bd 100644
---
a/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java
+++
b/impl/maven-executor/src/test/java/org/apache/maven/cling/executor/impl/HelperImplTest.java
@@ -28,6 +28,7 @@ import org.apache.maven.cling.executor.ExecutorHelper;
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;
@@ -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());
@@ -58,6 +63,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
Map<String, String> dump = helper.dump(helper.executorRequest());
@@ -70,6 +76,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
assertEquals(System.getProperty("maven3version"),
helper.mavenVersion());
@@ -81,6 +88,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
assertEquals(System.getProperty("maven4version"),
helper.mavenVersion());
@@ -92,6 +100,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String localRepository =
helper.localRepository(helper.executorRequest());
@@ -105,6 +114,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String localRepository =
helper.localRepository(helper.executorRequest());
@@ -118,6 +128,7 @@ public class HelperImplTest {
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");
@@ -133,6 +144,7 @@ public class HelperImplTest {
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");
@@ -148,6 +160,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn3ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.metadataPath(helper.executorRequest(),
"aopalliance", "someremote");
@@ -160,6 +173,7 @@ public class HelperImplTest {
ExecutorHelper helper = new HelperImpl(
mode,
mvn4ExecutorRequestBuilder().build().installationDirectory(),
+ userHome,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
String path = helper.metadataPath(helper.executorRequest(),
"aopalliance", "someremote");
diff --git
a/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
b/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
index 4624035ce3..b51180030e 100644
---
a/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
+++
b/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
@@ -140,6 +140,7 @@ public class Verifier {
this.executorHelper = new HelperImpl(
VERIFIER_FORK_MODE,
Paths.get(System.getProperty("maven.home")),
+ this.userHomeDirectory,
EMBEDDED_MAVEN_EXECUTOR,
FORKED_MAVEN_EXECUTOR);
this.defaultCliArguments =