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
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,25 @@ void writeBinding(File inputFile, File projectDir) throws MojoExecutionException
// file does not, so a tool reading the file looked at a different
// class than the build and reported the annotations on the
// selected one as absent.
+ bindingValue("mavenHome", System.getProperty("maven.home"))
+ mavenArgumentsBinding()
Comment thread
shai-almog marked this conversation as resolved.
+ bindingValue("mainName", effective("codename1.mainName"))
+ bindingValue("packageName", effective("codename1.packageName"));
try {
// User properties can contain credentials. Restrict access before
// writing, including when replacing an existing binding on POSIX.
java.nio.file.Path path = inputFile.toPath();
java.nio.file.Files.createDirectories(path.toAbsolutePath().getParent());
if (path.getFileSystem().supportedFileAttributeViews().contains("posix")) {
java.util.Set<java.nio.file.attribute.PosixFilePermission> permissions =
java.nio.file.attribute.PosixFilePermissions.fromString("rw-------");
if (!java.nio.file.Files.exists(path)) {
java.nio.file.Files.createFile(path,
java.nio.file.attribute.PosixFilePermissions.asFileAttribute(permissions));
} else {
java.nio.file.Files.setPosixFilePermissions(path, permissions);
}
}
FileUtils.write(inputFile, content, StandardCharsets.UTF_8);
} catch (IOException ex) {
throw new MojoExecutionException("Failed to write Settings binding", ex);
Expand Down Expand Up @@ -221,6 +237,44 @@ private String effective(String key) {
return properties == null ? null : properties.getProperty(key);
}

/// Forward resolved session options rather than reparsing a shell command.
/// Each argument is encoded separately so whitespace, newlines and equals
/// signs in paths/property values survive the line-oriented binding.
private String mavenArgumentsBinding() {
MavenSession session = getSession();
if (session == null || session.getRequest() == null) return "";
org.apache.maven.execution.MavenExecutionRequest request = session.getRequest();
java.util.List<String> args = new java.util.ArrayList<>();
if (request.getUserSettingsFile() != null && request.getUserSettingsFile().isFile()) {
args.add("--settings");
args.add(request.getUserSettingsFile().getAbsolutePath());
}
if (request.getGlobalSettingsFile() != null && request.getGlobalSettingsFile().isFile()) {
args.add("--global-settings");
args.add(request.getGlobalSettingsFile().getAbsolutePath());
}
if (request.isOffline()) args.add("--offline");
if (request.isUpdateSnapshots()) args.add("--update-snapshots");
Comment thread
shai-almog marked this conversation as resolved.
if (request.isNoSnapshotUpdates()) args.add("--no-snapshot-updates");
for (String profile : request.getActiveProfiles()) args.add("-P" + profile);
for (String profile : request.getInactiveProfiles()) args.add("-P!" + profile);
java.util.Properties user = session.getUserProperties();
if (user != null) {
for (String key : new java.util.TreeSet<String>(user.stringPropertyNames())) {
args.add("-D" + key + "=" + user.getProperty(key));
}
}
if (request.getLocalRepositoryPath() != null) {
args.add("-Dmaven.repo.local=" + request.getLocalRepositoryPath().getAbsolutePath());
}
StringBuilder out = new StringBuilder();
for (String arg : args) {
out.append(bindingValue("mavenArgument", java.util.Base64.getEncoder()
.encodeToString(arg.getBytes(StandardCharsets.UTF_8))));
}
return out.toString();
}

private static String bindingValue(String key, String value) {
return value == null ? "" : key + "=" + value + "\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,102 @@ public class OpenSettingsMojoTest {
@Rule
public TemporaryFolder tmp = new TemporaryFolder();

@Test
public void forwardsSnapshotUpdateSuppressionIndependentlyOfForcedUpdates() throws Exception {
for (boolean suppress : new boolean[]{false, true}) {
org.apache.maven.execution.MavenExecutionRequest request =
new org.apache.maven.execution.DefaultMavenExecutionRequest();
request.setNoSnapshotUpdates(suppress);
final org.apache.maven.execution.MavenSession session = new org.apache.maven.execution.MavenSession(
null, null, request, new org.apache.maven.execution.DefaultMavenExecutionResult());
OpenSettingsMojo mojo = new OpenSettingsMojo() {
@Override protected org.apache.maven.execution.MavenSession getSession() { return session; }
};
File input = tmp.newFile("snapshot-" + suppress + ".input");
mojo.writeBinding(input, tmp.newFolder("common-" + suppress));
String content = new String(Files.readAllBytes(input.toPath()), StandardCharsets.UTF_8);
String flag = java.util.Base64.getEncoder().encodeToString(
"--no-snapshot-updates".getBytes(StandardCharsets.UTF_8));
assertEquals(suppress, content.contains("mavenArgument=" + flag + "\n"));
}
}

@Test
public void forwardsMavenSessionOptionsInPrivateBinding() throws Exception {
org.apache.maven.execution.MavenExecutionRequest request =
new org.apache.maven.execution.DefaultMavenExecutionRequest();
File userSettings = new File(tmp.getRoot(), "user settings.xml");
File globalSettings = new File(tmp.getRoot(), "global settings.xml");
Files.write(userSettings.toPath(), "<settings/>".getBytes(StandardCharsets.UTF_8));
Files.write(globalSettings.toPath(), "<settings/>".getBytes(StandardCharsets.UTF_8));
File repository = new File(tmp.getRoot(), "private repo");
request.setUserSettingsFile(userSettings).setGlobalSettingsFile(globalSettings)
.setLocalRepositoryPath(repository).setOffline(true).setUpdateSnapshots(true)
.setActiveProfiles(Arrays.asList("private", "desktop"))
.setInactiveProfiles(Arrays.asList("public"));
java.util.Properties properties = new java.util.Properties();
properties.setProperty("custom", "spaces & equals=and\nnewlines Ω ");
request.setUserProperties(properties);
final org.apache.maven.execution.MavenSession session = new org.apache.maven.execution.MavenSession(
null, null, request, new org.apache.maven.execution.DefaultMavenExecutionResult());
OpenSettingsMojo mojo = new OpenSettingsMojo() {
@Override protected org.apache.maven.execution.MavenSession getSession() { return session; }
};
for (String fileName : Arrays.asList("existing.input", "new.input")) {
File input = new File(tmp.getRoot(), fileName);
if (fileName.startsWith("existing")) Files.write(input.toPath(), new byte[0]);
mojo.writeBinding(input, tmp.newFolder(fileName + "-common"));
List<String> arguments = new java.util.ArrayList<>();
for (String line : Files.readAllLines(input.toPath(), StandardCharsets.UTF_8)) {
if (line.startsWith("mavenArgument=")) {
arguments.add(new String(java.util.Base64.getDecoder().decode(
line.substring("mavenArgument=".length())), StandardCharsets.UTF_8));
}
}
assertEquals(Arrays.asList("--settings", userSettings.getAbsolutePath(),
"--global-settings", globalSettings.getAbsolutePath(), "--offline", "--update-snapshots",
"-Pprivate", "-Pdesktop", "-P!public", "-Dcustom=" + properties.getProperty("custom"),
"-Dmaven.repo.local=" + repository.getAbsolutePath()), arguments);
if (input.toPath().getFileSystem().supportedFileAttributeViews().contains("posix")) {
assertEquals(java.nio.file.attribute.PosixFilePermissions.fromString("rw-------"),
Files.getPosixFilePermissions(input.toPath()));
}
}
}

@Test
public void absentDefaultSettingsFilesDoNotBecomeRequiredCliFiles() throws Exception {
org.apache.maven.execution.MavenExecutionRequest request =
new org.apache.maven.execution.DefaultMavenExecutionRequest();
request.setUserSettingsFile(new File(tmp.getRoot(), "absent-user.xml"))
.setGlobalSettingsFile(new File(tmp.getRoot(), "absent-global.xml"));
final org.apache.maven.execution.MavenSession session = new org.apache.maven.execution.MavenSession(
null, null, request, new org.apache.maven.execution.DefaultMavenExecutionResult());
OpenSettingsMojo mojo = new OpenSettingsMojo() {
@Override protected org.apache.maven.execution.MavenSession getSession() { return session; }
};
File input = tmp.newFile("binding.input");
mojo.writeBinding(input, tmp.newFolder("common"));
assertFalse(new String(Files.readAllBytes(input.toPath()), StandardCharsets.UTF_8)
.contains("mavenArgument="));
}

@Test
public void forwardsMavenHomeToSettingsBinding() throws Exception {
String previous = System.getProperty("maven.home");
try {
System.setProperty("maven.home", "/tools/maven with spaces");
File common = tmp.newFolder("binding-common");
File input = tmp.newFile("binding.input");
new OpenSettingsMojo().writeBinding(input, common);
assertTrue(new String(Files.readAllBytes(input.toPath()), StandardCharsets.UTF_8)
.contains("mavenHome=/tools/maven with spaces\n"));
} finally {
if (previous == null) System.clearProperty("maven.home");
else System.setProperty("maven.home", previous);
}
}

@Test
public void extractsPackagedIconForDesktopLaunchers() throws Exception {
File jar = jarWithIcon("settings.jar");
Expand Down
21 changes: 11 additions & 10 deletions scripts/run-windows-tooling-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,7 @@ EOF
mvn -B -q -f "$(winpath "$SETTINGS_CP_DIR/cp-pom.xml")" dependency:build-classpath \
"-Dmdep.outputFile=$(winpath "$SETTINGS_CP_DIR/cp.txt")"
SETTINGS_CP="$(cat "$SETTINGS_CP_DIR/cp.txt")"

BINDING_FILE="$SETTINGS_CP_DIR/settings.input"
WORK_DIR_W="$(winpath "$WORK_DIR")"
cat > "$BINDING_FILE" <<EOF
# Codename One Settings project binding
projectDir=$WORK_DIR_W
settings=$WORK_DIR_W\\codenameone_settings.properties
pom=$WORK_DIR_W\\pom.xml
multimoduleRoot=$WORK_DIR_W
EOF

MATRIX_FAILURES=()

Expand All @@ -153,10 +144,20 @@ run_settings_scenario() {
local name="$1"; shift
local png="$ARTIFACTS_DIR/settings-$name.png"
local diag="$ARTIFACTS_DIR/settings-$name.txt"
# Settings consumes its launch binding after reading it. Every scenario
# needs its own file or only the first screenshot has a selected project.
local binding_file="$SETTINGS_CP_DIR/settings-$name.input"
cat > "$binding_file" <<EOF
# Codename One Settings project binding
projectDir=$WORK_DIR_W
settings=$WORK_DIR_W\\codenameone_settings.properties
pom=$WORK_DIR_W\\pom.xml
multimoduleRoot=$WORK_DIR_W
EOF
wt_log "Settings render scenario: $name ($*)"
if ! "$JAVA_BIN" "$@" \
-Djava.awt.headless=false \
"-Dsettings.input=$(winpath "$BINDING_FILE")" \
"-Dsettings.input=$(winpath "$binding_file")" \
"-Dsettings.screenshot=$(winpath "$png")" \
"-Dsettings.diagnostics=$(winpath "$diag")" \
-Dsettings.screenshot.delay=8000 \
Expand Down
Loading