Skip to content

Commit

Permalink
build: set up test plugion version verification
Browse files Browse the repository at this point in the history
  • Loading branch information
cleydyr committed Dec 28, 2024
1 parent 70928c2 commit 8031df5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/java-ci-linux-musl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
java-version: '11'
distribution: 'adopt'

- name: Basic plugin version verification
run: java -ea scripts/CheckPomVersions.java

- name: Build Docker Image
run: docker build -f Dockerfile-test-alpine -t alpine-test-image .

Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/java-ci-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'adopt'

- name: Basic plugin version verification
run: java -ea scripts/CheckPomVersions.java

- name: Build with Maven
run: mvn --batch-mode --update-snapshots clean test spotless:check ossindex:audit
5 changes: 5 additions & 0 deletions .github/workflows/java-ci-mac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'adopt'

- name: Basic plugin version verification
run: java -ea scripts/CheckPomVersions.java

- name: Build with Maven
run: mvn --batch-mode --update-snapshots clean test spotless:check ossindex:audit
5 changes: 5 additions & 0 deletions .github/workflows/java-ci-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'adopt'

- name: Basic plugin version verification
run: java -ea scripts/CheckPomVersions.java

- name: Build with Maven
run: mvn --batch-mode --update-snapshots clean test spotless:check ossindex:audit
3 changes: 3 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
mkdir -p $HOME/.m2
echo "<settings><servers><server><id>ossrh</id><username>${{ secrets.SONATYPE_USERNAME }}</username><password>${{ secrets.SONATYPE_PASSWORD }}</password></server></servers></settings>" > $HOME/.m2/settings.xml
- name: Basic plugin version verification
run: java -ea scripts/CheckPomVersions.java

- name: Build and Test
run: mvn --batch-mode --update-snapshots clean test spotless:check ossindex:audit

Expand Down
55 changes: 55 additions & 0 deletions scripts/CheckPomVersions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
This script checks the versions of the pom.xml files in the repository.
There are two test pom files in this repo: src/test/resources/musl/test-project/pom.xml and
src/test/resources/test-project/pom.xml. We want to ensure they use the same version as the plugin version set in the
root pom.xml. This script automatically checks if that's the case.
*/

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;

public class CheckPomVersions {
public static void main(String[] args) {
String pluginVersion = getPluginVersion("pom.xml", "/project/version");

String testProjectUsedVersion = getPluginVersion("src/test/resources/test-project/pom.xml", "/project/build/plugins/plugin[artifactId='dart-sass-maven-plugin']/version");
String muslTestProjectUsedVersion = getPluginVersion("src/test/resources/musl/test-project/pom.xml", "/project/build/plugins/plugin[artifactId='dart-sass-maven-plugin']/version");

assert testProjectUsedVersion != null;
assert muslTestProjectUsedVersion != null;
assert pluginVersion != null;

assert pluginVersion.equals(testProjectUsedVersion) : String.format("Plugin version in test-project pom.xml (%s) does not match the root plugin version (%s).", testProjectUsedVersion, pluginVersion);
assert pluginVersion.equals(muslTestProjectUsedVersion) : String.format("Plugin version in musl test-project pom.xml (%s) does not match the root plugin version (%s).", muslTestProjectUsedVersion, pluginVersion);

}

private static String getPluginVersion(String fileName, String xpathExpression) {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

File pomFile = new File(fileName);

Document doc = builder.parse(pomFile);

XPathFactory xPathfactory = XPathFactory.newInstance();

XPath xpath = xPathfactory.newXPath();

XPathExpression expr = xpath.compile(xpathExpression);

return (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (IOException e) {
return null;
} catch (XPathExpressionException | ParserConfigurationException | SAXException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 8031df5

Please sign in to comment.