Skip to content

Commit

Permalink
Move code to split up a list of versions into series
Browse files Browse the repository at this point in the history
  • Loading branch information
vierbergenlars committed Aug 18, 2022
1 parent 14fcf4b commit c50aa32
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import eu.xenit.gradle.testrunner.versions.GradleVersionSeries;
import eu.xenit.gradle.testrunner.versions.GradleVersionSpec;
import eu.xenit.gradle.testrunner.versions.VersionFetcher;
import java.io.File;
Expand All @@ -20,6 +21,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
Expand Down Expand Up @@ -51,44 +53,28 @@ public static List<String[]> testData() {
}

boolean majorsOnly = Boolean.getBoolean("eu.xenit.gradle.integration.majorsOnly");
Comparator<GradleVersionSpec> byVersionNumber = Comparator.comparing(versionSpec -> GradleVersion.version(versionSpec.getVersion()));
List<GradleVersionSpec> fetchedVersions = VersionFetcher.fetchVersions()
.filter(VersionFetcher::isRelease)
.filter(VersionFetcher.greaterThan("5.6"))
.sorted(byVersionNumber)
.collect(Collectors.toList());

Map<String, List<GradleVersionSpec>> versionsByMinor = new HashMap<>();

for (GradleVersionSpec versionSpec : fetchedVersions) {
String[] versionParts = GradleVersion.version(versionSpec.getVersion()).getBaseVersion().getVersion().split("\\.");
String versionKey = majorsOnly?versionParts[0]:(versionParts[0]+"."+versionParts[1]);
if(versionsByMinor.containsKey(versionKey)) {
versionsByMinor.get(versionKey).add(versionSpec);
} else {
versionsByMinor.put(versionKey, new ArrayList<>(Collections.singletonList(versionSpec)));
if(majorsOnly) {
versionsByMinor.put(versionKey + "-lowest", Collections.singletonList(versionSpec));
}
}
}


List<String[]> versionsToBuild = versionsByMinor.values()
List<String[]> versionsToBuild = VersionFetcher.fetchVersionSeries()
// Only release versions >= 5.6
.map(series -> series.filter(VersionFetcher::isRelease)
.filter(VersionFetcher.greaterThanOrEqual("5.6")))
.filter(series -> !series.isEmpty())
// When selecting only majors, select the series with only the major version component
.filter(series -> !majorsOnly || series.getSeriesPriority() == 1)
// For series with only the major version component, select the first and last version, for series with the minor version component, only select the last version
.flatMap(series -> series.getSeriesPriority() == 1 ? Stream.of(series.getLowestVersion(), series.getHighestVersion()): Stream.of(
series.getHighestVersion()))
// Collect to a set to deduplicate versions
.collect(Collectors.toSet())
.stream()
.map(versions -> versions.stream()
// Find latest patch version
.sorted(byVersionNumber.reversed())
.findFirst()
.orElse(null)
)
.filter(Objects::nonNull)
.sorted(byVersionNumber)
// Re-sort by version number
.sorted(GradleVersionSpec.BY_VERSION_NUMBER)
// Convert to parameter for junit
.map(GradleVersionSpec::getVersion)
.map(version -> new String[]{version})
.collect(Collectors.toList());

List<String[]> slicedVersions = slice(versionsToBuild);

System.out.println(slicedVersions.stream()
.map(i -> i[0])
.collect(Collectors.joining(", ", "Running test on versions: ", "")));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package eu.xenit.gradle.testrunner.versions;

