Skip to content

Commit 0b6cd20

Browse files
authored
Fix dependency check for ignored licenses (#104736) (#104743)
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
1 parent 76be62a commit 0b6cd20

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.File;
3030
import java.util.ArrayList;
31+
import java.util.Arrays;
3132
import java.util.HashMap;
3233
import java.util.HashSet;
3334
import java.util.LinkedHashMap;
@@ -189,7 +190,7 @@ public void checkDependencies() {
189190
}
190191
File licensesDirAsFile = licensesDir.get().getAsFile();
191192
if (dependencies.isEmpty()) {
192-
if (licensesDirAsFile.exists()) {
193+
if (licensesDirAsFile.exists() && allIgnored() == false) {
193194
throw new GradleException("Licenses dir " + licensesDirAsFile + " exists, but there are no dependencies");
194195
}
195196
return; // no dependencies to check
@@ -229,6 +230,10 @@ public void checkDependencies() {
229230
sources.forEach((item, exists) -> failIfAnyMissing(item, exists, "sources"));
230231
}
231232

233+
private boolean allIgnored() {
234+
return Arrays.asList(getLicensesDir().listFiles()).stream().map(f -> f.getName()).allMatch(ignoreFiles::contains);
235+
}
236+
232237
// This is just a marker output folder to allow this task being up-to-date.
233238
// The check logic is exception driven so a failed tasks will not be defined
234239
// by this output but when successful we can safely mark the task as up-to-date.

build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTaskTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@ public void execute(DependencyLicensesTask dependencyLicensesTask) {
6363
public void givenProjectWithLicensesDirButNoDependenciesThenShouldThrowException() throws Exception {
6464
expectedException.expect(GradleException.class);
6565
expectedException.expectMessage(containsString("exists, but there are no dependencies"));
66+
getLicensesDir(project).mkdir();
67+
createFileIn(getLicensesDir(project), "groovy-LICENSE.txt", PERMISSIVE_LICENSE_TEXT);
68+
task.get().checkDependencies();
69+
}
6670

71+
@Test
72+
public void givenProjectWithLicensesDirButAllIgnoreFileAndNoDependencies() throws Exception {
6773
getLicensesDir(project).mkdir();
74+
String licenseFileName = "cloudcarbonfootprint-LICENSE.txt";
75+
createFileIn(getLicensesDir(project), licenseFileName, PERMISSIVE_LICENSE_TEXT);
76+
task.get().ignoreFile(licenseFileName);
6877
task.get().checkDependencies();
6978
}
7079

0 commit comments

Comments
 (0)