Skip to content

Commit

Permalink
added tests and fixed other test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
neloydas committed Feb 22, 2025
1 parent 9f0b163 commit 4d1450f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
60 changes: 50 additions & 10 deletions plugins/maven/__tests__/maven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,13 @@ describe("maven", () => {
await hooks.version.promise({ bump: Auto.SEMVER.patch });

const call = exec.mock.calls[0][1];
expect(call).toContain("tag");
expect(call).toContain("v1.0.0");
expect(call).toContain('"Update version to v1.0.0"');
expect(call).toContain("help:effective-pom");
expect(call).toContain("-Doutput=target/output.xml");

const secondCall = exec.mock.calls[1][1];
expect(secondCall).toContain("tag");
expect(secondCall).toContain("v1.0.0");
expect(secondCall).toContain('"Update version to v1.0.0"');
});

test("should version release", async () => {
Expand All @@ -290,9 +294,13 @@ describe("maven", () => {
await hooks.version.promise({ bump: Auto.SEMVER.patch });

const call = exec.mock.calls[0][1];
expect(call).toContain("tag");
expect(call).toContain("v1.0.1");
expect(call).toContain('"Update version to v1.0.1"');
expect(call).toContain("help:effective-pom");
expect(call).toContain("-Doutput=target/output.xml");

const secondCall = exec.mock.calls[1][1];
expect(secondCall).toContain("tag");
expect(secondCall).toContain("v1.0.1");
expect(secondCall).toContain('"Update version to v1.0.1"');
});

test("should replace the previousVersion with the newVersion", async () => {
Expand Down Expand Up @@ -359,9 +367,37 @@ describe("maven", () => {
);

const call = exec.mock.calls[0][1];
expect(call).toContain("versions:set");
expect(call).toContain("-DgenerateBackupPoms=false");
expect(call).toContain("-DnewVersion=1.0.0");
expect(call).toContain("help:effective-pom");
expect(call).toContain("-Doutput=target/output.xml");

const secondCall = exec.mock.calls[1][1];
expect(secondCall).toContain("versions:set");
expect(secondCall).toContain("-DgenerateBackupPoms=false");
expect(secondCall).toContain("-DnewVersion=1.0.0");
});

test("should create effective POM file successfully", async () => {
mockReadFile(`<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<version>1.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
</project>`);

await hooks.beforeRun.promise({} as any);

const call = exec.mock.calls[0][1];
expect(call).toContain("help:effective-pom");
expect(call).toContain("-Doutput=target/output.xml");
});

test("should replace the parent previousVersion with the newVersion", async () => {
Expand Down Expand Up @@ -468,7 +504,11 @@ describe("maven", () => {

await hooks.publish.promise({ bump: Auto.SEMVER.patch });

expect(exec.mock.calls[0][1]).toContain("deploy");
const call = exec.mock.calls[0][1];
expect(call).toContain("help:effective-pom");
expect(call).toContain("-Doutput=target/output.xml");

expect(exec.mock.calls[1][1]).toContain("deploy");
});
});
});
6 changes: 3 additions & 3 deletions plugins/maven/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export default class MavenPlugin implements IPlugin {
}

/** Detect whether the parent pom.xml has the versions-maven-plugin **/
private static async detectVersionMavenPlugin(): Promise<boolean> {
await maven.createEffectivePom(pluginOptions, Auto);
private static async detectVersionMavenPlugin(options: IMavenPluginOptions, auto: Auto): Promise<boolean> {
await maven.createEffectivePom(options, auto);
const pom = await getPom("target/output.xml");
const pomDom = new jsdom.JSDOM(pom.pomXml, { contentType: "text/xml" })
.window.document;
Expand Down Expand Up @@ -183,7 +183,7 @@ export default class MavenPlugin implements IPlugin {
this.snapshotRelease = true;
}

this.versionsMavenPlugin = await MavenPlugin.detectVersionMavenPlugin();
this.versionsMavenPlugin = await MavenPlugin.detectVersionMavenPlugin(this.options, auto);
});

auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => {
Expand Down
13 changes: 9 additions & 4 deletions plugins/maven/src/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ export async function createEffectivePom(
`Creating effective-pom file in target directory`
);

await executeMaven(options, [
"help:effective-pom",
"-Doutput='target/output.xml'"
]);
try {
await executeMaven(options, [
"help:effective-pom",
"-Doutput=target/output.xml"
]);
} catch (error) {
auto.logger.verbose.error(`Failed to create effective pom: ${error.message}`);
throw new Error(`Failed to create effective pom in target directory`);
}
}

0 comments on commit 4d1450f

Please sign in to comment.