import java.util.Collection;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class GradleVersionSeries {
private final String series;
private final Set<GradleVersionSpec> versionSpecs;

public GradleVersionSeries(String series, Set<GradleVersionSpec> versionSpecs) {
this.series = series;
this.versionSpecs = versionSpecs;
}

public static GradleVersionSeries extract(String series, Collection<GradleVersionSpec> versionSpecs) {
Set<GradleVersionSpec> extractedVersions = versionSpecs.stream()
.filter(spec -> spec.getVersionSeries().contains(series))
.collect(Collectors.toSet());
return new GradleVersionSeries(series, extractedVersions);
}

public int getSeriesPriority() {
return series.split("\\.").length;
}

public GradleVersionSpec getLowestVersion() {
return versionSpecs.stream()
.min(GradleVersionSpec.BY_VERSION_NUMBER)
.orElse(null);
}

public GradleVersionSpec getHighestVersion() {
return versionSpecs.stream()
.max(GradleVersionSpec.BY_VERSION_NUMBER)
.orElse(null);
}

public GradleVersionSeries filter(Predicate<GradleVersionSpec> predicate) {
return new GradleVersionSeries(series, versionSpecs.stream().filter(predicate).collect(Collectors.toSet()));
}

public boolean isEmpty() {
return versionSpecs.isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package eu.xenit.gradle.testrunner.versions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import org.gradle.util.GradleVersion;

@JsonIgnoreProperties(ignoreUnknown = true)
public class GradleVersionSpec {
public static final Comparator<GradleVersionSpec> BY_VERSION_NUMBER = Comparator.comparing(versionSpec -> GradleVersion.version(versionSpec.getVersion()));

private String version;
private boolean snapshot;
private boolean broken;
Expand All @@ -15,6 +21,22 @@ public String getVersion() {
return version;
}

public Set<String> getVersionSeries() {
String[] parts = GradleVersion.version(version).getBaseVersion().getVersion().split("\\.");

Set<String> series = new HashSet<>();

for (int i = 0; i < parts.length; i++) {
String serie = "";
for (int j = 0; j <= i; j++) {
serie+=parts[j]+".";
}
series.add(serie.substring(0, Math.max(serie.length()-1, 0)));
}

return Collections.unmodifiableSet(series);
}

public void setVersion(String version) {
this.version = version;
}
Expand Down Expand Up @@ -42,4 +64,22 @@ public String getRcFor() {
public void setRcFor(String rcFor) {
this.rcFor = rcFor;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GradleVersionSpec that = (GradleVersionSpec) o;
return snapshot == that.snapshot && broken == that.broken && version.equals(that.version) && rcFor.equals(
that.rcFor);
}

@Override
public int hashCode() {
return Objects.hash(version, snapshot, broken, rcFor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.util.GradleVersion;

Expand All @@ -33,16 +36,8 @@ public static boolean isRelease(GradleVersionSpec versionSpec) {
return !versionSpec.isSnapshot() && versionSpec.getRcFor().isEmpty() && Objects.equals(gradleVersion.getBaseVersion(), gradleVersion);
}

public static boolean isBroken(GradleVersionSpec versionSpec) {
return versionSpec.isBroken();
}

public static Predicate<GradleVersionSpec> greaterThan(String version) {
return versionSpec -> GradleVersion.version(versionSpec.getVersion()).compareTo(GradleVersion.version(version)) > 0;
}

public static boolean firstPointRelease(GradleVersionSpec versionSpec) {
return GradleVersion.version(versionSpec.getVersion()).getBaseVersion().getVersion().endsWith(".0");
public static Predicate<GradleVersionSpec> greaterThanOrEqual(String version) {
return versionSpec -> GradleVersion.version(versionSpec.getVersion()).compareTo(GradleVersion.version(version)) >= 0;
}

public static Stream<GradleVersionSpec> fetchVersions() {
Expand All @@ -51,4 +46,16 @@ public static Stream<GradleVersionSpec> fetchVersions() {
.filter(((Predicate<? super GradleVersionSpec>) GradleVersionSpec::isBroken).negate());
}

public static Stream<GradleVersionSeries> fetchVersionSeries() {
List<GradleVersionSpec> versions = fetchVersions().collect(Collectors.toList());
Set<String> versionSeries = new HashSet<>();
for (GradleVersionSpec version : versions) {
versionSeries.addAll(version.getVersionSeries());
}

return versionSeries.stream()
.map(series -> GradleVersionSeries.extract(series, versions))
.filter(series -> series.getSeriesPriority() <= 2);
}

}

0 comments on commit c50aa32

Please sign in to comment.