Skip to content

Commit 959d6b7

Browse files
authored
Fix license header bug for years in range (#840)
2 parents 572e13e + 591b3fb commit 959d6b7

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

Diff for: 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+
* Fix license header bug for years in range ([#840](https://github.com/diffplug/spotless/pull/840)).
1315

1416
## [2.13.1] - 2021-04-10
1517
### Changed

Diff for: lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

+32-9
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private static class Runtime implements Serializable {
151151
private final @Nullable String beforeYear;
152152
private final @Nullable String afterYear;
153153
private final boolean updateYearWithLatest;
154+
private final boolean licenseHeaderWithRange;
154155

155156
/** The license that we'd like enforced. */
156157
private Runtime(String licenseHeader, String delimiter, String yearSeparator, boolean updateYearWithLatest) {
@@ -166,18 +167,28 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
166167

167168
Optional<String> yearToken = getYearToken(licenseHeader);
168169
if (yearToken.isPresent()) {
169-
yearToday = String.valueOf(YearMonth.now().getYear());
170+
this.yearToday = String.valueOf(YearMonth.now().getYear());
170171
int yearTokenIndex = licenseHeader.indexOf(yearToken.get());
171-
beforeYear = licenseHeader.substring(0, yearTokenIndex);
172-
afterYear = licenseHeader.substring(yearTokenIndex + yearToken.get().length());
173-
yearSepOrFull = yearSeparator;
172+
this.beforeYear = licenseHeader.substring(0, yearTokenIndex);
173+
this.afterYear = licenseHeader.substring(yearTokenIndex + yearToken.get().length());
174+
this.yearSepOrFull = yearSeparator;
174175
this.updateYearWithLatest = updateYearWithLatest;
176+
177+
boolean hasHeaderWithRange = false;
178+
int yearPlusSep = 4 + yearSeparator.length();
179+
if (beforeYear.endsWith(yearSeparator) && yearTokenIndex > yearPlusSep) {
180+
// year from in range
181+
String yearFrom = licenseHeader.substring(yearTokenIndex - yearPlusSep, yearTokenIndex).substring(0, 4);
182+
hasHeaderWithRange = YYYY.matcher(yearFrom).matches();
183+
}
184+
this.licenseHeaderWithRange = hasHeaderWithRange;
175185
} else {
176-
yearToday = null;
177-
beforeYear = null;
178-
afterYear = null;
186+
this.yearToday = null;
187+
this.beforeYear = null;
188+
this.afterYear = null;
179189
this.yearSepOrFull = licenseHeader;
180190
this.updateYearWithLatest = false;
191+
this.licenseHeaderWithRange = false;
181192
}
182193
}
183194

@@ -243,7 +254,11 @@ private String calculateYearExact(String parsedYear) {
243254
return parsedYear;
244255
} else if (YYYY.matcher(parsedYear).matches()) {
245256
if (updateYearWithLatest) {
246-
return parsedYear + yearSepOrFull + yearToday;
257+
if (licenseHeaderWithRange) {
258+
return yearToday;
259+
} else {
260+
return parsedYear + yearSepOrFull + yearToday;
261+
}
247262
} else {
248263
// it's already good as a single year
249264
return parsedYear;
@@ -266,7 +281,15 @@ private String calculateYearBySearching(String content) {
266281
} else {
267282
secondYear = null;
268283
}
269-
return secondYear == null ? firstYear : firstYear + yearSepOrFull + secondYear;
284+
if (secondYear == null) {
285+
return firstYear;
286+
} else {
287+
if (licenseHeaderWithRange) {
288+
return secondYear;
289+
} else {
290+
return firstYear + yearSepOrFull + secondYear;
291+
}
292+
}
270293
} else {
271294
System.err.println("Can't parse copyright year '" + content + "', defaulting to " + yearToday);
272295
// couldn't recognize the year format

Diff for: 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+
* Fix license header bug for years in range ([#840](https://github.com/diffplug/spotless/pull/840)).
68

79
## [5.12.0] - 2021-04-10
810
### Added

Diff for: 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+
* Fix license header bug for years in range ([#840](https://github.com/diffplug/spotless/pull/840)).
68

79
## [2.10.0] - 2021-04-10
810
### Added

Diff for: testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class LicenseHeaderStepTest extends ResourceHarness {
3232
private static final String FILE_NO_LICENSE = "license/FileWithoutLicenseHeader.test";
3333
private static final String package_ = "package ";
3434
private static final String HEADER_WITH_$YEAR = "This is a fake license, $YEAR. ACME corp.";
35+
private static final String HEADER_WITH_RANGE_TO_$YEAR = "This is a fake license with range, 2009-$YEAR. ACME corp.";
3536

3637
@Test
3738
public void parseExistingYear() throws Exception {
@@ -137,6 +138,10 @@ private String hasHeaderYear(String years) throws IOException {
137138
return hasHeaderYear(HEADER_WITH_$YEAR, years);
138139
}
139140

141+
private String hasHeaderWithRangeAndWithYearTo(String toYear) throws IOException {
142+
return hasHeaderYear(HEADER_WITH_RANGE_TO_$YEAR, toYear);
143+
}
144+
140145
private static String currentYear() {
141146
return String.valueOf(YearMonth.now().getYear());
142147
}
@@ -201,4 +206,10 @@ protected FormatterStep create() {
201206
}
202207
}.testEquals();
203208
}
209+
210+
@Test
211+
public void should_apply_license_containing_YEAR_token_in_range() throws Throwable {
212+
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_RANGE_TO_$YEAR), package_).withYearMode(YearMode.UPDATE_TO_TODAY).build();
213+
StepHarness.forStep(step).test(hasHeaderWithRangeAndWithYearTo("2015"), hasHeaderWithRangeAndWithYearTo(currentYear()));
214+
}
204215
}

0 commit comments

Comments
 (0)