From 4c27bbfe92ae368fa9719bef868b5fc5f54dba5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Fri, 2 Aug 2024 19:05:18 +0200 Subject: [PATCH] Migrate Invoker to Integration test for version-bump-plugin (cherry picked from commit e1967079439bb9dd6f3e604105cb6f5b118d1fce) --- .../tycho-version-bump-plugin/pom.xml | 23 ------ .../src/it/update-target/update-target.target | 12 --- .../src/it/update-target/verify.groovy | 10 --- .../tycho/versionbump/AbstractUpdateMojo.java | 8 +- .../tycho/versionbump/UpdateTargetMojo.java | 27 +++++-- .../update-target/pom.xml | 15 +--- .../update-target/update-target.target | 16 ++++ .../tycho/test/VersionBumpPluginTest.java | 73 +++++++++++++++++++ .../targetplatform/TargetDefinitionFile.java | 5 ++ 9 files changed, 123 insertions(+), 66 deletions(-) delete mode 100644 tycho-extras/tycho-version-bump-plugin/src/it/update-target/update-target.target delete mode 100644 tycho-extras/tycho-version-bump-plugin/src/it/update-target/verify.groovy rename {tycho-extras/tycho-version-bump-plugin/src/it => tycho-its/projects/tycho-version-bump-plugin}/update-target/pom.xml (79%) create mode 100644 tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target create mode 100644 tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java diff --git a/tycho-extras/tycho-version-bump-plugin/pom.xml b/tycho-extras/tycho-version-bump-plugin/pom.xml index 8aed4ade9d..154ad2eeda 100644 --- a/tycho-extras/tycho-version-bump-plugin/pom.xml +++ b/tycho-extras/tycho-version-bump-plugin/pom.xml @@ -54,27 +54,4 @@ - - - - its - - - - org.apache.maven.plugins - maven-invoker-plugin - - - ${project.groupId}:${project.artifactId}:${project.version}:update-target - - - update-target.target - - - - - - - - diff --git a/tycho-extras/tycho-version-bump-plugin/src/it/update-target/update-target.target b/tycho-extras/tycho-version-bump-plugin/src/it/update-target/update-target.target deleted file mode 100644 index f8b260bcf8..0000000000 --- a/tycho-extras/tycho-version-bump-plugin/src/it/update-target/update-target.target +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/tycho-extras/tycho-version-bump-plugin/src/it/update-target/verify.groovy b/tycho-extras/tycho-version-bump-plugin/src/it/update-target/verify.groovy deleted file mode 100644 index 4cf90220e3..0000000000 --- a/tycho-extras/tycho-version-bump-plugin/src/it/update-target/verify.groovy +++ /dev/null @@ -1,10 +0,0 @@ -import java.io.*; -import groovy.xml.XmlSlurper; - -def target = new XmlSlurper().parse(new File(basedir, "update-target.target")); - -assert target.locations.location.unit.size() == 4; -assert target.locations.location.unit.find{ it.@id == "org.eclipse.equinox.executable.feature.group" }.@version == "3.8.900.v20200819-0940"; -assert target.locations.location.unit.find{ it.@id == "org.eclipse.jdt.feature.group" }.@version == "3.18.500.v20200902-1800"; -assert target.locations.location.unit.find{ it.@id == "org.eclipse.platform.ide" }.@version == "4.17.0.I20200902-1800"; -assert target.locations.location.unit.find{ it.@id == "org.eclipse.pde.feature.group" }.@version == "3.14.500.v20200902-1800"; diff --git a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/AbstractUpdateMojo.java b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/AbstractUpdateMojo.java index 04add136a3..cffceb5594 100644 --- a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/AbstractUpdateMojo.java +++ b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/AbstractUpdateMojo.java @@ -51,11 +51,17 @@ public void execute() throws MojoExecutionException, MojoFailureException { createResolver(); doUpdate(); } catch (Exception e) { + if (e instanceof MojoFailureException mfe) { + throw mfe; + } + if (e instanceof MojoExecutionException mee) { + throw mee; + } throw new MojoExecutionException("Could not update " + getFileToBeUpdated(), e); } } - protected abstract File getFileToBeUpdated(); + protected abstract File getFileToBeUpdated() throws MojoExecutionException, MojoFailureException; protected abstract void doUpdate() throws Exception; diff --git a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java index 9c6e9dce55..af9ab2a7ba 100644 --- a/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java +++ b/tycho-extras/tycho-version-bump-plugin/src/main/java/org/eclipse/tycho/versionbump/UpdateTargetMojo.java @@ -28,6 +28,7 @@ import javax.xml.parsers.ParserConfigurationException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -40,6 +41,8 @@ import org.eclipse.tycho.targetplatform.TargetDefinition.InstallableUnitLocation; import org.eclipse.tycho.targetplatform.TargetDefinition.Unit; import org.eclipse.tycho.targetplatform.TargetDefinitionFile; +import org.eclipse.tycho.targetplatform.TargetPlatformArtifactResolver; +import org.eclipse.tycho.targetplatform.TargetResolveException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -58,14 +61,18 @@ public class UpdateTargetMojo extends AbstractUpdateMojo { private TargetDefinitionVariableResolver varResolver; @Override - protected void doUpdate() throws IOException, URISyntaxException, ParserConfigurationException, SAXException { + protected void doUpdate() throws IOException, URISyntaxException, ParserConfigurationException, SAXException, + TargetResolveException, MojoFailureException { + File file = getFileToBeUpdated(); + getLog().info("Update target file " + file); Document target; - try (FileInputStream input = new FileInputStream(targetFile)) { + try (FileInputStream input = new FileInputStream(file)) { target = TargetDefinitionFile.parseDocument(input); - TargetDefinitionFile parsedTarget = TargetDefinitionFile.parse(target, targetFile.getAbsolutePath()); + TargetDefinitionFile parsedTarget = TargetDefinitionFile.parse(target, file.getAbsolutePath()); resolutionContext.setEnvironments(Collections.singletonList(TargetEnvironment.getRunningEnvironment())); resolutionContext.addTargetDefinition(new LatestVersionTarget(parsedTarget, varResolver)); + resolutionContext.setIgnoreLocalArtifacts(true); P2ResolutionResult result = p2.getTargetPlatformAsResolutionResult(resolutionContext, executionEnvironment); Map ius = new HashMap<>(); @@ -85,14 +92,22 @@ protected void doUpdate() throws IOException, URISyntaxException, ParserConfigur } } } - try (FileOutputStream outputStream = new FileOutputStream(targetFile)) { + try (FileOutputStream outputStream = new FileOutputStream(file)) { TargetDefinitionFile.writeDocument(target, outputStream); } } @Override - protected File getFileToBeUpdated() { - return targetFile; + protected File getFileToBeUpdated() throws MojoFailureException { + if (targetFile == null) { + try { + return TargetPlatformArtifactResolver.getMainTargetFile(project); + } catch (TargetResolveException e) { + throw new MojoFailureException(e); + } + } else { + return targetFile; + } } private static final class LatestVersionTarget implements TargetDefinition { diff --git a/tycho-extras/tycho-version-bump-plugin/src/it/update-target/pom.xml b/tycho-its/projects/tycho-version-bump-plugin/update-target/pom.xml similarity index 79% rename from tycho-extras/tycho-version-bump-plugin/src/it/update-target/pom.xml rename to tycho-its/projects/tycho-version-bump-plugin/update-target/pom.xml index 2861093f89..fea7b79f0b 100644 --- a/tycho-extras/tycho-version-bump-plugin/src/it/update-target/pom.xml +++ b/tycho-its/projects/tycho-version-bump-plugin/update-target/pom.xml @@ -17,20 +17,7 @@ update-target 1.0.0-SNAPSHOT eclipse-target-definition - - - - tycho-snapshots - ${tycho-snapshots-url} - - true - - - true - - - - + diff --git a/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target b/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target new file mode 100644 index 0000000000..eeed349f9b --- /dev/null +++ b/tycho-its/projects/tycho-version-bump-plugin/update-target/update-target.target @@ -0,0 +1,16 @@ + + + + + + + + + + + + + diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java b/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java new file mode 100644 index 0000000000..342ae9aa29 --- /dev/null +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/VersionBumpPluginTest.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2024 Christoph Läubrich and others. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.test; + +import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.io.FileInputStream; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.maven.it.Verifier; +import org.eclipse.tycho.targetplatform.TargetDefinition.InstallableUnitLocation; +import org.eclipse.tycho.targetplatform.TargetDefinition.Location; +import org.eclipse.tycho.targetplatform.TargetDefinition.Unit; +import org.eclipse.tycho.targetplatform.TargetDefinitionFile; +import org.eclipse.tycho.version.TychoVersion; +import org.junit.Test; +import org.w3c.dom.Document; + +public class VersionBumpPluginTest extends AbstractTychoIntegrationTest { + + @Test + public void testUpdateTarget() throws Exception { + Verifier verifier = getVerifier("tycho-version-bump-plugin/update-target", false, true); + String sourceTargetFile = "update-target.target"; + verifier.setSystemProperty("target", sourceTargetFile); + verifier.setSystemProperty("tycho.localArtifacts", "ignore"); + verifier.executeGoal("org.eclipse.tycho.extras:tycho-version-bump-plugin:" + TychoVersion.getTychoVersion() + + ":update-target"); + verifier.verifyErrorFreeLog(); + File targetFile = new File(verifier.getBasedir(), sourceTargetFile); + try (FileInputStream input = new FileInputStream(targetFile)) { + Document target = TargetDefinitionFile.parseDocument(input); + TargetDefinitionFile parsedTarget = TargetDefinitionFile.parse(target, targetFile.getAbsolutePath()); + List locations = parsedTarget.getLocations(); + InstallableUnitLocation iu = locations.stream().filter(InstallableUnitLocation.class::isInstance) + .map(InstallableUnitLocation.class::cast).findFirst() + .orElseThrow(() -> new AssertionError("IU Location not found!")); + List units = iu.getUnits(); + assertEquals(4, units.size()); + assertIUVersion("org.eclipse.equinox.executable.feature.group", "3.8.900.v20200819-0940", units, + targetFile); + assertIUVersion("org.eclipse.jdt.feature.group", "3.18.500.v20200902-1800", units, targetFile); + assertIUVersion("org.eclipse.platform.ide", "4.17.0.I20200902-1800", units, targetFile); + assertIUVersion("org.eclipse.pde.feature.group", "3.14.500.v20200902-1800", units, targetFile); + } + } + + private void assertIUVersion(String id, String version, List units, File targetFile) { + for (Unit unit : units) { + if (unit.getId().equals(id) && unit.getVersion().equals(version)) { + return; + } + } + fail("Unit with id " + id + " and version " + version + " not found: " + + units.stream().map(String::valueOf).collect(Collectors.joining(System.lineSeparator())) + + " in target file " + targetFile); + + } + +} diff --git a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java index 01ecc755d2..e4bd84a9db 100644 --- a/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java +++ b/tycho-targetplatform/src/main/java/org/eclipse/tycho/targetplatform/TargetDefinitionFile.java @@ -520,6 +520,11 @@ public String getVersion() { return version; } + @Override + public String toString() { + return "Unit [id=" + id + ", version=" + version + "]"; + } + } private TargetDefinitionFile(Document document, String origin) throws TargetDefinitionSyntaxException {