Skip to content

Commit cb071bb

Browse files
authored
Fix in LicenseHeaderStep which treats address as copyright year (#844 fixes #716)
2 parents 5123dc0 + 47fb559 commit cb071bb

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* LicenseHeaderStep treats address as copyright year ([#716](https://github.com/diffplug/spotless/issues/716))
1315

1416
## [2.13.2] - 2021-04-12
1517
### Fixed

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,22 @@ private String calculateYearBySearching(String content) {
273273
Matcher yearMatcher = YYYY.matcher(content);
274274
if (yearMatcher.find()) {
275275
String firstYear = yearMatcher.group();
276-
String secondYear;
276+
277+
String secondYear = null;
277278
if (updateYearWithLatest) {
278279
secondYear = firstYear.equals(yearToday) ? null : yearToday;
279-
} else if (yearMatcher.find(yearMatcher.end() + 1)) {
280-
secondYear = yearMatcher.group();
281280
} else {
282-
secondYear = null;
281+
String contentWithSecondYear = content.substring(yearMatcher.end() + 1);
282+
int endOfLine = contentWithSecondYear.indexOf('\n');
283+
if (endOfLine != -1) {
284+
contentWithSecondYear = contentWithSecondYear.substring(0, endOfLine);
285+
}
286+
Matcher secondYearMatcher = YYYY.matcher(contentWithSecondYear);
287+
if (secondYearMatcher.find()) {
288+
secondYear = secondYearMatcher.group();
289+
}
283290
}
291+
284292
if (secondYear == null) {
285293
return firstYear;
286294
} else {

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* LicenseHeaderStep treats address as copyright year ([#716](https://github.com/diffplug/spotless/issues/716))
68

79
## [5.12.1] - 2021-04-13
810
### Fixed

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* LicenseHeaderStep treats address as copyright year ([#716](https://github.com/diffplug/spotless/issues/716))
68

79
## [2.10.1] - 2021-04-13
810
### Fixed

testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ public void should_remove_header_when_empty() throws Throwable {
120120
.test(getTestResource("license/HasLicense.test"), getTestResource("license/MissingLicense.test"));
121121
}
122122

123+
private String licenceWithAddress() {
124+
return "Copyright © $YEAR FooBar Inc. All Rights Reserved.\n" +
125+
" *\n" +
126+
" * Use of this software is covered by inscrutable legal protection and\n" +
127+
" * complex automation. Violaters of undisclosed terms must expect\n" +
128+
" * unforeseen consequences.\n" +
129+
" *\n" +
130+
" * FooBar, Inc.\n" +
131+
" * 9 Food Truck\n" +
132+
" * Perry Derry, TX 55656 USA";
133+
}
134+
123135
private String header(String contents) throws IOException {
124136
return "/*\n" +
125137
" * " + contents + "\n" +
@@ -212,4 +224,20 @@ public void should_apply_license_containing_YEAR_token_in_range() throws Throwab
212224
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_RANGE_TO_$YEAR), package_).withYearMode(YearMode.UPDATE_TO_TODAY).build();
213225
StepHarness.forStep(step).test(hasHeaderWithRangeAndWithYearTo("2015"), hasHeaderWithRangeAndWithYearTo(currentYear()));
214226
}
227+
228+
@Test
229+
public void should_update_year_for_license_with_address() throws Throwable {
230+
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(licenceWithAddress()), package_).withYearMode(YearMode.UPDATE_TO_TODAY).build();
231+
StepHarness.forStep(step).test(
232+
hasHeader(licenceWithAddress().replace("$YEAR", "2015")),
233+
hasHeader(licenceWithAddress().replace("$YEAR", "2015-2021")));
234+
}
235+
236+
@Test
237+
public void should_preserve_year_for_license_with_address() throws Throwable {
238+
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(licenceWithAddress()), package_).withYearMode(YearMode.PRESERVE).build();
239+
StepHarness.forStep(step).test(
240+
hasHeader(licenceWithAddress().replace("$YEAR", "2015").replace("FooBar Inc. All", "FooBar Inc. All")),
241+
hasHeader(licenceWithAddress().replace("$YEAR", "2015")));
242+
}
215243
}

0 commit comments

Comments
 (0)