From 1913e1c0b549bd382a82f7a42647be87cdc092f3 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 4 Mar 2023 17:06:30 +0900 Subject: [PATCH 1/9] Support a file name field in license headers --- .../spotless/generic/LicenseHeaderStep.java | 24 ++++++++++----- .../com/diffplug/spotless/StepHarness.java | 14 +++++++-- .../generic/LicenseHeaderStepTest.java | 30 +++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index b4c0cf2a6f..624ea16cc6 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -142,7 +142,7 @@ public FormatterStep build() { throw new IllegalStateException(yearMode.toString()); } return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear, skipLinesMatching); - }, step -> step::format); + }, step -> FormatterFunc.needsFile(step::format)); } if (contentPattern == null) { @@ -214,6 +214,8 @@ private static class Runtime implements Serializable { private final boolean updateYearWithLatest; private final boolean licenseHeaderWithRange; + private static final Pattern FILENAME_PATTERN = Pattern.compile("\\$FILE"); + /** The license that we'd like enforced. */ private Runtime(String licenseHeader, String delimiter, String yearSeparator, boolean updateYearWithLatest, @Nullable String skipLinesMatching) { if (delimiter.contains("\n")) { @@ -266,9 +268,9 @@ private static Optional getYearToken(String licenseHeader) { } /** Formats the given string. */ - private String format(String raw) { + private String format(String raw, File file) { if (skipLinesMatching == null) { - return addOrUpdateLicenseHeader(raw); + return addOrUpdateLicenseHeader(raw, file); } else { String[] lines = raw.split("\n"); StringBuilder skippedLinesBuilder = new StringBuilder(); @@ -287,11 +289,11 @@ private String format(String raw) { remainingLinesBuilder.append(line).append('\n'); } } - return skippedLinesBuilder + addOrUpdateLicenseHeader(remainingLinesBuilder.toString()); + return skippedLinesBuilder + addOrUpdateLicenseHeader(remainingLinesBuilder.toString(), file); } } - private String addOrUpdateLicenseHeader(String raw) { + private String addOrUpdateLicenseHeader(String raw, File file) { Matcher contentMatcher = delimiterPattern.matcher(raw); if (!contentMatcher.find()) { throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern); @@ -305,13 +307,14 @@ private String addOrUpdateLicenseHeader(String raw) { return raw; } else { // otherwise we'll have to add the header - return yearSepOrFull + content; + return replaceFileName(yearSepOrFull, file) + content; } } else { // the yes year case is a bit harder int beforeYearIdx = raw.indexOf(beforeYear); int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1); + String header; if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) { // and also ends with exactly the right header, so it's easy to parse the existing year String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx); @@ -323,12 +326,13 @@ private String addOrUpdateLicenseHeader(String raw) { return raw; } } - return beforeYear + newYear + afterYear + content; + header = beforeYear + newYear + afterYear; } else { String newYear = calculateYearBySearching(raw.substring(0, contentMatcher.start())); // at worst, we just say that it was made today - return beforeYear + newYear + afterYear + content; + header = beforeYear + newYear + afterYear; } + return replaceFileName(header, file) + content; } } } @@ -421,6 +425,10 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws return beforeYear + yearRange + afterYear + raw.substring(contentMatcher.start()); } + private String replaceFileName(String header, File file) { + return FILENAME_PATTERN.matcher(header).replaceAll(file.getName()); + } + private static String parseYear(String cmd, File file) throws IOException { String fullCmd = cmd + " -- " + file.getAbsolutePath(); ProcessBuilder builder = new ProcessBuilder().directory(file.getParentFile()); diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index c611cc738a..032345827d 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -57,14 +57,22 @@ public static StepHarness forFormatter(Formatter formatter) { /** Asserts that the given element is transformed as expected, and that the result is idempotent. */ public StepHarness test(String before, String after) { - String actual = formatter.compute(LineEnding.toUnix(before), new File("")); + return test(before, after, ""); + } + + public StepHarness test(String before, String after, String fileName) { + String actual = formatter.compute(LineEnding.toUnix(before), new File(fileName)); assertEquals(after, actual, "Step application failed"); - return testUnaffected(after); + return testUnaffected(after, fileName); } /** Asserts that the given element is idempotent w.r.t the step under test. */ public StepHarness testUnaffected(String idempotentElement) { - String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File("")); + return testUnaffected(idempotentElement, ""); + } + + public StepHarness testUnaffected(String idempotentElement, String fileName) { + String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File(fileName)); assertEquals(idempotentElement, actual, "Step is not idempotent"); return this; } diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 2b1d0aff07..d8d7700be5 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -35,6 +35,8 @@ class LicenseHeaderStepTest extends ResourceHarness { private static final String package_ = "package "; private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp."; private static final String HEADER_WITH_RANGE_TO_$YEAR = "This is a fake license with range, 2009-$YEAR. ACME corp."; + private static final String HEADER_WITH_$FILE = "This is a fake license, $FILE. ACME corp."; + private static final String HEADER_WITH_$YEAR_$FILE = "This is a fake license, $FILE, $YEAR. ACME corp."; @Test void parseExistingYear() throws Exception { @@ -163,6 +165,16 @@ private String hasHeaderWithRangeAndWithYearTo(String toYear) throws IOException return hasHeaderYear(HEADER_WITH_RANGE_TO_$YEAR, toYear); } + private String hasHeaderFileName(String license, String fileName) throws IOException { + return header(license).replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); + } + + private String hasHeaderYearFileName(String license, String year, String fileName) throws IOException { + return header(license) + .replace("$YEAR", year) + .replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); + } + private static String currentYear() { return String.valueOf(YearMonth.now().getYear()); } @@ -250,4 +262,22 @@ void should_preserve_year_for_license_with_address() throws Throwable { hasHeader(licenceWithAddress().replace("$YEAR", "2015").replace("FooBar Inc. All", "FooBar Inc. All")), hasHeader(licenceWithAddress().replace("$YEAR", "2015"))); } + + @Test + void should_apply_license_containing_filename_token() throws Exception { + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); + StepHarness.forStep(step) + .test(getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java"), "Test.java"); + } + + @Test + void should_apply_license_containing_YEAR_filename_token() throws Exception { + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); + StepHarness.forStep(step) + .test( + getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java"), + "Test.java" + ); + } } From bdc857e8c1b55c87a138976742da702ef53ff373 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 4 Mar 2023 17:31:22 +0900 Subject: [PATCH 2/9] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 8540a44851..f5033f6d2a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes * We are now opting in to Gradle's new stable configuration cache. ([#1591](https://github.com/diffplug/spotless/pull/1591)) +* Support a file name field in license headers. ([#1605](https://github.com/diffplug/spotless/pull/1605)) ## [2.36.0] - 2023-02-27 ### Added From d8b0e20ef0a22d0aa03e668a4917f3f1ba4dc725 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Wed, 8 Mar 2023 09:11:51 +0900 Subject: [PATCH 3/9] Use StepHarnessWithFile --- .../java/com/diffplug/spotless/StepHarness.java | 14 +++----------- .../spotless/generic/LicenseHeaderStepTest.java | 12 +++++++----- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index 032345827d..c611cc738a 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -57,22 +57,14 @@ public static StepHarness forFormatter(Formatter formatter) { /** Asserts that the given element is transformed as expected, and that the result is idempotent. */ public StepHarness test(String before, String after) { - return test(before, after, ""); - } - - public StepHarness test(String before, String after, String fileName) { - String actual = formatter.compute(LineEnding.toUnix(before), new File(fileName)); + String actual = formatter.compute(LineEnding.toUnix(before), new File("")); assertEquals(after, actual, "Step application failed"); - return testUnaffected(after, fileName); + return testUnaffected(after); } /** Asserts that the given element is idempotent w.r.t the step under test. */ public StepHarness testUnaffected(String idempotentElement) { - return testUnaffected(idempotentElement, ""); - } - - public StepHarness testUnaffected(String idempotentElement, String fileName) { - String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File(fileName)); + String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File("")); assertEquals(idempotentElement, actual, "Step is not idempotent"); return this; } diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index d8d7700be5..6df0c23950 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -21,6 +21,8 @@ import java.time.YearMonth; import java.time.ZoneOffset; +import com.diffplug.spotless.StepHarnessWithFile; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -266,18 +268,18 @@ void should_preserve_year_for_license_with_address() throws Throwable { @Test void should_apply_license_containing_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); - StepHarness.forStep(step) - .test(getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java"), "Test.java"); + StepHarnessWithFile.forStep(this, step) + .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); } @Test void should_apply_license_containing_YEAR_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); - StepHarness.forStep(step) + StepHarnessWithFile.forStep(this, step) .test( + new File("Test.java"), getTestResource(FILE_NO_LICENSE), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java"), - "Test.java" + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java") ); } } From a414e937e2c89e56ea038096c8f0ac1ee6fb1eff Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Wed, 8 Mar 2023 09:16:38 +0900 Subject: [PATCH 4/9] Fix CHANGES.md --- CHANGES.md | 3 ++- plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f5033f6d2a..c44dfd9050 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,9 +10,10 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ### Changes * We are now opting in to Gradle's new stable configuration cache. ([#1591](https://github.com/diffplug/spotless/pull/1591)) -* Support a file name field in license headers. ([#1605](https://github.com/diffplug/spotless/pull/1605)) ## [2.36.0] - 2023-02-27 ### Added diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index c19a7de733..3ecf10d071 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ## [6.16.0] - 2023-02-27 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 36a51d6ef2..5c50b62027 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* You can now put the filename into a license header template with `$FILE`. ([#1605](https://github.com/diffplug/spotless/pull/1605) fixes [#1147](https://github.com/diffplug/spotless/issues/1147)) ## [2.34.0] - 2023-02-27 ### Added From bc34a4b8e0b2fe5063df528411163d19e2ee8d85 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Wed, 8 Mar 2023 09:19:04 +0900 Subject: [PATCH 5/9] Run spotlessApply --- .../generic/LicenseHeaderStepTest.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 6df0c23950..7d2e018028 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -21,8 +21,6 @@ import java.time.YearMonth; import java.time.ZoneOffset; -import com.diffplug.spotless.StepHarnessWithFile; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -30,6 +28,7 @@ import com.diffplug.spotless.ResourceHarness; import com.diffplug.spotless.SerializableEqualityTester; import com.diffplug.spotless.StepHarness; +import com.diffplug.spotless.StepHarnessWithFile; import com.diffplug.spotless.generic.LicenseHeaderStep.YearMode; class LicenseHeaderStepTest extends ResourceHarness { @@ -173,8 +172,8 @@ private String hasHeaderFileName(String license, String fileName) throws IOExcep private String hasHeaderYearFileName(String license, String year, String fileName) throws IOException { return header(license) - .replace("$YEAR", year) - .replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); + .replace("$YEAR", year) + .replace("$FILE", fileName) + getTestResource(FILE_NO_LICENSE); } private static String currentYear() { @@ -269,17 +268,16 @@ void should_preserve_year_for_license_with_address() throws Throwable { void should_apply_license_containing_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); + .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); } @Test void should_apply_license_containing_YEAR_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test( - new File("Test.java"), - getTestResource(FILE_NO_LICENSE), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java") - ); + .test( + new File("Test.java"), + getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); } } From 318f453ee003aaf16b787ab5e947f44deb95fafb Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Thu, 9 Mar 2023 11:59:13 +0900 Subject: [PATCH 6/9] Fix file name not applied --- .../java/com/diffplug/spotless/generic/LicenseHeaderStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index 624ea16cc6..ccc665bb0d 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -323,7 +323,7 @@ private String addOrUpdateLicenseHeader(String raw, File file) { // fastpath where we don't need to make any changes at all boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw if (noPadding) { - return raw; + return replaceFileName(raw.substring(0, contentMatcher.start()), file) + content; } } header = beforeYear + newYear + afterYear; From d4d3814b770219681aa985cb5cc50b76cdfa17f8 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 11 Mar 2023 11:51:05 +0900 Subject: [PATCH 7/9] Refactor LicenseHeaderStep --- .../spotless/generic/LicenseHeaderStep.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java index ccc665bb0d..0b7d076e12 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java @@ -213,6 +213,7 @@ private static class Runtime implements Serializable { private final @Nullable String afterYear; private final boolean updateYearWithLatest; private final boolean licenseHeaderWithRange; + private final boolean hasFileToken; private static final Pattern FILENAME_PATTERN = Pattern.compile("\\$FILE"); @@ -228,6 +229,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo } this.delimiterPattern = Pattern.compile('^' + delimiter, Pattern.UNIX_LINES | Pattern.MULTILINE); this.skipLinesMatching = skipLinesMatching == null ? null : Pattern.compile(skipLinesMatching); + this.hasFileToken = FILENAME_PATTERN.matcher(licenseHeader).find(); Optional yearToken = getYearToken(licenseHeader); if (yearToken.isPresent()) { @@ -294,6 +296,12 @@ private String format(String raw, File file) { } private String addOrUpdateLicenseHeader(String raw, File file) { + raw = replaceYear(raw); + raw = replaceFileName(raw, file); + return raw; + } + + private String replaceYear(String raw) { Matcher contentMatcher = delimiterPattern.matcher(raw); if (!contentMatcher.find()) { throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern); @@ -307,14 +315,13 @@ private String addOrUpdateLicenseHeader(String raw, File file) { return raw; } else { // otherwise we'll have to add the header - return replaceFileName(yearSepOrFull, file) + content; + return yearSepOrFull + content; } } else { // the yes year case is a bit harder int beforeYearIdx = raw.indexOf(beforeYear); int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1); - String header; if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) { // and also ends with exactly the right header, so it's easy to parse the existing year String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx); @@ -323,16 +330,15 @@ private String addOrUpdateLicenseHeader(String raw, File file) { // fastpath where we don't need to make any changes at all boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw if (noPadding) { - return replaceFileName(raw.substring(0, contentMatcher.start()), file) + content; + return raw; } } - header = beforeYear + newYear + afterYear; + return beforeYear + newYear + afterYear + content; } else { String newYear = calculateYearBySearching(raw.substring(0, contentMatcher.start())); // at worst, we just say that it was made today - header = beforeYear + newYear + afterYear; + return beforeYear + newYear + afterYear + content; } - return replaceFileName(header, file) + content; } } } @@ -425,8 +431,17 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws return beforeYear + yearRange + afterYear + raw.substring(contentMatcher.start()); } - private String replaceFileName(String header, File file) { - return FILENAME_PATTERN.matcher(header).replaceAll(file.getName()); + private String replaceFileName(String raw, File file) { + if (!hasFileToken) { + return raw; + } + Matcher contentMatcher = delimiterPattern.matcher(raw); + if (!contentMatcher.find()) { + throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern); + } + String header = raw.substring(0, contentMatcher.start()); + String content = raw.substring(contentMatcher.start()); + return FILENAME_PATTERN.matcher(header).replaceAll(file.getName()) + content; } private static String parseYear(String cmd, File file) throws IOException { From 91977a1a71a0a81a1be2324d30eb0746150b2027 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 11 Mar 2023 11:55:45 +0900 Subject: [PATCH 8/9] Add tests --- .../spotless/generic/LicenseHeaderStepTest.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index 7d2e018028..760725229f 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -268,7 +268,18 @@ void should_preserve_year_for_license_with_address() throws Throwable { void should_apply_license_containing_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); + .test(new File("Test.java"), getTestResource(FILE_NO_LICENSE), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")) + .testUnaffected(new File("Test.java"), hasHeaderFileName(HEADER_WITH_$FILE, "Test.java")); + } + + @Test + void should_update_license_containing_filename_token() throws Exception { + FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$FILE), package_).build(); + StepHarnessWithFile.forStep(this, step) + .test( + new File("After.java"), + hasHeaderFileName(HEADER_WITH_$FILE, "Before.java"), + hasHeaderFileName(HEADER_WITH_$FILE, "After.java")); } @Test @@ -278,6 +289,9 @@ void should_apply_license_containing_YEAR_filename_token() throws Exception { .test( new File("Test.java"), getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")) + .testUnaffected( + new File("Test.java"), hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); } } From 40488be7041de75d8bbfedeaf5cff20058202145 Mon Sep 17 00:00:00 2001 From: Kenji Abe Date: Sat, 11 Mar 2023 12:02:45 +0900 Subject: [PATCH 9/9] Run spotlessApply --- .../spotless/generic/LicenseHeaderStepTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java index ba65cdbafc..92c6794643 100644 --- a/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java @@ -286,13 +286,13 @@ void should_update_license_containing_filename_token() throws Exception { void should_apply_license_containing_YEAR_filename_token() throws Exception { FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR_$FILE), package_).build(); StepHarnessWithFile.forStep(this, step) - .test( - new File("Test.java"), - getTestResource(FILE_NO_LICENSE), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")) - .testUnaffected( - new File("Test.java"), - hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); + .test( + new File("Test.java"), + getTestResource(FILE_NO_LICENSE), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")) + .testUnaffected( + new File("Test.java"), + hasHeaderYearFileName(HEADER_WITH_$YEAR_$FILE, currentYear(), "Test.java")); } void noPackage() throws Throwable {