Verify Antora display_versionsaving

Issue gh-10765
This commit is contained in:
Rob Winch 2022-01-18 12:38:03 -06:00
parent 55cf7c9683
commit ade0872d09
3 changed files with 38 additions and 3 deletions

View File

@ -20,6 +20,7 @@ public class CheckAntoraVersionPlugin implements Plugin<Project> {
antoraCheckVersion.setDescription("Checks the antora.yml version properties match the Gradle version"); antoraCheckVersion.setDescription("Checks the antora.yml version properties match the Gradle version");
antoraCheckVersion.getAntoraVersion().convention(project.provider(() -> getDefaultAntoraVersion(project))); antoraCheckVersion.getAntoraVersion().convention(project.provider(() -> getDefaultAntoraVersion(project)));
antoraCheckVersion.getAntoraPrerelease().convention(project.provider(() -> getDefaultAntoraPrerelease(project))); antoraCheckVersion.getAntoraPrerelease().convention(project.provider(() -> getDefaultAntoraPrerelease(project)));
antoraCheckVersion.getAntoraDisplayVersion().convention(project.provider(() -> getDefaultAntoraDisplayVersion(project)));
antoraCheckVersion.getAntoraYmlFile().fileProvider(project.provider(() -> project.file("antora.yml"))); antoraCheckVersion.getAntoraYmlFile().fileProvider(project.provider(() -> project.file("antora.yml")));
} }
}); });
@ -54,6 +55,14 @@ public class CheckAntoraVersionPlugin implements Plugin<Project> {
return null; return null;
} }
private static String getDefaultAntoraDisplayVersion(Project project) {
String projectVersion = getProjectVersion(project);
if (!isSnapshot(projectVersion) && isPreRelease(projectVersion)) {
return getDefaultAntoraVersion(project);
}
return null;
}
private static String getProjectVersion(Project project) { private static String getProjectVersion(Project project) {
Object projectVersion = project.getVersion(); Object projectVersion = project.getVersion();
if (projectVersion == null) { if (projectVersion == null) {

View File

@ -22,6 +22,7 @@ public abstract class CheckAntoraVersionTask extends DefaultTask {
File antoraYmlFile = getAntoraYmlFile().getAsFile().get(); File antoraYmlFile = getAntoraYmlFile().getAsFile().get();
String expectedAntoraVersion = getAntoraVersion().get(); String expectedAntoraVersion = getAntoraVersion().get();
String expectedAntoraPrerelease = getAntoraPrerelease().getOrElse(null); String expectedAntoraPrerelease = getAntoraPrerelease().getOrElse(null);
String expectedAntoraDisplayVersion = getAntoraDisplayVersion().getOrElse(null);
Representer representer = new Representer(); Representer representer = new Representer();
representer.getPropertyUtils().setSkipMissingProperties(true); representer.getPropertyUtils().setSkipMissingProperties(true);
@ -32,10 +33,17 @@ public abstract class CheckAntoraVersionTask extends DefaultTask {
String actualAntoraPrerelease = antoraYml.getPrerelease(); String actualAntoraPrerelease = antoraYml.getPrerelease();
boolean preReleaseMatches = antoraYml.getPrerelease() == null && expectedAntoraPrerelease == null || boolean preReleaseMatches = antoraYml.getPrerelease() == null && expectedAntoraPrerelease == null ||
(actualAntoraPrerelease != null && actualAntoraPrerelease.equals(expectedAntoraPrerelease)); (actualAntoraPrerelease != null && actualAntoraPrerelease.equals(expectedAntoraPrerelease));
String actualAntoraDisplayVersion = antoraYml.getDisplay_version();
boolean displayVersionMatches = antoraYml.getDisplay_version() == null && expectedAntoraDisplayVersion == null ||
(actualAntoraDisplayVersion != null && actualAntoraDisplayVersion.equals(expectedAntoraDisplayVersion));
String actualAntoraVersion = antoraYml.getVersion(); String actualAntoraVersion = antoraYml.getVersion();
if (!preReleaseMatches || if (!preReleaseMatches ||
!displayVersionMatches ||
!expectedAntoraVersion.equals(actualAntoraVersion)) { !expectedAntoraVersion.equals(actualAntoraVersion)) {
throw new GradleException("The Gradle version of '" + getProject().getVersion() + "' should have version: '" + expectedAntoraVersion + "' and prerelease: '" + expectedAntoraPrerelease + "' defined in " + antoraYmlFile + " but got version: '" + actualAntoraVersion+"' and prerelease: '" + actualAntoraPrerelease + "'"); throw new GradleException("The Gradle version of '" + getProject().getVersion() + "' should have version: '"
+ expectedAntoraVersion + "' prerelease: '" + expectedAntoraPrerelease + "' display_version: '"
+ expectedAntoraDisplayVersion + "' defined in " + antoraYmlFile + " but got version: '"
+ actualAntoraVersion + "' prerelease: '" + actualAntoraPrerelease + "' display_version: '" + actualAntoraDisplayVersion + "'");
} }
} }
@ -48,11 +56,16 @@ public abstract class CheckAntoraVersionTask extends DefaultTask {
@Input @Input
public abstract Property<String> getAntoraPrerelease(); public abstract Property<String> getAntoraPrerelease();
@Input
public abstract Property<String> getAntoraDisplayVersion();
public static class AntoraYml { public static class AntoraYml {
private String version; private String version;
private String prerelease; private String prerelease;
private String display_version;
public String getVersion() { public String getVersion() {
return version; return version;
} }
@ -68,5 +81,13 @@ public abstract class CheckAntoraVersionTask extends DefaultTask {
public void setPrerelease(String prerelease) { public void setPrerelease(String prerelease) {
this.prerelease = prerelease; this.prerelease = prerelease;
} }
public String getDisplay_version() {
return display_version;
}
public void setDisplay_version(String display_version) {
this.display_version = display_version;
}
} }
} }

View File

@ -31,6 +31,7 @@ class CheckAntoraVersionPluginTests {
CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task; CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task;
assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0"); assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0");
assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("-SNAPSHOT"); assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("-SNAPSHOT");
assertThat(checkAntoraVersionTask.getAntoraDisplayVersion().isPresent()).isFalse();
assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml")); assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml"));
} }
@ -48,6 +49,7 @@ class CheckAntoraVersionPluginTests {
CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task; CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task;
assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0-M1"); assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0-M1");
assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("true"); assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("true");
assertThat(checkAntoraVersionTask.getAntoraDisplayVersion().get()).isEqualTo(checkAntoraVersionTask.getAntoraVersion().get());
assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml")); assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml"));
} }
@ -65,6 +67,7 @@ class CheckAntoraVersionPluginTests {
CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task; CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task;
assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0-RC1"); assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0-RC1");
assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("true"); assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("true");
assertThat(checkAntoraVersionTask.getAntoraDisplayVersion().get()).isEqualTo(checkAntoraVersionTask.getAntoraVersion().get());
assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml")); assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml"));
} }
@ -82,6 +85,7 @@ class CheckAntoraVersionPluginTests {
CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task; CheckAntoraVersionTask checkAntoraVersionTask = (CheckAntoraVersionTask) task;
assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0"); assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0");
assertThat(checkAntoraVersionTask.getAntoraPrerelease().isPresent()).isFalse(); assertThat(checkAntoraVersionTask.getAntoraPrerelease().isPresent()).isFalse();
assertThat(checkAntoraVersionTask.getAntoraDisplayVersion().isPresent()).isFalse();
assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml")); assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml"));
} }
@ -97,6 +101,7 @@ class CheckAntoraVersionPluginTests {
checkAntoraVersionTask.getAntoraPrerelease().set("-SNAPSHOT"); checkAntoraVersionTask.getAntoraPrerelease().set("-SNAPSHOT");
assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0"); assertThat(checkAntoraVersionTask.getAntoraVersion().get()).isEqualTo("1.0.0");
assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("-SNAPSHOT"); assertThat(checkAntoraVersionTask.getAntoraPrerelease().get()).isEqualTo("-SNAPSHOT");
assertThat(checkAntoraVersionTask.getAntoraDisplayVersion().isPresent()).isFalse();
assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml")); assertThat(checkAntoraVersionTask.getAntoraYmlFile().getAsFile().get()).isEqualTo(project.file("antora.yml"));
} }
@ -170,7 +175,7 @@ class CheckAntoraVersionPluginTests {
String expectedVersion = "1.0.0-M1"; String expectedVersion = "1.0.0-M1";
Project project = ProjectBuilder.builder().build(); Project project = ProjectBuilder.builder().build();
File rootDir = project.getRootDir(); File rootDir = project.getRootDir();
IOUtils.write("version: '1.0.0-M1'\nprerelease: 'true'", new FileOutputStream(new File(rootDir, "antora.yml")), StandardCharsets.UTF_8); IOUtils.write("version: '1.0.0-M1'\nprerelease: 'true'\ndisplay_version: '1.0.0-M1'", new FileOutputStream(new File(rootDir, "antora.yml")), StandardCharsets.UTF_8);
project.setVersion(expectedVersion); project.setVersion(expectedVersion);
project.getPluginManager().apply(CheckAntoraVersionPlugin.class); project.getPluginManager().apply(CheckAntoraVersionPlugin.class);
@ -187,7 +192,7 @@ class CheckAntoraVersionPluginTests {
String expectedVersion = "1.0.0-RC1"; String expectedVersion = "1.0.0-RC1";
Project project = ProjectBuilder.builder().build(); Project project = ProjectBuilder.builder().build();
File rootDir = project.getRootDir(); File rootDir = project.getRootDir();
IOUtils.write("version: '1.0.0-RC1'\nprerelease: 'true'", new FileOutputStream(new File(rootDir, "antora.yml")), StandardCharsets.UTF_8); IOUtils.write("version: '1.0.0-RC1'\nprerelease: 'true'\ndisplay_version: '1.0.0-RC1'", new FileOutputStream(new File(rootDir, "antora.yml")), StandardCharsets.UTF_8);
project.setVersion(expectedVersion); project.setVersion(expectedVersion);
project.getPluginManager().apply(CheckAntoraVersionPlugin.class); project.getPluginManager().apply(CheckAntoraVersionPlugin.class);