Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support files without extensions #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,53 @@ class LicenserPluginFunctionalTest extends Specification {
[gradleVersion, _, extraArgs] << testMatrix
}

@Unroll
def "support mapping without extension (gradle #gradleVersion)"() {
given:
def projectDir = temporaryFolder.newFolder()
def sourcesDir = new File(projectDir, "sources")
sourcesDir.mkdirs()
new File(projectDir, "settings.gradle") << ""
new File(projectDir, "header.txt") << "Copyright header"
def sourceFile = new File(sourcesDir, "Specialfile") << "TEST"
new File(projectDir, "build.gradle") << """
plugins {
id('org.cadixdev.licenser')
}

license {
lineEnding = '\\n'
header = project.file("header.txt")
newLine = false
style {
Specialfile = 'HASH'
}
tasks {
sources {
files.from("sources")
}
}
}
""".stripIndent()

when:
def runner = runner(projectDir, gradleVersion, extraArgs + "updateLicenses")
runner.debug = true
def result = runner.build()

then:
result.task(":updateLicenseCustomSources").outcome == TaskOutcome.SUCCESS
sourceFile.text == """\
#
# Copyright header
#
TEST
""".stripIndent()

where:
[gradleVersion, _, extraArgs] << testMatrix
}

@Unroll
def "license formatting configuration is configuration-cacheable (gradle #gradleVersion)"() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import java.util.regex.Pattern

enum HeaderStyle {
BLOCK_COMMENT(~/^\s*\/\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/*', ' *', ' */',
'java', 'groovy', 'scala', 'kt', 'kts', 'gradle', 'css', 'js'),
'java', 'groovy', 'scala', 'kt', 'kts', 'gradle', 'css', 'js', 'Jenkinsfile'),
JAVADOC(~/^\s*\/\*\*(?:[^*].*)?$/, ~/\*\/\s*(.*?)$/, null, '/**', ' *', ' */'),
HASH(~/^\s*#/, null, ~/^\s*#!/, '#', '#', '#', 'properties', 'yml', 'yaml', 'sh'),
HASH(~/^\s*#/, null, ~/^\s*#!/, '#', '#', '#',
'properties', 'yml', 'yaml', 'sh', 'Dockerfile', 'Vagrantfile'),
XML(~/^\s*<!--/, ~/-->\s*(.*?)$/, ~/^\s*<(?:\?xml .*\?|!DOCTYPE .*)>\s*$/, '<!--', ' ', '-->',
'xml', 'xsd', 'xsl', 'fxml', 'dtd', 'html', 'xhtml'),
DOUBLE_SLASH(~/^\s*\/\//, null, null, '//', '//', '//')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.regex.Pattern;
Expand All @@ -37,9 +38,15 @@ public final class HeaderHelper {
private HeaderHelper() {
}

public static String getExtension(String s) {
final int pos = s.lastIndexOf('.');
return pos >= 0 ? s.substring(pos + 1) : null;
public static String getExtension(String path) {
String filename = getFilename(path);
final int pos = filename.lastIndexOf('.');
return pos >= 0 ? filename.substring(pos + 1) : filename;
}

private static String getFilename(String path) {
final int pos = path.lastIndexOf(File.separatorChar);
return pos >= 0 ? path.substring(pos + 1) : path;
}

public static String stripIndent(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,51 @@ import org.cadixdev.gradle.licenser.header.HeaderStyle
import spock.lang.Specification

class HeaderHelperTest extends Specification {

def "getExtension returns the extension"() {
given:
def filename = "App.java"

when:
def result = HeaderHelper.getExtension(filename)

then:
result == "java"
}

def "getExtension returns the whole filename without dot"() {
given:
def filename = "Dockerfile"

when:
def result = HeaderHelper.getExtension(filename)

then:
result == "Dockerfile"
}

def "getExtension returns filename from path"() {
given:
def path = "some/sub/path/Jenkinsfile"

when:
def result = HeaderHelper.getExtension(path)

then:
result == "Jenkinsfile"
}

def "getExtension returns the whole filename even if the path contains a dot"() {
given:
def path = "some.sub/path.with.dots/Vagrantfile"

when:
def result = HeaderHelper.getExtension(path)

then:
result == "Vagrantfile"
}

def "contentStartsWithValidHeaderFormat returns false with empty input"() {
given:
def inputString = ""
Expand Down