Skip to content

Commit 23d52bf

Browse files
committed
fix: Ensure TableTestFormatterFunc uses fresh EditorConfigProvider
The issue is that when editoconfig changes, it was not captured by the formatter function, because the `EditorConfigProvider` was static. And currently `EditorConfigProvider` uses internally an ec4j "permanent" cache: ``` ResourcePropertiesService.builder().cache(Caches.permanent()).build(); ```
1 parent 708a1b0 commit 23d52bf

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

lib/src/tableTestFormatter/java/com/diffplug/spotless/glue/java/TableTestFormatterFunc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
public class TableTestFormatterFunc implements FormatterFunc.NeedsFile {
3131

32-
private static final EditorConfigProvider CONFIG_PROVIDER = new EditorConfigProvider();
32+
private final EditorConfigProvider CONFIG_PROVIDER = new EditorConfigProvider();
3333

3434
private final SourceFileFormatter sourceFormatter = new SourceFileFormatter();
3535
private final TableTestFormatter tableFormatter = new TableTestFormatter();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.tabletest.junit.TableTest;
2+
3+
class CalculatorTest {
4+
5+
@TableTest("""
6+
scenario | a | b | sum?
7+
positive | 1 | 2 | 3
8+
negative | -1 | -2 | -3
9+
""")
10+
void shouldAddNumbers(int a, int b, int sum) {
11+
assert a + b == sum;
12+
}
13+
}

testlib/src/test/java/com/diffplug/spotless/java/TableTestFormatterStepTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,37 @@ void behaviorTableFile() {
4343
}
4444
}
4545

46+
@Test
47+
void editorConfigChangesAreHonored() {
48+
// Verifies that a new FormatterStep picks up .editorconfig changes rather than
49+
// returning stale results from a shared cache.
50+
//
51+
// Two steps with the same version share a classloader (via SpotlessCache's
52+
// serialization-based key). If EditorConfigProvider were stored as a static field,
53+
// its permanent ec4j cache would survive across FormatterFunc instances, and the
54+
// second step would still see the indent_size=4 result cached by the first step.
55+
// Making EditorConfigProvider an instance field ensures each FormatterFunc gets a
56+
// fresh provider with an empty cache.
57+
58+
// Step 1: .editorconfig with indent_size=4
59+
setFile(".editorconfig").toContent("[*.java]\nindent_size = 4\n");
60+
FormatterStep step1 = TableTestFormatterStep.create(VERSION, TestProvisioner.mavenCentral());
61+
try (StepHarnessWithFile harness = StepHarnessWithFile.forStep(this, step1)) {
62+
harness.testResource("CalculatorTest.java",
63+
"java/tableTestFormatter/JavaCodeUnformatted.test",
64+
"java/tableTestFormatter/JavaCodeFormatted.test");
65+
}
66+
67+
// Step 2: change .editorconfig to indent_size=2, create a new step
68+
setFile(".editorconfig").toContent("[*.java]\nindent_size = 2\n");
69+
FormatterStep step2 = TableTestFormatterStep.create(VERSION, TestProvisioner.mavenCentral());
70+
try (StepHarnessWithFile harness = StepHarnessWithFile.forStep(this, step2)) {
71+
harness.testResource("CalculatorTest.java",
72+
"java/tableTestFormatter/JavaCodeUnformatted.test",
73+
"java/tableTestFormatter/JavaCodeFormattedWith2SpaceIndent.test");
74+
}
75+
}
76+
4677
@Test
4778
void equality() {
4879
new SerializableEqualityTester() {

0 commit comments

Comments
 (0)