Skip to content

Commit 281e12f

Browse files
authored
fix(FormatExtension/Gradle): Fix step setup with toggleFence and targetExcludeContentPattern (#2487)
2 parents b0eec1f + 0d62d75 commit 281e12f

File tree

7 files changed

+71
-2
lines changed

7 files changed

+71
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
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 `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487))
1315
### Changed
1416
* Bump default `eclipse` version to latest `4.34` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458))
1517
* Bump default `greclipse` version to latest `4.32` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458))

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
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 `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487))
68
### Changed
79
* Bump default `eclipse` version to latest `4.34` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458))
810
* Bump default `greclipse` version to latest `4.32` -> `4.35`. ([#2458](https://github.com/diffplug/spotless/pull/2458))

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,9 @@ protected void setupTask(SpotlessTask task) {
10611061
task.setTarget(totalTarget);
10621062
List<FormatterStep> steps;
10631063
if (toggleFence != null) {
1064-
steps = List.of(toggleFence.preserveWithin(this.steps));
1064+
// need a mutable List, 'steps' is mutated by 'steps.replaceAll()' below
1065+
steps = new ArrayList<>();
1066+
steps.add(toggleFence.preserveWithin(this.steps));
10651067
} else {
10661068
steps = this.steps;
10671069
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,4 +80,56 @@ void removeUnusedImportsWithCleanthat() throws IOException {
8080
gradleRunner().withArguments("spotlessApply").build();
8181
assertFile("src/main/java/test.java").sameAsResource("java/removeunusedimports/Jdk17TextBlockFormatted.test");
8282
}
83+
84+
/**
85+
* Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
86+
* {@code targetExcludeContentPattern} both being not {@code null}.
87+
*/
88+
@Test
89+
void fenceWithTargetExcludeNoMatch() throws Exception {
90+
setFile("build.gradle").toLines(
91+
"plugins {",
92+
" id 'com.diffplug.spotless'",
93+
" id 'java'",
94+
"}",
95+
"repositories { mavenCentral() }",
96+
"",
97+
"spotless {",
98+
" java {",
99+
" licenseHeader('// my-copyright')",
100+
" toggleOffOn()",
101+
" targetExcludeIfContentContains('excludeMe')",
102+
" }",
103+
"}");
104+
105+
setFile("src/main/java/test.java").toResource("java/targetExclude/TargetExcludeNoMatchUnformatted.test");
106+
gradleRunner().withArguments("spotlessApply").build();
107+
assertFile("src/main/java/test.java").sameAsResource("java/targetExclude/TargetExcludeNoMatchFormatted.test");
108+
}
109+
110+
/**
111+
* Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
112+
* {@code targetExcludeContentPattern} both being not {@code null}.
113+
*/
114+
@Test
115+
void fenceWithTargetExcludeMatch() throws Exception {
116+
setFile("build.gradle").toLines(
117+
"plugins {",
118+
" id 'com.diffplug.spotless'",
119+
" id 'java'",
120+
"}",
121+
"repositories { mavenCentral() }",
122+
"",
123+
"spotless {",
124+
" java {",
125+
" licenseHeader('// my-copyright')",
126+
" toggleOffOn()",
127+
" targetExcludeIfContentContains('excludeMe')",
128+
" }",
129+
"}");
130+
131+
setFile("src/main/java/test.java").toResource("java/targetExclude/TargetExcludeMatch.test");
132+
gradleRunner().withArguments("spotlessApply").build();
133+
assertFile("src/main/java/test.java").sameAsResource("java/targetExclude/TargetExcludeMatch.test");
134+
}
83135
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// excludeMe
2+
import foo.Bar;
3+
4+
class Bar {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// my-copyright
2+
import foo.Bar;
3+
4+
class Bar {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import foo.Bar;
2+
3+
class Bar {}

0 commit comments

Comments
 (0)