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

Fix skipExistingHeaders on long files without headers yet #41

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 @@ -167,6 +167,59 @@ class LicenserPluginFunctionalTest extends Specification {
[gradleVersion, _, extraArgs] << testMatrix
}

@Unroll
def "supports long files with skipExistingHeaders = true in updateLicenses task (gradle #gradleVersion)"() {
given:
def projectDir = temporaryFolder.newFolder()
def sourceDir = projectDir.toPath().resolve(Paths.get("src", "main", "java", "com", "example")).toFile()
sourceDir.mkdirs()
new File(projectDir, "header.txt") << "New copyright header"
new File(projectDir, "settings.gradle") << ""
new File(projectDir, "build.gradle") << """
plugins {
id('java')
id('org.cadixdev.licenser')
}

license {
lineEnding = '\\n'
header = project.file('header.txt')
skipExistingHeaders = true
}
""".stripIndent()
def sourceFileBuilder = new StringBuilder("""\
package com.example;

class MyClass {
""".stripIndent())
// Add a lot of fields to make the file long
for (int i = 0; i < 1000; i++) {
sourceFileBuilder += " private int field"
sourceFileBuilder += i
sourceFileBuilder += ";\n"
}
sourceFileBuilder += "}\n"
def sourceFileContent = sourceFileBuilder.toString()
def sourceFile = new File(sourceDir, "MyClass.java") << sourceFileContent

def expectedResult = """\
/*
* New copyright header
*/

""".stripIndent() + sourceFileContent

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

then:
result.task(":updateLicenses").outcome == TaskOutcome.SUCCESS
sourceFile.text == expectedResult

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

@Unroll
def "updates invalid headers in updateLicenses task when skipExistingHeaders=true (gradle #gradleVersion)"() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ public static boolean contentStartsWithValidHeaderFormat(BufferedReader reader,
break;
}
// If the current line doesn't match and there's no end marker, assume the header is complete
contentLinesMatch = contentLinesMatch && (line.startsWith(format.getPrefix()) || format.getEnd() == null);
if (!line.startsWith(format.getPrefix()) && format.getEnd() != null) {
contentLinesMatch = false;
break;
}
}

return firstLineMatches && contentLinesMatch && lastLineMatches;
Expand Down