From 6f15a2bca7cbde4560b04bb829c9789857b51104 Mon Sep 17 00:00:00 2001 From: Technici4n <13494793+Technici4n@users.noreply.github.com> Date: Thu, 19 Oct 2023 14:42:26 +0200 Subject: [PATCH] Fix skipExistingHeaders on long files without headers yet --- .../LicenserPluginFunctionalTest.groovy | 53 +++++++++++++++++++ .../gradle/licenser/util/HeaderHelper.java | 5 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy b/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy index afb9943..751410b 100644 --- a/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy +++ b/src/functionalTest/groovy/org/cadixdev/gradle/licenser/LicenserPluginFunctionalTest.groovy @@ -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: diff --git a/src/main/groovy/org/cadixdev/gradle/licenser/util/HeaderHelper.java b/src/main/groovy/org/cadixdev/gradle/licenser/util/HeaderHelper.java index dbe4b01..4d3b44d 100644 --- a/src/main/groovy/org/cadixdev/gradle/licenser/util/HeaderHelper.java +++ b/src/main/groovy/org/cadixdev/gradle/licenser/util/HeaderHelper.java @@ -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;