Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #42 mvnd fails if there is no .mvn/ dir in the user home #46

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ private String startDaemon() {
if (timeout != null) {
args.add(Environment.DAEMON_IDLE_TIMEOUT.asCommandLineProperty(timeout));
}
args.add("\"-Dmaven.multiModuleProjectDirectory=" + layout.multiModuleProjectDirectory().toString() + "\"");

args.add(ServerMain.class.getName());
command = String.join(" ", args);
Expand Down
27 changes: 15 additions & 12 deletions client/src/main/java/org/jboss/fuse/mvnd/client/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,25 @@ public static EnvValue findBasicMavenHome() {
public static Path findMultiModuleProjectDirectory(Path pwd) {
return MAVEN_MULTIMODULE_PROJECT_DIRECTORY
.systemProperty()
.orDefault(() -> {
Path dir = pwd;
do {
if (Files.isDirectory(dir.resolve(".mvn"))) {
return dir.toString();
}
dir = dir.getParent();
} while (dir != null);
throw new IllegalStateException("Could not detect maven.multiModuleProjectDirectory by climbing up from ["
+ pwd
+ "] seeking a .mvn directory. You may want to create a .mvn directory in the root directory of your source tree.");
})
.orDefault(() -> findDefaultMultimoduleProjectDirectory(pwd))
.asPath()
.toAbsolutePath().normalize();
}

public static String findDefaultMultimoduleProjectDirectory(Path pwd) {
Path dir = pwd;
do {
if (Files.isDirectory(dir.resolve(".mvn"))) {
return dir.toString();
}
dir = dir.getParent();
} while (dir != null);
/* Return pwd if .mvn directory was not found in the hierarchy.
* Maven does the same thing in mvn shell script's find_maven_basedir()
* and find_file_argument_basedir() routines */
return pwd.toString();
}

public static Path findLogbackConfigurationPath(Supplier<Properties> mvndProperties, Path mvndPropertiesPath,
Path mvndHome) {
return LOGBACK_CONFIGURATION_FILE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.fuse.mvnd.it;

import java.io.IOException;
import javax.inject.Inject;
import org.assertj.core.api.Assertions;
import org.jboss.fuse.mvnd.assertj.MatchInOrderAmongOthers;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.ClientLayout;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.junit.MvndNativeTest;
import org.jboss.fuse.mvnd.junit.MvndTestExtension;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

@MvndNativeTest(projectDir = MvndTestExtension.TEMP_EXTERNAL)
public class VersionNativeIT {

@Inject
Client client;

@Inject
ClientLayout layout;

@Test
void version() throws IOException, InterruptedException {
final ClientOutput output = Mockito.mock(ClientOutput.class);

client.execute(output, "-v").assertSuccess();

final ArgumentCaptor<String> logMessage = ArgumentCaptor.forClass(String.class);
Mockito.verify(output, Mockito.atLeast(1)).accept(logMessage.capture());

Assertions.assertThat(logMessage.getAllValues())
.is(new MatchInOrderAmongOthers<>(
"\\QMaven Daemon " + System.getProperty("project.version") + "\\E",
"\\QMaven home: " + layout.mavenHome() + "\\E"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,10 @@
*/
package org.jboss.fuse.mvnd.it;

import java.io.IOException;
import javax.inject.Inject;
import org.assertj.core.api.Assertions;
import org.jboss.fuse.mvnd.assertj.MatchInOrderAmongOthers;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.ClientLayout;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.junit.MvndTest;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.jboss.fuse.mvnd.junit.MvndTestExtension;

@MvndTest(projectDir = "src/test/projects/single-module")
public class VersionTest {
@MvndTest(projectDir = MvndTestExtension.TEMP_EXTERNAL)
public class VersionTest extends VersionNativeIT {

@Inject
Client client;

@Inject
ClientLayout layout;

@Test
void version() throws IOException, InterruptedException {
final ClientOutput output = Mockito.mock(ClientOutput.class);

client.execute(output, "-v").assertSuccess();

final ArgumentCaptor<String> logMessage = ArgumentCaptor.forClass(String.class);
Mockito.verify(output, Mockito.atLeast(1)).accept(logMessage.capture());

Assertions.assertThat(logMessage.getAllValues())
.is(new MatchInOrderAmongOthers<>(
"\\QMaven Daemon " + System.getProperty("project.version") + "\\E",
"\\QMaven home: " + layout.mavenHome() + "\\E"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jboss.fuse.mvnd.client.DaemonInfo;
import org.jboss.fuse.mvnd.client.DaemonRegistry;
import org.jboss.fuse.mvnd.client.DefaultClient;
import org.jboss.fuse.mvnd.client.Environment;
import org.jboss.fuse.mvnd.client.Layout;
import org.jboss.fuse.mvnd.jpm.ProcessImpl;
import org.junit.jupiter.api.extension.AfterAllCallback;
Expand All @@ -40,6 +41,8 @@

public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback {

/** A placeholder to replace with a temporary directory outside of the current source tree */
public static final String TEMP_EXTERNAL = "${temp.external}";
private volatile Exception bootException;

public MvndTestExtension() {
Expand All @@ -54,11 +57,18 @@ public void beforeAll(ExtensionContext context) throws Exception {
final MvndTest mnvdTest = testClass.getAnnotation(MvndTest.class);
if (mnvdTest != null) {
store.put(MvndResource.class.getName(),
MvndResource.create(context.getRequiredTestClass().getSimpleName(), mnvdTest.projectDir(), false, -1L));
MvndResource.create(
context.getRequiredTestClass().getSimpleName(),
mnvdTest.projectDir(),
false,
-1L));
} else {
final MvndNativeTest mvndNativeTest = testClass.getAnnotation(MvndNativeTest.class);
store.put(MvndResource.class.getName(),
MvndResource.create(context.getRequiredTestClass().getSimpleName(), mvndNativeTest.projectDir(), true,
MvndResource.create(
context.getRequiredTestClass().getSimpleName(),
mvndNativeTest.projectDir(),
true,
mvndNativeTest.timeoutSec() * 1000L));
}
} catch (Exception e) {
Expand Down Expand Up @@ -126,29 +136,40 @@ public static MvndResource create(String className, String rawProjectDir, boolea
if (rawProjectDir == null) {
throw new IllegalStateException("rawProjectDir of @MvndTest must be set");
}
final Path mvndTestSrcDir = Paths.get(rawProjectDir).toAbsolutePath().normalize();
if (!Files.exists(mvndTestSrcDir)) {
throw new IllegalStateException("@MvndTest(projectDir = \"" + rawProjectDir
+ "\") points at a path that does not exist: " + mvndTestSrcDir);
}

final Path testDir = Paths.get("target/mvnd-tests/" + className).toAbsolutePath();
final Path testExecutionDir = testDir.resolve("project");
try (Stream<Path> files = Files.walk(mvndTestSrcDir)) {
files.forEach(source -> {
final Path dest = testExecutionDir.resolve(mvndTestSrcDir.relativize(source));
try {
if (Files.isDirectory(source)) {
Files.createDirectories(dest);
} else {
Files.createDirectories(dest.getParent());
Files.copy(source, dest);
Files.createDirectories(testDir);
final Path testExecutionDir;
if (TEMP_EXTERNAL.equals(rawProjectDir)) {
try {
testExecutionDir = Files.createTempDirectory(MvndTestExtension.class.getSimpleName());
} catch (IOException e) {
throw new RuntimeException("Could not create temporary directory", e);
}
} else {
final Path mvndTestSrcDir = Paths.get(rawProjectDir).toAbsolutePath().normalize();
if (!Files.exists(mvndTestSrcDir)) {
throw new IllegalStateException("@MvndTest(projectDir = \"" + mvndTestSrcDir
+ "\") points at a path that does not exist: " + mvndTestSrcDir);
}
testExecutionDir = testDir.resolve("project");
try (Stream<Path> files = Files.walk(mvndTestSrcDir)) {
files.forEach(source -> {
final Path dest = testExecutionDir.resolve(mvndTestSrcDir.relativize(source));
try {
if (Files.isDirectory(source)) {
Files.createDirectories(dest);
} else {
Files.createDirectories(dest.getParent());
Files.copy(source, dest);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
});
}
}
final Path multiModuleProjectDirectory = Paths
.get(Environment.findDefaultMultimoduleProjectDirectory(testExecutionDir));

final Path mvndHome = Paths
.get(Objects.requireNonNull(System.getProperty("mvnd.home"), "System property mvnd.home must be set"))
Expand All @@ -165,7 +186,7 @@ public static MvndResource create(String className, String rawProjectDir, boolea
mvndPropertiesPath,
mvndHome,
testExecutionDir,
testExecutionDir,
multiModuleProjectDirectory,
Paths.get(System.getProperty("java.home")).toAbsolutePath().normalize(),
localMavenRepository, settingsPath,
mvndHome.resolve("conf/logging/logback.xml"));
Expand Down