Skip to content

Commit

Permalink
Fix dependency check for ignored licenses (#104736) (#104743)
Browse files Browse the repository at this point in the history
if there are no dependencies declared and the license file is marked as
ignored, no exception should be thrown and dependencylicense check
should pass.

Fixes issues we identified in #104628
  • Loading branch information
breskeby committed Jan 25, 2024
1 parent 76be62a commit 0b6cd20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -189,7 +190,7 @@ public void checkDependencies() {
}
File licensesDirAsFile = licensesDir.get().getAsFile();
if (dependencies.isEmpty()) {
if (licensesDirAsFile.exists()) {
if (licensesDirAsFile.exists() && allIgnored() == false) {
throw new GradleException("Licenses dir " + licensesDirAsFile + " exists, but there are no dependencies");
}
return; // no dependencies to check
Expand Down Expand Up @@ -229,6 +230,10 @@ public void checkDependencies() {
sources.forEach((item, exists) -> failIfAnyMissing(item, exists, "sources"));
}

private boolean allIgnored() {
return Arrays.asList(getLicensesDir().listFiles()).stream().map(f -> f.getName()).allMatch(ignoreFiles::contains);
}

// This is just a marker output folder to allow this task being up-to-date.
// The check logic is exception driven so a failed tasks will not be defined
// by this output but when successful we can safely mark the task as up-to-date.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ public void execute(DependencyLicensesTask dependencyLicensesTask) {
public void givenProjectWithLicensesDirButNoDependenciesThenShouldThrowException() throws Exception {
expectedException.expect(GradleException.class);
expectedException.expectMessage(containsString("exists, but there are no dependencies"));
getLicensesDir(project).mkdir();
createFileIn(getLicensesDir(project), "groovy-LICENSE.txt", PERMISSIVE_LICENSE_TEXT);
task.get().checkDependencies();
}

@Test
public void givenProjectWithLicensesDirButAllIgnoreFileAndNoDependencies() throws Exception {
getLicensesDir(project).mkdir();
String licenseFileName = "cloudcarbonfootprint-LICENSE.txt";
createFileIn(getLicensesDir(project), licenseFileName, PERMISSIVE_LICENSE_TEXT);
task.get().ignoreFile(licenseFileName);
task.get().checkDependencies();
}

Expand Down

0 comments on commit 0b6cd20

Please sign in to comment.