From 76b626cc73ef469c499a3b5fc33f807c0645766f Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 25 Jan 2023 22:29:12 +0400 Subject: [PATCH 01/10] Handle specifically empty filePath, and not default FS rootDir --- gradle.properties | 2 +- .../java/com/diffplug/spotless/Formatter.java | 12 +++- testlib/build.gradle | 1 + .../com/diffplug/spotless/FormatterTest.java | 66 ++++++++++++++++++- 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5702d21bdd..523440be25 100644 --- a/gradle.properties +++ b/gradle.properties @@ -28,4 +28,4 @@ VER_DURIAN=1.2.0 VER_JGIT=5.13.1.202206130422-r VER_JUNIT=5.9.2 VER_ASSERTJ=3.24.2 -VER_MOCKITO=4.11.0 \ No newline at end of file +VER_MOCKITO=5.0.0 diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index 038e77fa64..3a38893cd6 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -237,8 +237,14 @@ public String compute(String unix, File file) { unix = LineEnding.toUnix(formatted); } } catch (Throwable e) { - String relativePath = rootDir.relativize(file.toPath()).toString(); - exceptionPolicy.handleError(e, step, relativePath); + if (file.getPath().isEmpty()) { + // Path.relativize would fail if rootDir is an absolute path + exceptionPolicy.handleError(e, step, ""); + } else { + // Path may be forged from a different FileSystem than Filesystem.default + String relativePath = rootDir.relativize(rootDir.getFileSystem().getPath(file.getPath())).toString(); + exceptionPolicy.handleError(e, step, relativePath); + } } } return unix; diff --git a/testlib/build.gradle b/testlib/build.gradle index 64d4681805..97b17d0dc6 100644 --- a/testlib/build.gradle +++ b/testlib/build.gradle @@ -12,6 +12,7 @@ dependencies { api "com.diffplug.durian:durian-testlib:${VER_DURIAN}" api "org.junit.jupiter:junit-jupiter:${VER_JUNIT}" api "org.assertj:assertj-core:${VER_ASSERTJ}" + api "org.mockito:mockito-core:$VER_MOCKITO" implementation "com.diffplug.durian:durian-io:${VER_DURIAN}" implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}" diff --git a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java index 06b8e64d31..0faf40bb95 100644 --- a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,15 +15,19 @@ */ package com.diffplug.spotless; +import java.io.File; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import com.diffplug.common.base.StandardSystemProperty; import com.diffplug.spotless.generic.EndWithNewlineStep; @@ -89,4 +93,64 @@ protected Formatter create() { } }.testEquals(); } + + // new File("") can be used if there is no File representing this content. It should not conflict with rootDir.relativize(...) + @Test + public void testExceptionWithEmptyPath() throws Exception { + LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); + Charset encoding = StandardCharsets.UTF_8; + FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); + + Path rootDir = Paths.get(StandardSystemProperty.USER_DIR.value()); + + FormatterStep step = Mockito.mock(FormatterStep.class); + Mockito.when(step.getName()).thenReturn("someFailingStep"); + Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason")); + List steps = Collections.singletonList(step); + + Formatter formatter = Formatter.builder() + .lineEndingsPolicy(lineEndingsPolicy) + .encoding(encoding) + .rootDir(rootDir) + .steps(steps) + .exceptionPolicy(exceptionPolicy) + .build(); + + formatter.compute("someFileContent", new File("")); + } + + // rootDir may be a path not from the default FileSystem + @Test + public void testExceptionWithRootDirIsNotFileSystem() throws Exception { + LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); + Charset encoding = StandardCharsets.UTF_8; + FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); + + Path rootDir = Mockito.mock(Path.class); + Path relativized = Mockito.mock(Path.class); + Mockito.when(rootDir.relativize(Mockito.any(Path.class))).then(invok -> { + Path filePath = invok.getArgument(0); + if (filePath.getFileSystem() == FileSystems.getDefault()) { + throw new IllegalArgumentException("Can not relativize through different FileSystems"); + } + + return relativized; + }); + + FormatterStep step = Mockito.mock(FormatterStep.class); + Mockito.when(step.getName()).thenReturn("someFailingStep"); + Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason")); + List steps = Collections.singletonList(step); + + Formatter formatter = Formatter.builder() + .lineEndingsPolicy(lineEndingsPolicy) + .encoding(encoding) + .rootDir(rootDir) + .steps(steps) + .exceptionPolicy(exceptionPolicy) + .build(); + + formatter.compute("someFileContent", new File("")); + } + } From a8c0bb818de07242feeb08f5731af6ce6fd7a0cb Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 25 Jan 2023 22:53:07 +0400 Subject: [PATCH 02/10] Rollback Mockito to 4.X (as 5.X requires JDK11) --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 523440be25..d99bdac6ce 100644 --- a/gradle.properties +++ b/gradle.properties @@ -28,4 +28,4 @@ VER_DURIAN=1.2.0 VER_JGIT=5.13.1.202206130422-r VER_JUNIT=5.9.2 VER_ASSERTJ=3.24.2 -VER_MOCKITO=5.0.0 +VER_MOCKITO=4.11.0 From 0d34fa703536b7f83b35d45f7eea153d724e2cc3 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Thu, 26 Jan 2023 23:20:30 +0400 Subject: [PATCH 03/10] Switch from new File() to a Sentinel reference --- .../java/com/diffplug/spotless/Formatter.java | 6 ++- .../com/diffplug/spotless/FormatterStep.java | 6 +-- .../diffplug/spotless/FormatterStepImpl.java | 9 ++--- .../com/diffplug/spotless/FormatterTest.java | 38 +++++++++++++++++-- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index 3a38893cd6..be045e7fe1 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -39,6 +39,9 @@ public final class Formatter implements Serializable, AutoCloseable { private static final long serialVersionUID = 1L; + // This Sentinel reference may be used where Formatter requires a File, while there is no actual File to format + public static final File SENTINEL_NO_FILE_ON_DISK = new File("NO_FILE_ON_DISK.sentinel"); + private LineEnding.Policy lineEndingsPolicy; private Charset encoding; private Path rootDir; @@ -237,8 +240,7 @@ public String compute(String unix, File file) { unix = LineEnding.toUnix(formatted); } } catch (Throwable e) { - if (file.getPath().isEmpty()) { - // Path.relativize would fail if rootDir is an absolute path + if (file == SENTINEL_NO_FILE_ON_DISK) { exceptionPolicy.handleError(e, step, ""); } else { // Path may be forged from a different FileSystem than Filesystem.default diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java index 5729f676b2..79793a403e 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,8 +37,8 @@ public interface FormatterStep extends Serializable { * @param rawUnix * the content to format, guaranteed to have unix-style newlines ('\n'); never null * @param file - * the file which {@code rawUnix} was obtained from; never null. Pass an empty file using - * {@code new File("")} if and only if no file is actually associated with {@code rawUnix} + * the file which {@code rawUnix} was obtained from; never null. Pass the reference + * {@code Formatter#SENTINEL_NO_FILE_ON_DISK} if and only if no file is actually associated with {@code rawUnix} * @return the formatted content, guaranteed to only have unix-style newlines; may return null * if the formatter step doesn't have any changes to make * @throws Exception if the formatter step experiences a problem diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index 7663145f51..fe7cabca49 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -109,18 +109,15 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti if (formatter == null) { formatter = formatterSupplier.get(); if (formatter instanceof FormatterFunc.Closeable) { - throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284"); + throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284"); } } return formatter.apply(rawUnix, file); } } - /** A dummy SENTINEL file. */ - static final File SENTINEL = new File(""); - static void checkNotSentinel(File file) { - if (file == SENTINEL) { + if (file == Formatter.SENTINEL_NO_FILE_ON_DISK) { throw new IllegalArgumentException("This step requires the underlying file. If this is a test, use StepHarnessWithFile"); } } diff --git a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java index 0faf40bb95..de96cd2ae3 100644 --- a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java @@ -18,6 +18,7 @@ import java.io.File; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; @@ -94,7 +95,7 @@ protected Formatter create() { }.testEquals(); } - // new File("") can be used if there is no File representing this content. It should not conflict with rootDir.relativize(...) + // new File("") as filePath is known to fail @Test public void testExceptionWithEmptyPath() throws Exception { LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); @@ -116,7 +117,32 @@ public void testExceptionWithEmptyPath() throws Exception { .exceptionPolicy(exceptionPolicy) .build(); - formatter.compute("someFileContent", new File("")); + Assertions.assertThrows(IllegalArgumentException.class, () -> formatter.compute("someFileContent", new File(""))); + } + + // If there is no File actually holding the content, one may rely on Formatter.NO_FILE_ON_DISK + @Test + public void testExceptionWithSentinelNoFileOnDisk() throws Exception { + LineEnding.Policy lineEndingsPolicy = LineEnding.UNIX.createPolicy(); + Charset encoding = StandardCharsets.UTF_8; + FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); + + Path rootDir = Paths.get(StandardSystemProperty.USER_DIR.value()); + + FormatterStep step = Mockito.mock(FormatterStep.class); + Mockito.when(step.getName()).thenReturn("someFailingStep"); + Mockito.when(step.format(Mockito.anyString(), Mockito.any(File.class))).thenThrow(new IllegalArgumentException("someReason")); + List steps = Collections.singletonList(step); + + Formatter formatter = Formatter.builder() + .lineEndingsPolicy(lineEndingsPolicy) + .encoding(encoding) + .rootDir(rootDir) + .steps(steps) + .exceptionPolicy(exceptionPolicy) + .build(); + + formatter.compute("someFileContent", Formatter.SENTINEL_NO_FILE_ON_DISK); } // rootDir may be a path not from the default FileSystem @@ -127,6 +153,12 @@ public void testExceptionWithRootDirIsNotFileSystem() throws Exception { FormatExceptionPolicy exceptionPolicy = FormatExceptionPolicy.failOnlyOnError(); Path rootDir = Mockito.mock(Path.class); + FileSystem customFileSystem = Mockito.mock(FileSystem.class); + Mockito.when(rootDir.getFileSystem()).thenReturn(customFileSystem); + + Path pathFromFile = Mockito.mock(Path.class); + Mockito.when(customFileSystem.getPath(Mockito.anyString())).thenReturn(pathFromFile); + Path relativized = Mockito.mock(Path.class); Mockito.when(rootDir.relativize(Mockito.any(Path.class))).then(invok -> { Path filePath = invok.getArgument(0); @@ -150,7 +182,7 @@ public void testExceptionWithRootDirIsNotFileSystem() throws Exception { .exceptionPolicy(exceptionPolicy) .build(); - formatter.compute("someFileContent", new File("")); + formatter.compute("someFileContent", new File("/some/folder/some.file")); } } From dbea734458f3323cf121f84744838fa865c1a1a6 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Thu, 26 Jan 2023 23:33:27 +0400 Subject: [PATCH 04/10] Refactor mire usages --- lib/src/main/java/com/diffplug/spotless/FormatterFunc.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java index 48a8e810ee..8a8a15076e 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java @@ -127,7 +127,7 @@ public String apply(String unix, File file) throws Exception { @Override public String apply(String unix) throws Exception { - return apply(unix, FormatterStepImpl.SENTINEL); + return apply(unix, Formatter.SENTINEL_NO_FILE_ON_DISK); } }; } @@ -156,7 +156,7 @@ default String apply(String unix, File file) throws Exception { @Override default String apply(String unix) throws Exception { - return apply(unix, FormatterStepImpl.SENTINEL); + return apply(unix, Formatter.SENTINEL_NO_FILE_ON_DISK); } } } From 378a4e8e7f022320b4f0d26844ff390025ee0992 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Thu, 26 Jan 2023 23:39:00 +0400 Subject: [PATCH 05/10] Fix style --- lib/src/main/java/com/diffplug/spotless/FormatterFunc.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java index 8a8a15076e..90d576e8a0 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 4439934095209a332a8b1cfe1037756cbe878108 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Thu, 26 Jan 2023 23:48:21 +0400 Subject: [PATCH 06/10] Rely on existing Sentinel --- lib/src/main/java/com/diffplug/spotless/Formatter.java | 5 +---- lib/src/main/java/com/diffplug/spotless/FormatterFunc.java | 6 +++--- lib/src/main/java/com/diffplug/spotless/FormatterStep.java | 2 +- .../main/java/com/diffplug/spotless/FormatterStepImpl.java | 7 +++++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index be045e7fe1..035452e29c 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -39,9 +39,6 @@ public final class Formatter implements Serializable, AutoCloseable { private static final long serialVersionUID = 1L; - // This Sentinel reference may be used where Formatter requires a File, while there is no actual File to format - public static final File SENTINEL_NO_FILE_ON_DISK = new File("NO_FILE_ON_DISK.sentinel"); - private LineEnding.Policy lineEndingsPolicy; private Charset encoding; private Path rootDir; @@ -240,7 +237,7 @@ public String compute(String unix, File file) { unix = LineEnding.toUnix(formatted); } } catch (Throwable e) { - if (file == SENTINEL_NO_FILE_ON_DISK) { + if (file == FormatterStepImpl.SENTINEL) { exceptionPolicy.handleError(e, step, ""); } else { // Path may be forged from a different FileSystem than Filesystem.default diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java index 90d576e8a0..48a8e810ee 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2021 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -127,7 +127,7 @@ public String apply(String unix, File file) throws Exception { @Override public String apply(String unix) throws Exception { - return apply(unix, Formatter.SENTINEL_NO_FILE_ON_DISK); + return apply(unix, FormatterStepImpl.SENTINEL); } }; } @@ -156,7 +156,7 @@ default String apply(String unix, File file) throws Exception { @Override default String apply(String unix) throws Exception { - return apply(unix, Formatter.SENTINEL_NO_FILE_ON_DISK); + return apply(unix, FormatterStepImpl.SENTINEL); } } } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java index 79793a403e..11a4d5f99e 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java @@ -38,7 +38,7 @@ public interface FormatterStep extends Serializable { * the content to format, guaranteed to have unix-style newlines ('\n'); never null * @param file * the file which {@code rawUnix} was obtained from; never null. Pass the reference - * {@code Formatter#SENTINEL_NO_FILE_ON_DISK} if and only if no file is actually associated with {@code rawUnix} + * {@code FormatterStepImpl#SENTINEL} if and only if no file is actually associated with {@code rawUnix} * @return the formatted content, guaranteed to only have unix-style newlines; may return null * if the formatter step doesn't have any changes to make * @throws Exception if the formatter step experiences a problem diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index fe7cabca49..3b1b5cf278 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -109,15 +109,18 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti if (formatter == null) { formatter = formatterSupplier.get(); if (formatter instanceof FormatterFunc.Closeable) { - throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284"); + throw new AssertionError("NeverUpToDate does not support FormatterFunc.Closeable. See https://github.com/diffplug/spotless/pull/284"); } } return formatter.apply(rawUnix, file); } } + /**This Sentinel reference may be used where Formatter requires a File, while there is no actual File to format */ + public static final File SENTINEL = new File(""); + static void checkNotSentinel(File file) { - if (file == Formatter.SENTINEL_NO_FILE_ON_DISK) { + if (file == SENTINEL) { throw new IllegalArgumentException("This step requires the underlying file. If this is a test, use StepHarnessWithFile"); } } From 7f7cc49fed67ea9b8fc5706427dfc16c830570e0 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Thu, 26 Jan 2023 23:54:13 +0400 Subject: [PATCH 07/10] Fix compilation --- testlib/src/test/java/com/diffplug/spotless/FormatterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java index de96cd2ae3..76f11b3d15 100644 --- a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java @@ -142,7 +142,7 @@ public void testExceptionWithSentinelNoFileOnDisk() throws Exception { .exceptionPolicy(exceptionPolicy) .build(); - formatter.compute("someFileContent", Formatter.SENTINEL_NO_FILE_ON_DISK); + formatter.compute("someFileContent", FormatterStepImpl.SENTINEL); } // rootDir may be a path not from the default FileSystem From 07a57ff66f3fa0ffd8abea94e7c0f6ac4f025b5f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 26 Jan 2023 12:18:26 -0800 Subject: [PATCH 08/10] Rename the sentinel and move it to `Formatter`. --- lib/src/main/java/com/diffplug/spotless/Formatter.java | 5 ++++- lib/src/main/java/com/diffplug/spotless/FormatterFunc.java | 6 +++--- .../main/java/com/diffplug/spotless/FormatterStepImpl.java | 5 +---- .../src/main/java/com/diffplug/spotless/StepHarness.java | 2 +- .../src/test/java/com/diffplug/spotless/FormatterTest.java | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index 035452e29c..a55e8e6504 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -237,7 +237,7 @@ public String compute(String unix, File file) { unix = LineEnding.toUnix(formatted); } } catch (Throwable e) { - if (file == FormatterStepImpl.SENTINEL) { + if (file == NO_FILE_SENTINEL) { exceptionPolicy.handleError(e, step, ""); } else { // Path may be forged from a different FileSystem than Filesystem.default @@ -289,4 +289,7 @@ public void close() { } } } + + /** This Sentinel reference may be used to Formatter requires a File, while there is no actual File to format */ + public static final File NO_FILE_SENTINEL = new File("NO_FILE_SENTINEL"); } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java index 48a8e810ee..14b407a2eb 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -127,7 +127,7 @@ public String apply(String unix, File file) throws Exception { @Override public String apply(String unix) throws Exception { - return apply(unix, FormatterStepImpl.SENTINEL); + return apply(unix, Formatter.NO_FILE_SENTINEL); } }; } @@ -156,7 +156,7 @@ default String apply(String unix, File file) throws Exception { @Override default String apply(String unix) throws Exception { - return apply(unix, FormatterStepImpl.SENTINEL); + return apply(unix, Formatter.NO_FILE_SENTINEL); } } } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java index 3b1b5cf278..17b62e75c6 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java @@ -116,11 +116,8 @@ protected String format(Integer state, String rawUnix, File file) throws Excepti } } - /**This Sentinel reference may be used where Formatter requires a File, while there is no actual File to format */ - public static final File SENTINEL = new File(""); - static void checkNotSentinel(File file) { - if (file == SENTINEL) { + if (file == Formatter.NO_FILE_SENTINEL) { throw new IllegalArgumentException("This step requires the underlying file. If this is a test, use StepHarnessWithFile"); } } diff --git a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java index 71e2a663b5..c611cc738a 100644 --- a/testlib/src/main/java/com/diffplug/spotless/StepHarness.java +++ b/testlib/src/main/java/com/diffplug/spotless/StepHarness.java @@ -88,7 +88,7 @@ public AbstractStringAssert testResourceExceptionMsg(String resourceBefore) { public AbstractStringAssert testExceptionMsg(String before) { try { - formatter.compute(LineEnding.toUnix(before), FormatterStepImpl.SENTINEL); + formatter.compute(LineEnding.toUnix(before), Formatter.NO_FILE_SENTINEL); throw new SecurityException("Expected exception"); } catch (Throwable e) { if (e instanceof SecurityException) { diff --git a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java index 76f11b3d15..314507bd8d 100644 --- a/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/FormatterTest.java @@ -142,7 +142,7 @@ public void testExceptionWithSentinelNoFileOnDisk() throws Exception { .exceptionPolicy(exceptionPolicy) .build(); - formatter.compute("someFileContent", FormatterStepImpl.SENTINEL); + formatter.compute("someFileContent", Formatter.NO_FILE_SENTINEL); } // rootDir may be a path not from the default FileSystem From 9adce647c6dd3e605ddc555c5a972f32d9b192d4 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 26 Jan 2023 12:25:17 -0800 Subject: [PATCH 09/10] Fix javadoc. --- lib/src/main/java/com/diffplug/spotless/Formatter.java | 2 +- lib/src/main/java/com/diffplug/spotless/FormatterStep.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/Formatter.java b/lib/src/main/java/com/diffplug/spotless/Formatter.java index a55e8e6504..3470a3dc69 100644 --- a/lib/src/main/java/com/diffplug/spotless/Formatter.java +++ b/lib/src/main/java/com/diffplug/spotless/Formatter.java @@ -290,6 +290,6 @@ public void close() { } } - /** This Sentinel reference may be used to Formatter requires a File, while there is no actual File to format */ + /** This Sentinel reference may be used to pass string content to a Formatter or FormatterStep when there is no actual File to format */ public static final File NO_FILE_SENTINEL = new File("NO_FILE_SENTINEL"); } diff --git a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java index 11a4d5f99e..ce09f68450 100644 --- a/lib/src/main/java/com/diffplug/spotless/FormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/FormatterStep.java @@ -38,7 +38,7 @@ public interface FormatterStep extends Serializable { * the content to format, guaranteed to have unix-style newlines ('\n'); never null * @param file * the file which {@code rawUnix} was obtained from; never null. Pass the reference - * {@code FormatterStepImpl#SENTINEL} if and only if no file is actually associated with {@code rawUnix} + * {@link Formatter#NO_FILE_SENTINEL} if and only if no file is actually associated with {@code rawUnix} * @return the formatted content, guaranteed to only have unix-style newlines; may return null * if the formatter step doesn't have any changes to make * @throws Exception if the formatter step experiences a problem From 1f78affc868f275b92ad3db7aa9eb7ab3416c16d Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 26 Jan 2023 12:28:40 -0800 Subject: [PATCH 10/10] Update changelog. --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 22e568f566..2af0ab7090 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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] +### Added +* `Formatter` now has a field `public static final File NO_FILE_SENTINEL` which can be used to pass string content to a Formatter or FormatterStep when there is no actual File to format. ([#1525](https://github.com/diffplug/spotless/pull/1525)) ## [2.33.0] - 2023-01-26 ### Added