Skip to content

Commit

Permalink
Fix in LicenseHeaderStep which treats address as copyright year (#844 f…
Browse files Browse the repository at this point in the history
…ixes #716)
  • Loading branch information
nedtwigg authored Apr 20, 2021
2 parents 5123dc0 + 47fb559 commit cb071bb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 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
* LicenseHeaderStep treats address as copyright year ([#716](https://github.com/diffplug/spotless/issues/716))

## [2.13.2] - 2021-04-12
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,22 @@ private String calculateYearBySearching(String content) {
Matcher yearMatcher = YYYY.matcher(content);
if (yearMatcher.find()) {
String firstYear = yearMatcher.group();
String secondYear;

String secondYear = null;
if (updateYearWithLatest) {
secondYear = firstYear.equals(yearToday) ? null : yearToday;
} else if (yearMatcher.find(yearMatcher.end() + 1)) {
secondYear = yearMatcher.group();
} else {
secondYear = null;
String contentWithSecondYear = content.substring(yearMatcher.end() + 1);
int endOfLine = contentWithSecondYear.indexOf('\n');
if (endOfLine != -1) {
contentWithSecondYear = contentWithSecondYear.substring(0, endOfLine);
}
Matcher secondYearMatcher = YYYY.matcher(contentWithSecondYear);
if (secondYearMatcher.find()) {
secondYear = secondYearMatcher.group();
}
}

if (secondYear == null) {
return firstYear;
} else {
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
* LicenseHeaderStep treats address as copyright year ([#716](https://github.com/diffplug/spotless/issues/716))

## [5.12.1] - 2021-04-13
### Fixed
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
* LicenseHeaderStep treats address as copyright year ([#716](https://github.com/diffplug/spotless/issues/716))

## [2.10.1] - 2021-04-13
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public void should_remove_header_when_empty() throws Throwable {
.test(getTestResource("license/HasLicense.test"), getTestResource("license/MissingLicense.test"));
}

private String licenceWithAddress() {
return "Copyright © $YEAR FooBar Inc. All Rights Reserved.\n" +
" *\n" +
" * Use of this software is covered by inscrutable legal protection and\n" +
" * complex automation. Violaters of undisclosed terms must expect\n" +
" * unforeseen consequences.\n" +
" *\n" +
" * FooBar, Inc.\n" +
" * 9 Food Truck\n" +
" * Perry Derry, TX 55656 USA";
}

private String header(String contents) throws IOException {
return "/*\n" +
" * " + contents + "\n" +
Expand Down Expand Up @@ -212,4 +224,20 @@ public void should_apply_license_containing_YEAR_token_in_range() throws Throwab
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()));
}

@Test
public void should_update_year_for_license_with_address() throws Throwable {
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(licenceWithAddress()), package_).withYearMode(YearMode.UPDATE_TO_TODAY).build();
StepHarness.forStep(step).test(
hasHeader(licenceWithAddress().replace("$YEAR", "2015")),
hasHeader(licenceWithAddress().replace("$YEAR", "2015-2021")));
}

@Test
public void should_preserve_year_for_license_with_address() throws Throwable {
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(licenceWithAddress()), package_).withYearMode(YearMode.PRESERVE).build();
StepHarness.forStep(step).test(
hasHeader(licenceWithAddress().replace("$YEAR", "2015").replace("FooBar Inc. All", "FooBar Inc. All")),
hasHeader(licenceWithAddress().replace("$YEAR", "2015")));
}
}

0 comments on commit cb071bb

Please sign in to comment.