From 03a46fa247b4756d9fbb7052d65b85061a85ff3e Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sat, 10 Jul 2021 16:43:55 +0200 Subject: [PATCH] Support double slash within DOUBLE_SLASH style Closes #26 --- .../LicenserPluginFunctionalTest.groovy | 46 ++++++++++++ .../header/CommentHeaderFormat.groovy | 13 ++-- .../gradle/licenser/header/HeaderStyle.groovy | 6 +- .../licenser/header/HeaderStyleTest.groovy | 70 +++++++++++++++++++ 4 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 src/test/groovy/org/cadixdev/gradle/licenser/header/HeaderStyleTest.groovy diff --git a/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy b/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy index afb9943..646d347 100644 --- a/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy +++ b/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy @@ -446,4 +446,50 @@ class LicenserPluginFunctionalTest extends Specification { [gradleVersion, _, extraArgs] << testMatrix } + + @Unroll + def "supports URL in header (gradle #gradleVersion)"() { + given: + def projectDir = temporaryFolder.newFolder() + def sourcesDir = projectDir.toPath().resolve(Paths.get("src", "main", "java")).toFile() + sourcesDir.mkdirs() + new File(projectDir, "settings.gradle") << "" + new File(projectDir, "header.txt") << """ + Copyright header + http://example.com/path?query=value#fragment + """.stripIndent().trim() + def sourceFile = new File(sourcesDir, "MyClass.java") << "public class MyClass {}" + new File(projectDir, "build.gradle") << """ + plugins { + id('java') + id('org.cadixdev.licenser') + } + + license { + header = project.file("header.txt") + style { + java = 'DOUBLE_SLASH' + } + } + """.stripIndent() + + when: + def runner = runner(projectDir, gradleVersion, extraArgs + "updateLicenses") + runner.debug = true + def result = runner.build() + + then: + result.task(":updateLicenses").outcome == TaskOutcome.SUCCESS + sourceFile.text == """\ + // + // Copyright header + // http://example.com/path?query=value#fragment + // + + public class MyClass {} + """.stripIndent() + + where: + [gradleVersion, _, extraArgs] << testMatrix + } } diff --git a/src/main/groovy/org/cadixdev/gradle/licenser/header/CommentHeaderFormat.groovy b/src/main/groovy/org/cadixdev/gradle/licenser/header/CommentHeaderFormat.groovy index 72b2fc8..c4dddd0 100644 --- a/src/main/groovy/org/cadixdev/gradle/licenser/header/CommentHeaderFormat.groovy +++ b/src/main/groovy/org/cadixdev/gradle/licenser/header/CommentHeaderFormat.groovy @@ -24,12 +24,14 @@ package org.cadixdev.gradle.licenser.header +import groovy.transform.CompileStatic import groovy.transform.PackageScope import org.cadixdev.gradle.licenser.util.HeaderHelper import javax.annotation.Nullable import java.util.regex.Pattern +@CompileStatic class CommentHeaderFormat implements HeaderFormat { final String name @@ -40,13 +42,15 @@ class CommentHeaderFormat implements HeaderFormat { @Nullable final Pattern skipLine + @Nullable final String firstLine final String prefix + @Nullable final String lastLine @PackageScope CommentHeaderFormat(String name, Pattern start, @Nullable Pattern end, @Nullable Pattern skipLine, - String firstLine, String prefix, String lastLine) { + @Nullable String firstLine, String prefix, @Nullable String lastLine) { this.name = name this.start = start this.end = end @@ -60,18 +64,19 @@ class CommentHeaderFormat implements HeaderFormat { ensureAbsent(text, firstLine) ensureAbsent(text, lastLine) - List result = [firstLine] + List result = firstLine == null ? [prefix]: [firstLine] text.eachLine { result << HeaderHelper.stripTrailingIndent("$prefix $it") } - result << lastLine + result << (lastLine == null ? prefix : lastLine) + return result } private static void ensureAbsent(String s, String search) { - if (s.contains(search)) { + if (search != null && s.contains(search)) { throw new IllegalArgumentException("Header contains unsupported characters $search") } } diff --git a/src/main/groovy/org/cadixdev/gradle/licenser/header/HeaderStyle.groovy b/src/main/groovy/org/cadixdev/gradle/licenser/header/HeaderStyle.groovy index 7e1bfa2..e06dad5 100644 --- a/src/main/groovy/org/cadixdev/gradle/licenser/header/HeaderStyle.groovy +++ b/src/main/groovy/org/cadixdev/gradle/licenser/header/HeaderStyle.groovy @@ -31,16 +31,16 @@ enum HeaderStyle { BLOCK_COMMENT(~/^\s*\/\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/*', ' *', ' */', 'java', 'groovy', 'scala', 'kt', 'kts', 'gradle', 'css', 'js'), JAVADOC(~/^\s*\/\*\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/**', ' *', ' */'), - HASH(~/^\s*#/, null, ~/^\s*#!/, '#', '#', '#', 'properties', 'yml', 'yaml', 'sh'), + HASH(~/^\s*#/, null, ~/^\s*#!/, null, '#', null, 'properties', 'yml', 'yaml', 'sh'), XML(~/^\s*\s*(.*?)$/, ~/^\s*<(?:\?xml .*\?|!DOCTYPE .*)>\s*$/, '', 'xml', 'xsd', 'xsl', 'fxml', 'dtd', 'html', 'xhtml'), - DOUBLE_SLASH(~/^\s*\/\//, null, null, '//', '//', '//') + DOUBLE_SLASH(~/^\s*\/\//, null, null, null, '//', null) final CommentHeaderFormat format private final String[] extensions HeaderStyle(Pattern start, @Nullable Pattern end, @Nullable Pattern skipLine, - String firstLine, String prefix, String lastLine, String... extensions) { + @Nullable String firstLine, String prefix, @Nullable String lastLine, String... extensions) { this.format = new CommentHeaderFormat(this.name(), start, end, skipLine, firstLine, prefix, lastLine) this.extensions = extensions } diff --git a/src/test/groovy/org/cadixdev/gradle/licenser/header/HeaderStyleTest.groovy b/src/test/groovy/org/cadixdev/gradle/licenser/header/HeaderStyleTest.groovy new file mode 100644 index 0000000..79e3fd0 --- /dev/null +++ b/src/test/groovy/org/cadixdev/gradle/licenser/header/HeaderStyleTest.groovy @@ -0,0 +1,70 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015, Minecrell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.cadixdev.gradle.licenser.header + +import spock.lang.Specification +import spock.lang.Unroll + +class HeaderStyleTest extends Specification { + @Unroll + def "test header style #style"() { + when: + def lines = style.format.format("Test") + + then: + lines == expectedLines + + where: + [style, expectedLines] << [ + [HeaderStyle.BLOCK_COMMENT, ["/*", " * Test", " */"]], + [HeaderStyle.JAVADOC, ["/**", " * Test", " */"]], + [HeaderStyle.DOUBLE_SLASH, ["//", "// Test", "//"]], + [HeaderStyle.HASH, ["#", "# Test", "#"]], + [HeaderStyle.XML, [""]] + ] + } + + def "DOUBLE_SLASH may contain slashes"() { + given: + HeaderStyle style = HeaderStyle.DOUBLE_SLASH + + when: + def lines = style.format.format("// Test //") + + then: + lines == ["//", "// // Test //", "//"] + } + + def "BLOCK_COMMENT may not contain end of comment"() { + given: + HeaderStyle style = HeaderStyle.BLOCK_COMMENT + + when: + style.format.format("/* Test */") + + then: + thrown(IllegalArgumentException) + } +}