Skip to content
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
2 changes: 2 additions & 0 deletions impl/maven-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ under the License.
<promoteUserPropertiesToSystemProperties>false</promoteUserPropertiesToSystemProperties>
<systemPropertyVariables>
<maven.home>${basedir}/src/test/resources/mavenHome</maven.home>
<userHome>${basedir}/src/test/resources/userHome</userHome>
<userDir>${basedir}/src/test/resources/userDir</userDir>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.maven.properties.internal.SystemProperties;

import static java.util.Objects.requireNonNull;
import static org.apache.maven.cling.invoker.CliUtils.createInterpolator;
import static org.apache.maven.cling.invoker.CliUtils.getCanonicalPath;
import static org.apache.maven.cling.invoker.CliUtils.or;
import static org.apache.maven.cling.invoker.CliUtils.prefix;
Expand Down Expand Up @@ -399,6 +400,7 @@ protected Map<String, String> populateSystemProperties(LocalContext context) {

protected Map<String, String> populateUserProperties(LocalContext context) {
Properties userProperties = new Properties();
Map<String, String> paths = context.extraInterpolationSource();

// ----------------------------------------------------------------------
// Options that are set on the command line become system properties
Expand All @@ -407,12 +409,12 @@ protected Map<String, String> populateUserProperties(LocalContext context) {
// ----------------------------------------------------------------------

Map<String, String> userSpecifiedProperties =
context.options.userProperties().orElse(new HashMap<>());
new HashMap<>(context.options.userProperties().orElse(new HashMap<>()));
createInterpolator().interpolate(userSpecifiedProperties, paths::get);

// ----------------------------------------------------------------------
// Load config files
// ----------------------------------------------------------------------
Map<String, String> paths = context.extraInterpolationSource();
UnaryOperator<String> callback =
or(paths::get, prefix("cli.", userSpecifiedProperties::get), context.systemProperties::get);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
import static org.apache.maven.cling.invoker.CliUtils.toMap;

public class CommonsCliOptions implements Options {
public static CommonsCliOptions parse(String source, String[] args) throws ParseException {
CLIManager cliManager = new CLIManager();
return new CommonsCliOptions(source, cliManager, cliManager.parse(args));
}

protected final String source;
protected final CLIManager cliManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.maven.cling.invoker;

import java.nio.file.Path;
import java.util.Arrays;

import org.apache.commons.cli.ParseException;
import org.apache.maven.api.cli.InvokerRequest;
import org.apache.maven.api.cli.Options;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.api.services.MessageBuilderFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;

public class BaseParserTest {
private final BaseParser subject = new BaseParser() {
@Override
protected Options parseCliOptions(LocalContext context) {
try {
return CommonsCliOptions.parse(
"test", context.parserRequest.args().toArray(new String[0]));
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
};

@Test
void happy() {
InvokerRequest invokerRequest =
subject.parseInvocation(ParserRequest.mvn(Arrays.asList("-e", "-X"), mock(MessageBuilderFactory.class))
.cwd(Path.of(System.getProperty("userDir")))
.userHome(Path.of(System.getProperty("userHome")))
.build());

Assertions.assertTrue(invokerRequest.options().isPresent());
Options options = invokerRequest.options().orElseThrow();
Assertions.assertFalse(options.showVersion().orElse(false));
Assertions.assertFalse(options.showVersionAndExit().orElse(false));
Assertions.assertTrue(options.showErrors().orElse(false));
Assertions.assertTrue(options.verbose().orElse(false));

// user home
Assertions.assertTrue(invokerRequest.userProperties().containsKey("user.property"));
Assertions.assertEquals("yes it is", invokerRequest.userProperties().get("user.property"));

// maven installation
Assertions.assertTrue(invokerRequest.userProperties().containsKey("maven.property"));
Assertions.assertEquals("yes it is", invokerRequest.userProperties().get("maven.property"));
}

@Test
void notHappy() {
InvokerRequest invokerRequest = subject.parseInvocation(
ParserRequest.mvn(Arrays.asList("--what-is-this-option", "-X"), mock(MessageBuilderFactory.class))
.cwd(Path.of(System.getProperty("userDir")))
.userHome(Path.of(System.getProperty("userHome")))
.build());

Assertions.assertFalse(invokerRequest.options().isPresent());
Assertions.assertTrue(invokerRequest.parsingFailed());
}

@Test
void specials() {
InvokerRequest invokerRequest = subject.parseInvocation(ParserRequest.mvn(
Arrays.asList("-Dfoo=${session.rootDirectory}", "-X"), mock(MessageBuilderFactory.class))
.cwd(Path.of(System.getProperty("userDir")))
.userHome(Path.of(System.getProperty("userHome")))
.build());

Assertions.assertTrue(invokerRequest.options().isPresent());
Assertions.assertTrue(invokerRequest.userProperties().containsKey("foo"));
Assertions.assertNotEquals(
"${session.rootDirectory}", invokerRequest.userProperties().get("foo"));
Assertions.assertFalse(invokerRequest.userProperties().get("foo").trim().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# The properties defined in this file will be made available through
# user properties at the very beginning of Maven's boot process.
#
maven.property = yes it is

maven.installation.conf = ${maven.home}/conf
maven.user.conf = ${user.home}/.m2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user.property=yes it is
3 changes: 3 additions & 0 deletions impl/maven-cli/src/test/resources/userHome/.m2/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0">

</settings>