Skip to content

Commit

Permalink
Merge pull request #1 from neoforged/fix-long-files-skip-existing
Browse files Browse the repository at this point in the history
Fix skipExistingHeaders on long files without headers yet
  • Loading branch information
marchermans authored Oct 20, 2023
2 parents cb4b220 + 6f15a2b commit 016608c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
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

0 comments on commit 016608c

Please sign in to comment.