From 82c4cc75cb353af1758867bd503cd2c4ad89ed31 Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Thu, 25 Jan 2024 11:44:54 +0100 Subject: [PATCH] Fix dependency check for ignored licenses (#104736) 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 --- .../internal/precommit/DependencyLicensesTask.java | 7 ++++++- .../internal/precommit/DependencyLicensesTaskTests.java | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java index 092230a2b12ea..f71973c2fb15c 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java @@ -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; @@ -187,7 +188,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 @@ -227,6 +228,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. diff --git a/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTaskTests.java b/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTaskTests.java index 1a9284276043c..b909970638753 100644 --- a/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTaskTests.java +++ b/build-tools-internal/src/test/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTaskTests.java @@ -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(); }