Skip to content

Commit

Permalink
[MSHARED-1391] Code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 6, 2024
1 parent 4e21683 commit a309c8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ protected void setToolchainsLocation(InvocationRequest request, Commandline cli)
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setShellEnvironment(InvocationRequest request, Commandline cli) {
if (request.isShellEnvironmentInherited()) {
cli.addSystemEnvironment();
}
cli.setShellEnvironmentInherited(request.isShellEnvironmentInherited());

if (request.getJavaHome() != null) {
cli.addEnvironment("JAVA_HOME", request.getJavaHome().getAbsolutePath());
Expand All @@ -213,7 +211,7 @@ protected void setProfiles(InvocationRequest request, Commandline cli) {

if ((profiles != null) && !profiles.isEmpty()) {
cli.createArg().setValue("-P");
cli.createArg().setValue(StringUtils.join(profiles.iterator(), ","));
cli.createArg().setValue(String.join(",", profiles));
}
}

Expand All @@ -229,7 +227,7 @@ protected void setGoals(InvocationRequest request, Commandline cli) throws Comma

if ((goals != null) && !goals.isEmpty()) {
try {
cli.createArg().setLine(StringUtils.join(goals.iterator(), " "));
cli.createArg().setLine(String.join(" ", goals));
} catch (CommandLineException e) {
throw new CommandLineConfigurationException("Problem setting goals", e);
}
Expand Down Expand Up @@ -381,7 +379,7 @@ protected void setReactorBehavior(InvocationRequest request, Commandline cli) {
List<String> projectList = request.getProjects();
if (projectList != null) {
cli.createArg().setValue("-pl");
cli.createArg().setValue(StringUtils.join(projectList.iterator(), ","));
cli.createArg().setValue(String.join(",", projectList));

if (request.isAlsoMake()) {
cli.createArg().setValue("-am");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DefaultInvokerTest {
private InvocationRequest request = new DefaultInvocationRequest();

@BeforeEach
public void setUp() throws Exception {
public void setUp() {
request.setDebug(true);
request.setProperties(getProperties());
}
Expand Down Expand Up @@ -172,12 +172,7 @@ public void testMavenWrapperInProject() throws Exception {
request.setMavenExecutable(new File("./mvnw"));

final StringBuilder outlines = new StringBuilder();
request.setOutputHandler(new InvocationOutputHandler() {
@Override
public void consumeLine(String line) {
outlines.append(line);
}
});
request.setOutputHandler(outlines::append);

InvocationResult result = invoker.execute(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class MavenCommandLineBuilderTest {
Expand Down Expand Up @@ -75,21 +77,15 @@ public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile() {

mclb.setLocalRepositoryDirectory(lrd);

try {
mclb.setLocalRepository(newRequest(), cli);
fail("Should not set local repo location to point to a file.");
} catch (IllegalArgumentException expected) {
}
InvocationRequest request = newRequest();
assertThrows(IllegalArgumentException.class, () -> mclb.setLocalRepository(request, cli));
}

@Test
public void testShouldFailToSetLocalRepoLocationFromRequestWhenItIsAFile() {
InvocationRequest request = newRequest().setLocalRepositoryDirectory(lrd);
try {
mclb.setLocalRepository(request, cli);
fail("Should not set local repo location to point to a file.");
} catch (IllegalArgumentException expected) {
}

assertThrows(IllegalArgumentException.class, () -> mclb.setLocalRepository(request, cli));
}

@Test
Expand Down Expand Up @@ -207,11 +203,7 @@ private File setupTempMavenHomeIfMissing(boolean forceDummy) throws Exception {
public void testShouldFailIfLoggerSetToNull() {
mclb.setLogger(null);

try {
mclb.checkRequiredState();
fail("Should not allow execution to proceed when logger is missing.");
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> mclb.checkRequiredState());
}

@Test
Expand All @@ -236,21 +228,19 @@ public void testShouldFindDummyMavenExecutable() throws Exception {
}

@Test
@EnabledOnOs(OS.WINDOWS)
public void testShouldFindDummyPS1MavenExecutable() throws Exception {
File dummyMavenHomeBin = Files.createDirectories(temporaryFolder
.resolve("invoker-tests")
.resolve("dummy-maven-home")
.resolve("bin"))
.toFile();

File check;
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
check = createDummyFile(dummyMavenHomeBin, "mvn.ps1");
mclb.setMavenHome(dummyMavenHomeBin.getParentFile());
mclb.setupMavenExecutable(newRequest());
File check = createDummyFile(dummyMavenHomeBin, "mvn.ps1");
mclb.setMavenHome(dummyMavenHomeBin.getParentFile());
mclb.setupMavenExecutable(newRequest());

assertEquals(check.getCanonicalPath(), mclb.getMavenExecutable().getCanonicalPath());
}
assertEquals(check.getCanonicalPath(), mclb.getMavenExecutable().getCanonicalPath());
}

@Test
Expand Down

0 comments on commit a309c8e

Please # to comment.