Skip to content

Commit

Permalink
Fix license header bug for years in range (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Apr 12, 2021
2 parents 572e13e + 591b3fb commit 959d6b7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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]
### Fixed
* Fix license header bug for years in range ([#840](https://github.com/diffplug/spotless/pull/840)).

## [2.13.1] - 2021-04-10
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private static class Runtime implements Serializable {
private final @Nullable String beforeYear;
private final @Nullable String afterYear;
private final boolean updateYearWithLatest;
private final boolean licenseHeaderWithRange;

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

Optional<String> yearToken = getYearToken(licenseHeader);
if (yearToken.isPresent()) {
yearToday = String.valueOf(YearMonth.now().getYear());
this.yearToday = String.valueOf(YearMonth.now().getYear());
int yearTokenIndex = licenseHeader.indexOf(yearToken.get());
beforeYear = licenseHeader.substring(0, yearTokenIndex);
afterYear = licenseHeader.substring(yearTokenIndex + yearToken.get().length());
yearSepOrFull = yearSeparator;
this.beforeYear = licenseHeader.substring(0, yearTokenIndex);
this.afterYear = licenseHeader.substring(yearTokenIndex + yearToken.get().length());
this.yearSepOrFull = yearSeparator;
this.updateYearWithLatest = updateYearWithLatest;

boolean hasHeaderWithRange = false;
int yearPlusSep = 4 + yearSeparator.length();
if (beforeYear.endsWith(yearSeparator) && yearTokenIndex > yearPlusSep) {
// year from in range
String yearFrom = licenseHeader.substring(yearTokenIndex - yearPlusSep, yearTokenIndex).substring(0, 4);
hasHeaderWithRange = YYYY.matcher(yearFrom).matches();
}
this.licenseHeaderWithRange = hasHeaderWithRange;
} else {
yearToday = null;
beforeYear = null;
afterYear = null;
this.yearToday = null;
this.beforeYear = null;
this.afterYear = null;
this.yearSepOrFull = licenseHeader;
this.updateYearWithLatest = false;
this.licenseHeaderWithRange = false;
}
}

Expand Down Expand Up @@ -243,7 +254,11 @@ private String calculateYearExact(String parsedYear) {
return parsedYear;
} else if (YYYY.matcher(parsedYear).matches()) {
if (updateYearWithLatest) {
return parsedYear + yearSepOrFull + yearToday;
if (licenseHeaderWithRange) {
return yearToday;
} else {
return parsedYear + yearSepOrFull + yearToday;
}
} else {
// it's already good as a single year
return parsedYear;
Expand All @@ -266,7 +281,15 @@ private String calculateYearBySearching(String content) {
} else {
secondYear = null;
}
return secondYear == null ? firstYear : firstYear + yearSepOrFull + secondYear;
if (secondYear == null) {
return firstYear;
} else {
if (licenseHeaderWithRange) {
return secondYear;
} else {
return firstYear + yearSepOrFull + secondYear;
}
}
} else {
System.err.println("Can't parse copyright year '" + content + "', defaulting to " + yearToday);
// couldn't recognize the year format
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Fixed
* Fix license header bug for years in range ([#840](https://github.com/diffplug/spotless/pull/840)).

## [5.12.0] - 2021-04-10
### Added
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Fix license header bug for years in range ([#840](https://github.com/diffplug/spotless/pull/840)).

## [2.10.0] - 2021-04-10
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class LicenseHeaderStepTest extends ResourceHarness {
private static final String FILE_NO_LICENSE = "license/FileWithoutLicenseHeader.test";
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.";

@Test
public void parseExistingYear() throws Exception {
Expand Down Expand Up @@ -137,6 +138,10 @@ private String hasHeaderYear(String years) throws IOException {
return hasHeaderYear(HEADER_WITH_$YEAR, years);
}

private String hasHeaderWithRangeAndWithYearTo(String toYear) throws IOException {
return hasHeaderYear(HEADER_WITH_RANGE_TO_$YEAR, toYear);
}

private static String currentYear() {
return String.valueOf(YearMonth.now().getYear());
}
Expand Down Expand Up @@ -201,4 +206,10 @@ protected FormatterStep create() {
}
}.testEquals();
}

@Test
public void should_apply_license_containing_YEAR_token_in_range() throws Throwable {
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_RANGE_TO_$YEAR), package_).withYearMode(YearMode.UPDATE_TO_TODAY).build();
StepHarness.forStep(step).test(hasHeaderWithRangeAndWithYearTo("2015"), hasHeaderWithRangeAndWithYearTo(currentYear()));
}
}

0 comments on commit 959d6b7

Please sign in to comment.