diff --git a/README.md b/README.md index 002c53a4..8086e6bd 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,10 @@ licenseReport { // This is for the allowed-licenses-file in checkLicense Task // Accepts File, URL or String path to local or remote file allowedLicensesFile = project.layout.projectDirectory.file("config/allowed-licenses.json").asFile + // (default) OneRequiredLicenseChecker: a dependency is good, if any of its licenses are matched with allowedLicenses + // AllRequiredLicenseChecker: a dependency is good, if all of its (non-null) licenses are matched with allowedLicenses + // any class implementing LicenseChecker can be provided here + licenseChecker = new com.github.jk1.license.check.OneRequiredLicenseChecker() } ``` diff --git a/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy b/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy index 94018413..ffe50b4c 100644 --- a/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy +++ b/src/main/groovy/com/github/jk1/license/LicenseReportExtension.groovy @@ -15,6 +15,8 @@ */ package com.github.jk1.license +import com.github.jk1.license.check.LicenseChecker +import com.github.jk1.license.check.OneRequiredLicenseChecker import com.github.jk1.license.filter.DependencyFilter import com.github.jk1.license.importer.DependencyDataImporter import com.github.jk1.license.render.ReportRenderer @@ -41,6 +43,7 @@ class LicenseReportExtension { public String[] excludeGroups public String[] excludes public Object allowedLicensesFile + public LicenseChecker licenseChecker LicenseReportExtension(Project project) { unionParentPomLicenses = true @@ -54,6 +57,7 @@ class LicenseReportExtension { excludes = [] importers = [] filters = [] + licenseChecker = new OneRequiredLicenseChecker() } @Nested @@ -103,6 +107,8 @@ class LicenseReportExtension { snapshot += excludes snapshot << 'unionParentPomLicenses' snapshot += unionParentPomLicenses + snapshot << "licenseChecker" + snapshot += licenseChecker.class.name snapshot.join("!") } diff --git a/src/main/groovy/com/github/jk1/license/check/AllRequiredLicenseChecker.groovy b/src/main/groovy/com/github/jk1/license/check/AllRequiredLicenseChecker.groovy new file mode 100644 index 00000000..ced4812f --- /dev/null +++ b/src/main/groovy/com/github/jk1/license/check/AllRequiredLicenseChecker.groovy @@ -0,0 +1,69 @@ +/* + * Copyright 2018 Evgeny Naumenko + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jk1.license.check + +/** + * All licenses of a dependency must be found inside allowedLicenses to pass. + */ +class AllRequiredLicenseChecker implements LicenseChecker { + @Override + List>> checkAllDependencyLicensesAreAllowed(List allowedLicenses, List allDependencies) { + removeNullLicenses(allDependencies) + List>> result = new ArrayList<>() + for (Dependency dependency : (allDependencies)) { + List perDependencyAllowedLicenses = allowedLicenses.findAll { isDependencyNameMatchesAllowedLicense(dependency, it) && isDependencyVersionMatchesAllowedLicense(dependency, it) } + // allowedLicense matches anything, so we don't need to further check + if (perDependencyAllowedLicenses.any { it.moduleLicense == null || it.moduleLicense == ".*" }) { + continue + } + List notAllowedLicenses = dependency.moduleLicenses.findAll { !isDependencyLicenseMatchesAllowedLicense(it, perDependencyAllowedLicenses) } + if (!notAllowedLicenses.isEmpty()) { + result.add(new Tuple2(dependency, notAllowedLicenses)) + } + } + return result + } + + private static boolean isDependencyNameMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { + return dependency.moduleName ==~ allowedLicense.moduleName || allowedLicense.moduleName == null || dependency.moduleName == allowedLicense.moduleName + } + + private static boolean isDependencyVersionMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { + return dependency.moduleVersion ==~ allowedLicense.moduleVersion || allowedLicense.moduleVersion == null || dependency.moduleVersion == allowedLicense.moduleVersion + } + + private static boolean isDependencyLicenseMatchesAllowedLicense(ModuleLicense moduleLicense, List allowedLicenses) { + for (AllowedLicense allowedLicense : allowedLicenses) { + if (allowedLicense.moduleLicense == null || allowedLicense.moduleLicense == ".*") return true + + if (moduleLicense.moduleLicense ==~ allowedLicense.moduleLicense || moduleLicense.moduleLicense == allowedLicense.moduleLicense) return true + } + return false + } + + /** + * removes 'null'-licenses from dependencies which have at least one more license + */ + private static void removeNullLicenses(List dependencies) { + for (Dependency dependency : dependencies) { + if (dependency.moduleLicenses.any { it.moduleLicense == null } && !dependency.moduleLicenses.every { + it.moduleLicense == null + }) { + dependency.moduleLicenses = dependency.moduleLicenses.findAll { it.moduleLicense != null } + } + } + } +} diff --git a/src/main/groovy/com/github/jk1/license/check/LicenseChecker.groovy b/src/main/groovy/com/github/jk1/license/check/LicenseChecker.groovy index 7112ddcc..d8486d0b 100644 --- a/src/main/groovy/com/github/jk1/license/check/LicenseChecker.groovy +++ b/src/main/groovy/com/github/jk1/license/check/LicenseChecker.groovy @@ -18,70 +18,50 @@ package com.github.jk1.license.check import groovy.json.JsonOutput import org.gradle.api.GradleException -class LicenseChecker { +/** + * This class compares the found licences with the allowed licenses and creates a report for any missing license + */ +interface LicenseChecker extends Serializable { + List>> checkAllDependencyLicensesAreAllowed( + List allowedLicenses, + List allDependencies) - void checkAllDependencyLicensesAreAllowed( - Object allowedLicensesFile, File projectLicensesDataFile, File notPassedDependenciesOutputFile) { - List allDependencies = LicenseCheckerFileReader.importDependencies(projectLicensesDataFile) - List allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicensesFile) - List notPassedDependencies = searchForNotAllowedDependencies(allDependencies, allowedLicenses) - generateNotPassedDependenciesFile(notPassedDependencies, notPassedDependenciesOutputFile) + default void checkAllDependencyLicensesAreAllowed( + Object allowedLicensesFile, File projectLicensesDataFile, File notPassedDependenciesOutputFile) { + def notPassedDependencies = checkAllDependencyLicensesAreAllowed( + parseAllowedLicenseFile(allowedLicensesFile), getProjectDependencies(projectLicensesDataFile)) + generateNotPassedDependenciesFile(notPassedDependencies, notPassedDependenciesOutputFile) if (!notPassedDependencies.isEmpty()) { - throw new GradleException("Some library licenses are not allowed.\n" + - "Read [$notPassedDependenciesOutputFile.path] for more information.") - } - } - - private List searchForNotAllowedDependencies( - List dependencies, List allowedLicenses) { - return dependencies.findAll { !isDependencyHasAllowedLicense(it, allowedLicenses) } - } - - private void generateNotPassedDependenciesFile( - List notPassedDependencies, File notPassedDependenciesOutputFile) { - notPassedDependenciesOutputFile.text = - JsonOutput.prettyPrint(JsonOutput.toJson( - ["dependenciesWithoutAllowedLicenses": notPassedDependencies.collect { toAllowedLicenseList(it) }.flatten()])) - } - - private boolean isDependencyHasAllowedLicense(Dependency dependency, List allowedLicenses) { - for(allowedLicense in allowedLicenses) { - if (isDependencyMatchesAllowedLicense(dependency, allowedLicense)) return true + throw new GradleException("Some library licenses are not allowed:\n" + + "$notPassedDependenciesOutputFile.text\n\n" + + "Read [$notPassedDependenciesOutputFile.path] for more information.") } - return false - } - - private boolean isDependencyMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { - return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) && - isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) && - isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense) } - private boolean isDependencyNameMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { - return dependency.moduleName ==~ allowedLicense.moduleName || allowedLicense.moduleName == null || - dependency.moduleName == allowedLicense.moduleName + default List parseAllowedLicenseFile(Object allowedLicenseFile) { + return LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseFile) } - private boolean isDependencyVersionMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { - return dependency.moduleVersion ==~ allowedLicense.moduleVersion || allowedLicense.moduleVersion == null || - dependency.moduleVersion == allowedLicense.moduleVersion + default List getProjectDependencies(File depenenciesFile) { + return LicenseCheckerFileReader.importDependencies(depenenciesFile) } - private boolean isDependencyLicenseMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { - if (allowedLicense.moduleLicense == null || allowedLicense.moduleLicense == ".*") return true - for (moduleLicenses in dependency.moduleLicenses) - if (moduleLicenses.moduleLicense ==~ allowedLicense.moduleLicense || - moduleLicenses.moduleLicense == allowedLicense.moduleLicense) return true - return false + default void generateNotPassedDependenciesFile(List>> notPassedDependencies, File notPassedDependenciesOutputFile) { + notPassedDependenciesOutputFile.text = JsonOutput.prettyPrint( + JsonOutput.toJson([ + "dependenciesWithoutAllowedLicenses": notPassedDependencies.collect { + toAllowedLicenseList(it.getV1(), it.getV2()) + }.flatten() + ])) } - private List toAllowedLicenseList(Dependency dependency) { - if (dependency.moduleLicenses.isEmpty()) { - return [ new AllowedLicense(dependency.moduleName, dependency.moduleVersion, null) ] + default List toAllowedLicenseList(Dependency dependency, List moduleLicenses) { + if (moduleLicenses.isEmpty()) { + return [new AllowedLicense(dependency.moduleName, dependency.moduleVersion, null)] } else { - return dependency.moduleLicenses.collect { new AllowedLicense(dependency.moduleName, dependency.moduleVersion, it.moduleLicense) } + return moduleLicenses.findAll { it }.collect { new AllowedLicense(dependency.moduleName, dependency.moduleVersion, it.moduleLicense) } } } } diff --git a/src/main/groovy/com/github/jk1/license/check/OneRequiredLicenseChecker.groovy b/src/main/groovy/com/github/jk1/license/check/OneRequiredLicenseChecker.groovy new file mode 100644 index 00000000..772f2059 --- /dev/null +++ b/src/main/groovy/com/github/jk1/license/check/OneRequiredLicenseChecker.groovy @@ -0,0 +1,60 @@ +/* + * Copyright 2018 Evgeny Naumenko + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jk1.license.check + +/** + * A Dependency, which has at least one license inside allowedLicenses, will pass. + */ +class OneRequiredLicenseChecker implements LicenseChecker { + + @Override + List>> checkAllDependencyLicensesAreAllowed(List allowedLicenses, List allDependencies) { + List notPassedDependencies = allDependencies.findAll { !isDependencyHasAllowedLicense(it, allowedLicenses) } + return notPassedDependencies.collect { new Tuple2(it, it.moduleLicenses.isEmpty() ? null : it.moduleLicenses) } + } + + private boolean isDependencyHasAllowedLicense(Dependency dependency, List allowedLicenses) { + for (allowedLicense in allowedLicenses) { + if (isDependencyMatchesAllowedLicense(dependency, allowedLicense)) return true + } + return false + } + + private boolean isDependencyMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { + return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) && + isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) && + isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense) + } + + private boolean isDependencyNameMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { + return dependency.moduleName ==~ allowedLicense.moduleName || allowedLicense.moduleName == null || + dependency.moduleName == allowedLicense.moduleName + } + + private boolean isDependencyVersionMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { + return dependency.moduleVersion ==~ allowedLicense.moduleVersion || allowedLicense.moduleVersion == null || + dependency.moduleVersion == allowedLicense.moduleVersion + } + + private boolean isDependencyLicenseMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { + if (allowedLicense.moduleLicense == null || allowedLicense.moduleLicense == ".*") return true + + for (moduleLicenses in dependency.moduleLicenses) + if (moduleLicenses.moduleLicense ==~ allowedLicense.moduleLicense || + moduleLicenses.moduleLicense == allowedLicense.moduleLicense) return true + return false + } +} diff --git a/src/main/groovy/com/github/jk1/license/task/CheckLicenseTask.groovy b/src/main/groovy/com/github/jk1/license/task/CheckLicenseTask.groovy index 5437ac2f..dbbaf238 100644 --- a/src/main/groovy/com/github/jk1/license/task/CheckLicenseTask.groovy +++ b/src/main/groovy/com/github/jk1/license/task/CheckLicenseTask.groovy @@ -56,6 +56,11 @@ class CheckLicenseTask extends DefaultTask { return new File("${config.absoluteOutputDir}/${PROJECT_JSON_FOR_LICENSE_CHECKING_FILE}") } + @Input + LicenseChecker getLicenseChecker() { + return config.licenseChecker + } + @OutputFile File getNotPassedDependenciesFile() { new File("${config.absoluteOutputDir}/$NOT_PASSED_DEPENDENCIES_FILE") @@ -64,9 +69,9 @@ class CheckLicenseTask extends DefaultTask { @TaskAction void checkLicense() { LOGGER.info("Startup CheckLicense for ${config.projects.first()}") - LicenseChecker licenseChecker = new LicenseChecker() + LicenseChecker licenseChecker = getLicenseChecker() LOGGER.info("Check licenses if they are allowed to use.") licenseChecker.checkAllDependencyLicensesAreAllowed( - getAllowedLicenseFile(), getProjectDependenciesData(), notPassedDependenciesFile) + getAllowedLicenseFile(), getProjectDependenciesData(), notPassedDependenciesFile) } } diff --git a/src/test/groovy/com/github/jk1/license/check/LicenseCheckerSpec.groovy b/src/test/groovy/com/github/jk1/license/check/LicenseCheckerSpec.groovy index 5096184b..5487524e 100644 --- a/src/test/groovy/com/github/jk1/license/check/LicenseCheckerSpec.groovy +++ b/src/test/groovy/com/github/jk1/license/check/LicenseCheckerSpec.groovy @@ -83,7 +83,8 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() + licenseChecker.parseAllowedLicenseFile(allowedLicenseFile) then: noExceptionThrown() @@ -125,9 +126,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -161,9 +162,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: @@ -198,9 +199,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -230,9 +231,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -307,9 +308,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -341,9 +342,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -379,9 +380,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -414,9 +415,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -453,9 +454,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -482,9 +483,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: @@ -513,9 +514,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: @@ -549,9 +550,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -586,9 +587,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -620,9 +621,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -657,9 +658,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -695,9 +696,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -730,9 +731,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -768,9 +769,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -802,9 +803,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -846,9 +847,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: noExceptionThrown() @@ -881,9 +882,9 @@ class LicenseCheckerSpec extends Specification { }""" when: - def licenseChecker = new LicenseChecker() + def licenseChecker = new OneRequiredLicenseChecker() licenseChecker.checkAllDependencyLicensesAreAllowed( - allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) then: def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) @@ -891,4 +892,154 @@ class LicenseCheckerSpec extends Specification { notPassedDependencies.moduleLicense == ["License1", "License2", "License3"] thrown GradleException } + + def "check when ProjectData contains multiple licenses and CheckType is configured to require all licenses to match."() { + + allowedLicenseFile << """ + { + "allowedLicenses":[ + { + "moduleLicense": "License3", + "moduleName": "dummy-group:mod1" + } + ] + }""" + + projectDataFile << """ + { + "dependencies":[ + { + "moduleLicenses": [ + {"moduleLicense": "License1"}, + {"moduleLicense": "License2"}, + {"moduleLicense": "License3"} + ], + "moduleName": "dummy-group:mod1" + } + ] + }""" + + when: + def licenseChecker = new AllRequiredLicenseChecker() + licenseChecker.checkAllDependencyLicensesAreAllowed( + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + + then: + def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) + notPassedDependencies.moduleName == ["dummy-group:mod1", "dummy-group:mod1"] + notPassedDependencies.moduleLicense == ["License1", "License2"] + thrown GradleException + } + + def "check when ProjectData contains multiple licenses with one of them 'null' and CheckType is configured to require all licenses to match."() { + + allowedLicenseFile << """ + { + "allowedLicenses":[ + { + "moduleLicense": "License3", + "moduleName": "dummy-group:mod1" + } + ] + }""" + + projectDataFile << """ + { + "dependencies":[ + { + "moduleLicenses": [ + {"moduleLicense": null}, + {"moduleLicense": "License2"}, + {"moduleLicense": "License3"} + ], + "moduleName": "dummy-group:mod1" + } + ] + }""" + + when: + def licenseChecker = new AllRequiredLicenseChecker() + licenseChecker.checkAllDependencyLicensesAreAllowed( + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + + then: + def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) + notPassedDependencies.moduleName == ["dummy-group:mod1"] + notPassedDependencies.moduleLicense == ["License2"] + thrown GradleException + } + + def "check when ProjectData contains one license with 'null' and CheckType is configured to require all licenses to match."() { + + allowedLicenseFile << """ + { + "allowedLicenses":[ + { + "moduleLicense": "License3", + "moduleName": "dummy-group:mod1" + } + ] + }""" + + projectDataFile << """ + { + "dependencies":[ + { + "moduleLicenses": [ + {"moduleLicense": null} + ], + "moduleName": "dummy-group:mod1" + } + ] + }""" + + when: + def licenseChecker = new AllRequiredLicenseChecker() + licenseChecker.checkAllDependencyLicensesAreAllowed( + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + + then: + def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) + notPassedDependencies.moduleName == ["dummy-group:mod1"] + notPassedDependencies.moduleLicense == [null] + thrown GradleException + } + + + def "check when ProjectData contains multiple licenses and CheckType is configured to require any licenses to match."() { + + allowedLicenseFile << """ + { + "allowedLicenses":[ + { + "moduleLicense": "License3", + "moduleName": "dummy-group:mod1" + } + ] + }""" + + projectDataFile << """ + { + "dependencies":[ + { + "moduleLicenses": [ + {"moduleLicense": "License1"}, + {"moduleLicense": "License2"}, + {"moduleLicense": "License3"} + ], + "moduleName": "dummy-group:mod1" + } + ] + }""" + + when: + def licenseChecker = new OneRequiredLicenseChecker() + licenseChecker.checkAllDependencyLicensesAreAllowed( + allowedLicenseFile, projectDataFile, notPassedDependenciesFile) + + then: + def notPassedDependencies = importNotPassedDependencies(notPassedDependenciesFile) + notPassedDependencies.moduleName == [] + notPassedDependencies.moduleLicense == [] + } }