Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oss-license-plugin: sort ArtifactInfo into list #268

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class DependencyTask extends DefaultTask {
} as AppDependencies
}

private static Set<ArtifactInfo> convertDependenciesToArtifactInfo(
private static List<ArtifactInfo> convertDependenciesToArtifactInfo(
AppDependencies appDependencies
) {
return appDependencies.libraryList.stream()
Expand All @@ -88,7 +88,8 @@ abstract class DependencyTask extends DefaultTask {
library.mavenLibrary.version
)
}
.collect(Collectors.toUnmodifiableSet())
.sorted(Comparator.comparing { it.toString() })
.collect(Collectors.toUnmodifiableList())
}

private static void initOutput(File outputDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.slf4j.LoggerFactory

import java.util.stream.Collectors
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

Expand Down Expand Up @@ -107,14 +108,16 @@ abstract class LicensesTask extends DefaultTask {
writeMetadata()
}

private static Set<ArtifactInfo> loadDependenciesJson(File jsonFile) {
private static List<ArtifactInfo> loadDependenciesJson(File jsonFile) {
def allDependencies = new JsonSlurper().parse(jsonFile)
def artifactInfoSet = new HashSet<ArtifactInfo>()
for (entry in allDependencies) {
ArtifactInfo artifactInfo = artifactInfoFromEntry(entry)
artifactInfoSet.add(artifactInfo)
}
artifactInfoSet.asImmutable()
artifactInfoSet.stream()
.sorted(Comparator.comparing { it.toString() })
.collect(Collectors.toUnmodifiableList())
}

private void addDebugLicense() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Before;
Expand Down Expand Up @@ -84,8 +87,8 @@ public void testAction_valuesConvertedToJson() throws Exception {
File outputJson = new File(outputDir, "test.json");
dependencyTask.getDependenciesJson().set(outputJson);
ImmutableSet<ArtifactInfo> expectedArtifacts = ImmutableSet.of(
new ArtifactInfo("org.group.id", "artifactId", "1.0.0"),
new ArtifactInfo("org.group.other", "other-artifact", "3.2.1")
new ArtifactInfo("org.group.other", "other-artifact", "3.2.1"),
new ArtifactInfo("org.group.id", "artifactId", "1.0.0")
);
AppDependencies appDependencies = createAppDependencies(expectedArtifacts);
File protoFile = writeAppDependencies(appDependencies, temporaryFolder.newFile());
Expand All @@ -102,8 +105,8 @@ public void testAction_withNonMavenDeps_nonMavenDepsIgnored() throws Exception {
File outputJson = new File(outputDir, "test.json");
dependencyTask.getDependenciesJson().set(outputJson);
ImmutableSet<ArtifactInfo> expectedArtifacts = ImmutableSet.of(
new ArtifactInfo("org.group.id", "artifactId", "1.0.0"),
new ArtifactInfo("org.group.other", "other-artifact", "3.2.1")
new ArtifactInfo("org.group.other", "other-artifact", "3.2.1"),
new ArtifactInfo("org.group.id", "artifactId", "1.0.0")
);
AppDependencies appDependencies = createAppDependencies(expectedArtifacts).toBuilder()
.addLibrary(Library.getDefaultInstance()) // There aren't any other library types supported.
Expand Down Expand Up @@ -131,11 +134,15 @@ public void testAction_depFileAbsent_writesAbsentDep() throws Exception {
private void verifyExpectedDependencies(ImmutableSet<ArtifactInfo> expectedArtifacts,
File outputJson) throws Exception {
Gson gson = new Gson();
List<ArtifactInfo> expectedArtifactsSorted = expectedArtifacts
.stream()
.sorted(Comparator.comparing(ArtifactInfo::toString))
.collect(Collectors.toUnmodifiableList());
try (FileReader reader = new FileReader(outputJson)) {
Type collectionOfArtifactInfo = new TypeToken<Collection<ArtifactInfo>>() {
}.getType();
Collection<ArtifactInfo> jsonArtifacts = gson.fromJson(reader, collectionOfArtifactInfo);
assertThat(jsonArtifacts).containsExactlyElementsIn(expectedArtifacts);
assertThat(jsonArtifacts).containsExactlyElementsIn(expectedArtifactsSorted).inOrder();
}
}

Expand Down