diff --git a/CHANGES.md b/CHANGES.md index 07b81a8fc5..f5882e5d99 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Added * `ProcessRunner` has added some convenience methods so it can be used for maven testing. ([#1496](https://github.com/diffplug/spotless/pull/1496)) +* `ProcessRunner` allows to limit captured output to a certain number of bytes. ([#1511](https://github.com/diffplug/spotless/pull/1511)) +* `ProcessRunner` is now capable of handling long-running tasks where waiting for exit is delegated to the caller. ([#1511](https://github.com/diffplug/spotless/pull/1511)) * Allow to specify node executable for node-based formatters using `nodeExecutable` parameter ([#1500](https://github.com/diffplug/spotless/pull/1500)) ### Fixed * The default list of type annotations used by `formatAnnotations` has had 8 more annotations from the Checker Framework added [#1494](https://github.com/diffplug/spotless/pull/1494) diff --git a/lib/src/main/java/com/diffplug/spotless/ProcessRunner.java b/lib/src/main/java/com/diffplug/spotless/ProcessRunner.java index 41c664cafe..4e48042184 100644 --- a/lib/src/main/java/com/diffplug/spotless/ProcessRunner.java +++ b/lib/src/main/java/com/diffplug/spotless/ProcessRunner.java @@ -15,6 +15,8 @@ */ package com.diffplug.spotless; +import static java.util.Objects.requireNonNull; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -29,9 +31,12 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; -import edu.umd.cs.findbugs.annotations.Nullable; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; /** @@ -47,10 +52,21 @@ public class ProcessRunner implements AutoCloseable { private final ExecutorService threadStdOut = Executors.newSingleThreadExecutor(); private final ExecutorService threadStdErr = Executors.newSingleThreadExecutor(); - private final ByteArrayOutputStream bufStdOut = new ByteArrayOutputStream(); - private final ByteArrayOutputStream bufStdErr = new ByteArrayOutputStream(); + private final ByteArrayOutputStream bufStdOut; + private final ByteArrayOutputStream bufStdErr; - public ProcessRunner() {} + public ProcessRunner() { + this(-1); + } + + public static ProcessRunner usingRingBuffersOfCapacity(int limit) { + return new ProcessRunner(limit); + } + + private ProcessRunner(int limitedBuffers) { + this.bufStdOut = limitedBuffers >= 0 ? new RingBufferByteArrayOutputStream(limitedBuffers) : new ByteArrayOutputStream(); + this.bufStdErr = limitedBuffers >= 0 ? new RingBufferByteArrayOutputStream(limitedBuffers) : new ByteArrayOutputStream(); + } /** Executes the given shell command (using {@code cmd} on windows and {@code sh} on unix). */ public Result shell(String cmd) throws IOException, InterruptedException { @@ -95,6 +111,36 @@ public Result exec(@Nullable byte[] stdin, List args) throws IOException /** Creates a process with the given arguments, the given byte array is written to stdin immediately. */ public Result exec(@Nullable File cwd, @Nullable Map environment, @Nullable byte[] stdin, List args) throws IOException, InterruptedException { + LongRunningProcess process = start(cwd, environment, stdin, args); + try { + // wait for the process to finish + process.waitFor(); + // collect the output + return process.result(); + } catch (ExecutionException e) { + throw ThrowingEx.asRuntime(e); + } + } + + /** + * Creates a process with the given arguments, the given byte array is written to stdin immediately. + *
+ * Delegates to {@link #start(File, Map, byte[], boolean, List)} with {@code false} for {@code redirectErrorStream}. + */ + public LongRunningProcess start(@Nullable File cwd, @Nullable Map environment, @Nullable byte[] stdin, List args) throws IOException { + return start(cwd, environment, stdin, false, args); + } + + /** + * Creates a process with the given arguments, the given byte array is written to stdin immediately. + *
+ * The process is not waited for, so the caller is responsible for calling {@link LongRunningProcess#waitFor()} (if needed). + *
+ * To dispose this {@code ProcessRunner} instance, either call {@link #close()} or {@link LongRunningProcess#close()}. After + * {@link #close()} or {@link LongRunningProcess#close()} has been called, this {@code ProcessRunner} instance must not be used anymore. + */ + public LongRunningProcess start(@Nullable File cwd, @Nullable Map environment, @Nullable byte[] stdin, boolean redirectErrorStream, List args) throws IOException { + checkState(); ProcessBuilder builder = new ProcessBuilder(args); if (cwd != null) { builder.directory(cwd); @@ -105,20 +151,20 @@ public Result exec(@Nullable File cwd, @Nullable Map environment if (stdin == null) { stdin = new byte[0]; } + if (redirectErrorStream) { + builder.redirectErrorStream(true); + } + Process process = builder.start(); Future outputFut = threadStdOut.submit(() -> drainToBytes(process.getInputStream(), bufStdOut)); - Future errorFut = threadStdErr.submit(() -> drainToBytes(process.getErrorStream(), bufStdErr)); + Future errorFut = null; + if (!redirectErrorStream) { + errorFut = threadStdErr.submit(() -> drainToBytes(process.getErrorStream(), bufStdErr)); + } // write stdin process.getOutputStream().write(stdin); process.getOutputStream().close(); - // wait for the process to finish - int exitCode = process.waitFor(); - try { - // collect the output - return new Result(args, exitCode, outputFut.get(), errorFut.get()); - } catch (ExecutionException e) { - throw ThrowingEx.asRuntime(e); - } + return new LongRunningProcess(process, args, outputFut, errorFut); } private static void drain(InputStream input, OutputStream output) throws IOException { @@ -141,17 +187,24 @@ public void close() { threadStdErr.shutdown(); } + /** Checks if this {@code ProcessRunner} instance is still usable. */ + private void checkState() { + if (threadStdOut.isShutdown() || threadStdErr.isShutdown()) { + throw new IllegalStateException("ProcessRunner has been closed and must not be used anymore."); + } + } + @SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) public static class Result { private final List args; private final int exitCode; private final byte[] stdOut, stdErr; - public Result(List args, int exitCode, byte[] stdOut, byte[] stdErr) { + public Result(@Nonnull List args, int exitCode, @Nonnull byte[] stdOut, @Nullable byte[] stdErr) { this.args = args; this.exitCode = exitCode; this.stdOut = stdOut; - this.stdErr = stdErr; + this.stdErr = (stdErr == null ? new byte[0] : stdErr); } public List args() { @@ -222,8 +275,86 @@ public String toString() { } }; perStream.accept(" stdout", stdOut); - perStream.accept(" stderr", stdErr); + if (stdErr.length > 0) { + perStream.accept(" stderr", stdErr); + } return builder.toString(); } } + + /** + * A long-running process that can be waited for. + */ + public class LongRunningProcess extends Process implements AutoCloseable { + + private final Process delegate; + private final List args; + private final Future outputFut; + private final Future errorFut; + + public LongRunningProcess(@Nonnull Process delegate, @Nonnull List args, @Nonnull Future outputFut, @Nullable Future errorFut) { + this.delegate = requireNonNull(delegate); + this.args = args; + this.outputFut = outputFut; + this.errorFut = errorFut; + } + + @Override + public OutputStream getOutputStream() { + return delegate.getOutputStream(); + } + + @Override + public InputStream getInputStream() { + return delegate.getInputStream(); + } + + @Override + public InputStream getErrorStream() { + return delegate.getErrorStream(); + } + + @Override + public int waitFor() throws InterruptedException { + return delegate.waitFor(); + } + + @Override + public boolean waitFor(long timeout, TimeUnit unit) throws InterruptedException { + return delegate.waitFor(timeout, unit); + } + + @Override + public int exitValue() { + return delegate.exitValue(); + } + + @Override + public void destroy() { + delegate.destroy(); + } + + @Override + public Process destroyForcibly() { + return delegate.destroyForcibly(); + } + + @Override + public boolean isAlive() { + return delegate.isAlive(); + } + + public Result result() throws ExecutionException, InterruptedException { + int exitCode = waitFor(); + return new Result(args, exitCode, this.outputFut.get(), (this.errorFut != null ? this.errorFut.get() : null)); + } + + @Override + public void close() { + if (isAlive()) { + destroy(); + } + ProcessRunner.this.close(); + } + } } diff --git a/lib/src/main/java/com/diffplug/spotless/RingBufferByteArrayOutputStream.java b/lib/src/main/java/com/diffplug/spotless/RingBufferByteArrayOutputStream.java new file mode 100644 index 0000000000..da4fc6aa04 --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/RingBufferByteArrayOutputStream.java @@ -0,0 +1,135 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +class RingBufferByteArrayOutputStream extends ByteArrayOutputStream { + + private final int limit; + + private int zeroIndexPointer = 0; + + private boolean isOverLimit = false; + + public RingBufferByteArrayOutputStream(int limit) { + this(limit, 32); + } + + public RingBufferByteArrayOutputStream(int limit, int initialCapacity) { + super(initialCapacity); + if (limit < initialCapacity) { + throw new IllegalArgumentException("Limit must be greater than initial capacity. Limit: " + limit + ", initial capacity: " + initialCapacity); + } + if (limit < 2) { + throw new IllegalArgumentException("Limit must be greater than or equal to 2 but is " + limit); + } + if (limit % 2 != 0) { + throw new IllegalArgumentException("Limit must be an even number but is " + limit); // to fit 16 bit unicode chars + } + this.limit = limit; + } + + // ---- writing + @Override + public synchronized void write(int b) { + if (count < limit) { + super.write(b); + return; + } + isOverLimit = true; + buf[zeroIndexPointer] = (byte) b; + zeroIndexPointer = (zeroIndexPointer + 1) % limit; + } + + @Override + public synchronized void write(byte[] b, int off, int len) { + int remaining = limit - count; + if (remaining >= len) { + super.write(b, off, len); + return; + } + if (remaining > 0) { + // write what we can "normally" + super.write(b, off, remaining); + // rest delegated + write(b, off + remaining, len - remaining); + return; + } + // we are over the limit + isOverLimit = true; + // write till limit is reached + int writeTillLimit = Math.min(len, limit - zeroIndexPointer); + System.arraycopy(b, off, buf, zeroIndexPointer, writeTillLimit); + zeroIndexPointer = (zeroIndexPointer + writeTillLimit) % limit; + if (writeTillLimit < len) { + // write rest + write(b, off + writeTillLimit, len - writeTillLimit); + } + } + + @Override + public synchronized void reset() { + super.reset(); + zeroIndexPointer = 0; + isOverLimit = false; + } + + // ---- output + @Override + public synchronized void writeTo(OutputStream out) throws IOException { + if (!isOverLimit) { + super.writeTo(out); + return; + } + out.write(buf, zeroIndexPointer, limit - zeroIndexPointer); + out.write(buf, 0, zeroIndexPointer); + } + + @Override + public synchronized byte[] toByteArray() { + if (!isOverLimit) { + return super.toByteArray(); + } + byte[] result = new byte[limit]; + System.arraycopy(buf, zeroIndexPointer, result, 0, limit - zeroIndexPointer); + System.arraycopy(buf, 0, result, limit - zeroIndexPointer, zeroIndexPointer); + return result; + } + + @SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "We want to use the default encoding here since this is contract on ByteArrayOutputStream") + @Override + public synchronized String toString() { + if (!isOverLimit) { + return super.toString(); + } + return new String(buf, zeroIndexPointer, limit - zeroIndexPointer) + new String(buf, 0, zeroIndexPointer); + } + + @Override + public synchronized String toString(String charsetName) throws UnsupportedEncodingException { + if (!isOverLimit) { + return super.toString(charsetName); + } + return new String(buf, zeroIndexPointer, limit - zeroIndexPointer, charsetName) + new String(buf, 0, zeroIndexPointer, charsetName); + } + +} diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NpmProcess.java b/lib/src/main/java/com/diffplug/spotless/npm/NpmProcess.java index 1675a4aa4e..6384900d82 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NpmProcess.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NpmProcess.java @@ -19,9 +19,15 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; +import com.diffplug.spotless.ProcessRunner; +import com.diffplug.spotless.ProcessRunner.LongRunningProcess; + class NpmProcess { private final File workingDir; @@ -30,10 +36,13 @@ class NpmProcess { private final File nodeExecutable; + private final ProcessRunner processRunner; + NpmProcess(File workingDir, File npmExecutable, File nodeExecutable) { this.workingDir = workingDir; this.npmExecutable = npmExecutable; this.nodeExecutable = nodeExecutable; + processRunner = ProcessRunner.usingRingBuffersOfCapacity(100 * 1024); // 100kB } void install() { @@ -44,32 +53,27 @@ void install() { "--prefer-offline"); } - Process start() { + LongRunningProcess start() { // adding --scripts-prepend-node-path=true due to https://github.com/diffplug/spotless/issues/619#issuecomment-648018679 return npm("start", "--scripts-prepend-node-path=true"); } private void npmAwait(String... args) { - final Process npmProcess = npm(args); - - try { + try (LongRunningProcess npmProcess = npm(args)) { if (npmProcess.waitFor() != 0) { - throw new NpmProcessException("Running npm command '" + commandLine(args) + "' failed with exit code: " + npmProcess.exitValue()); + throw new NpmProcessException("Running npm command '" + commandLine(args) + "' failed with exit code: " + npmProcess.exitValue() + "\n\n" + npmProcess.result()); } } catch (InterruptedException e) { throw new NpmProcessException("Running npm command '" + commandLine(args) + "' was interrupted.", e); + } catch (ExecutionException e) { + throw new NpmProcessException("Running npm command '" + commandLine(args) + "' failed.", e); } } - private Process npm(String... args) { + private LongRunningProcess npm(String... args) { List processCommand = processCommand(args); try { - ProcessBuilder processBuilder = new ProcessBuilder() - .inheritIO() - .directory(this.workingDir) - .command(processCommand); - addEnvironmentVariables(processBuilder); - return processBuilder.start(); + return processRunner.start(this.workingDir, environmentVariables(), null, true, processCommand); } catch (IOException e) { throw new NpmProcessException("Failed to launch npm command '" + commandLine(args) + "'.", e); } @@ -82,8 +86,10 @@ private List processCommand(String... args) { return command; } - private void addEnvironmentVariables(ProcessBuilder processBuilder) { - processBuilder.environment().put("PATH", this.nodeExecutable.getParentFile().getAbsolutePath() + File.pathSeparator + System.getenv("PATH")); + private Map environmentVariables() { + Map environmentVariables = new HashMap<>(); + environmentVariables.put("PATH", this.nodeExecutable.getParentFile().getAbsolutePath() + File.pathSeparator + System.getenv("PATH")); + return environmentVariables; } private String commandLine(String... args) { diff --git a/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java index 20ff71d08e..22dd4586ee 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java @@ -122,7 +122,14 @@ public String applyWithFile(String unix, File file) throws Exception { FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, Math.min(50, unix.length())) + "[...]' in file '" + file + "'"); final String prettierConfigOptionsWithFilepath = assertFilepathInConfigOptions(file); - return restService.format(unix, prettierConfigOptionsWithFilepath); + try { + return restService.format(unix, prettierConfigOptionsWithFilepath); + } catch (SimpleRestClient.SimpleRestResponseException e) { + if (e.getStatusCode() != 200 && e.getResponseMessage().contains("No parser could be inferred")) { + throw new PrettierMissingParserException(file, e); + } + throw e; + } } private String assertFilepathInConfigOptions(File file) { @@ -141,4 +148,5 @@ private String assertFilepathInConfigOptions(File file) { return "{" + filePathOption + (hasAnyConfigOption ? "," : "") + prettierConfigOptions.substring(startOfConfigOption + 1); } } + } diff --git a/lib/src/main/java/com/diffplug/spotless/npm/PrettierMissingParserException.java b/lib/src/main/java/com/diffplug/spotless/npm/PrettierMissingParserException.java new file mode 100644 index 0000000000..6956545135 --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/npm/PrettierMissingParserException.java @@ -0,0 +1,117 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.npm; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +import javax.annotation.Nonnull; + +class PrettierMissingParserException extends RuntimeException { + private static final long serialVersionUID = 1L; + + private static final Map EXTENSIONS_TO_PLUGINS; + + static { + Map plugins = new HashMap<>(); + // ---- official plugins + plugins.put(".php", "@prettier/plugin-php"); + plugins.put(".pug", "@prettier/plugin-pug"); + plugins.put(".rb", "@prettier/plugin-ruby"); + plugins.put(".xml", "@prettier/plugin-xml"); + + // ---- community plugins + // default namings: astro, elm, java, jsonata, prisma, properties, sh, sql, svelte, toml + plugins.put(".trigger", "prettier-plugin-apex"); + plugins.put(".cls", "prettier-plugin-apex"); + plugins.put(".html.erb", "prettier-plugin-erb"); + Arrays.asList(".glsl", + ".fp", + ".frag", + ".frg", + ".fs", + ".fsh", + ".fshader", + ".geo", + ".geom", + ".glslf", + ".glslv", + ".gs", + ".gshader", + ".rchit", + ".rmiss", + ".shader", + ".tesc", + ".tese", + ".vert", + ".vrx", + ".vsh", + ".vshader").forEach(ext -> plugins.put(ext, "prettier-plugin-glsl")); + Arrays.asList(".go.html", + ".gohtml", + ".gotmpl", + ".go.tmpl", + ".tmpl", + ".tpl", + ".html.tmpl", + ".html.tpl").forEach(ext -> plugins.put(ext, "prettier-plugin-go-template")); + plugins.put(".kt", "kotlin"); + plugins.put(".mo", "motoko"); + Arrays.asList(".nginx", ".nginxconf").forEach(ext -> plugins.put(ext, "prettier-plugin-nginx")); + plugins.put(".sol", "prettier-plugin-solidity"); + + EXTENSIONS_TO_PLUGINS = Collections.unmodifiableMap(plugins); + } + + private final File file; + + public PrettierMissingParserException(@Nonnull File file, Exception cause) { + super("Prettier could not infer a parser for file '" + file + "'. Maybe you need to include a prettier plugin in devDependencies?\n\n" + recommendPlugin(file), cause); + this.file = Objects.requireNonNull(file); + } + + private static String recommendPlugin(File file) { + String pluginName = guessPlugin(file); + return "A good candidate for file '" + file + "' is '" + pluginName + "\n" + + "See if you can find it on \n" + + "or search on npmjs.com for a plugin matching that name: " + + String.format("", pluginName) + + "\n\n" + + "For instructions on how to include plugins for prettier in spotless see our documentation:\n" + + "- for gradle \n" + + "- for maven "; + } + + private static String guessPlugin(File file) { + return EXTENSIONS_TO_PLUGINS.entrySet().stream() + .filter(entry -> file.getName().endsWith(entry.getKey())) + .findFirst() + .map(entry -> entry.getValue()) + .orElse("prettier-plugin-" + extension(file)); + } + + public String fileType() { + return extension(file); + } + + private static String extension(File file) { + return file.getName().substring(file.getName().lastIndexOf('.') + 1); + } +} diff --git a/lib/src/test/java/com/diffplug/spotless/RingBufferByteArrayOutputStreamTest.java b/lib/src/test/java/com/diffplug/spotless/RingBufferByteArrayOutputStreamTest.java new file mode 100644 index 0000000000..94fa49dbc1 --- /dev/null +++ b/lib/src/test/java/com/diffplug/spotless/RingBufferByteArrayOutputStreamTest.java @@ -0,0 +1,179 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.stream.Stream; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class RingBufferByteArrayOutputStreamTest { + + private final byte[] bytes = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void toStringBehavesNormallyWithinLimit(String name, ByteWriteStrategy writeStrategy) { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(12, 1); + writeStrategy.write(stream, bytes); + Assertions.assertThat(stream.toString()).isEqualTo("0123456789"); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void toStringBehavesOverwritingOverLimit(String name, ByteWriteStrategy writeStrategy) { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(4, 1); + writeStrategy.write(stream, bytes); + Assertions.assertThat(stream.toString()).hasSize(4); + Assertions.assertThat(stream.toString()).isEqualTo("6789"); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void toStringBehavesNormallyAtExactlyLimit(String name, ByteWriteStrategy writeStrategy) { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(bytes.length, 1); + writeStrategy.write(stream, bytes); + Assertions.assertThat(stream.toString()).isEqualTo("0123456789"); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void toByteArrayBehavesNormallyWithinLimit(String name, ByteWriteStrategy writeStrategy) { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(12, 1); + writeStrategy.write(stream, bytes); + Assertions.assertThat(stream.toByteArray()).isEqualTo(bytes); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void toByteArrayBehavesOverwritingOverLimit(String name, ByteWriteStrategy writeStrategy) { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(4, 1); + writeStrategy.write(stream, bytes); + Assertions.assertThat(stream.toByteArray()).hasSize(4); + Assertions.assertThat(stream.toByteArray()).isEqualTo(new byte[]{'6', '7', '8', '9'}); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void toByteArrayBehavesOverwritingAtExactlyLimit(String name, ByteWriteStrategy writeStrategy) { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(bytes.length, 1); + writeStrategy.write(stream, bytes); + Assertions.assertThat(stream.toByteArray()).isEqualTo(bytes); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void writeToBehavesNormallyWithinLimit(String name, ByteWriteStrategy writeStrategy) throws IOException { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(12, 1); + writeStrategy.write(stream, bytes); + ByteArrayOutputStream target = new ByteArrayOutputStream(); + stream.writeTo(target); + Assertions.assertThat(target.toByteArray()).isEqualTo(bytes); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void writeToBehavesOverwritingOverLimit(String name, ByteWriteStrategy writeStrategy) throws IOException { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(4, 1); + writeStrategy.write(stream, bytes); + ByteArrayOutputStream target = new ByteArrayOutputStream(); + stream.writeTo(target); + Assertions.assertThat(target.toByteArray()).hasSize(4); + Assertions.assertThat(target.toByteArray()).isEqualTo(new byte[]{'6', '7', '8', '9'}); + } + + @ParameterizedTest(name = "{index} writeStrategy: {0}") + @MethodSource("writeStrategies") + void writeToBehavesNormallyAtExactlyLimit(String name, ByteWriteStrategy writeStrategy) throws IOException { + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(bytes.length, 1); + writeStrategy.write(stream, bytes); + ByteArrayOutputStream target = new ByteArrayOutputStream(); + stream.writeTo(target); + Assertions.assertThat(target.toByteArray()).isEqualTo(bytes); + } + + @Test + void writeToBehavesCorrectlyWhenOverLimitMultipleCalls() { + // this test explicitly captures a border case where the buffer is not empty but can exactly fit what we are writing + RingBufferByteArrayOutputStream stream = new RingBufferByteArrayOutputStream(2, 1); + stream.write('0'); + stream.write(new byte[]{'1', '2'}, 0, 2); + Assertions.assertThat(stream.toString()).hasSize(2); + Assertions.assertThat(stream.toString()).isEqualTo("12"); + } + + private static Stream writeStrategies() { + return Stream.of( + Arguments.of("writeAllAtOnce", allAtOnce()), + Arguments.of("writeOneByteAtATime", oneByteAtATime()), + Arguments.of("writeTwoBytesAtATime", twoBytesAtATime()), + Arguments.of("writeOneAndThenTwoBytesAtATime", oneAndThenTwoBytesAtATime()), + Arguments.of("firstFourBytesAndThenTheRest", firstFourBytesAndThenTheRest())); + } + + private static ByteWriteStrategy allAtOnce() { + return (stream, bytes) -> stream.write(bytes, 0, bytes.length); + } + + private static ByteWriteStrategy oneByteAtATime() { + return (stream, bytes) -> { + for (byte b : bytes) { + stream.write(b); + } + }; + } + + private static ByteWriteStrategy twoBytesAtATime() { + return (stream, bytes) -> { + for (int i = 0; i < bytes.length; i += 2) { + stream.write(bytes, i, 2); + } + }; + } + + private static ByteWriteStrategy oneAndThenTwoBytesAtATime() { + return (stream, bytes) -> { + int written = 0; + for (int i = 0; i + 3 < bytes.length; i += 3) { + stream.write(bytes, i, 1); + stream.write(bytes, i + 1, 2); + written += 3; + } + if (written < bytes.length) { + stream.write(bytes, written, bytes.length - written); + } + + }; + } + + private static ByteWriteStrategy firstFourBytesAndThenTheRest() { + return (stream, bytes) -> { + stream.write(bytes, 0, 4); + stream.write(bytes, 4, bytes.length - 4); + }; + } + + @FunctionalInterface + private interface ByteWriteStrategy { + void write(RingBufferByteArrayOutputStream stream, byte[] bytes); + } + +} diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index cac247a307..f8a6242adf 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -10,6 +10,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed * The default list of type annotations used by `formatAnnotations` has had 8 more annotations from the Checker Framework added [#1494](https://github.com/diffplug/spotless/pull/1494) ### Changes +* Prettier will now suggest to install plugins if a parser cannot be inferred from the file extension ([#1511](https://github.com/diffplug/spotless/pull/1511)) + ## [6.13.0] - 2023-01-14 ### Added diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java index bc8ad1148e..4c458e3ca7 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java @@ -135,6 +135,29 @@ void useJavaCommunityPlugin() throws IOException { assertFile("JavaTest.java").sameAsResource("npm/prettier/plugins/java-test.clean"); } + @Test + void suggestsMissingJavaCommunityPlugin() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "def prettierConfig = [:]", + "prettierConfig['tabWidth'] = 4", + "def prettierPackages = [:]", + "prettierPackages['prettier'] = '2.0.5'", + "spotless {", + " format 'java', {", + " target 'JavaTest.java'", + " prettier(prettierPackages).config(prettierConfig)", + " }", + "}"); + setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty"); + final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail(); + Assertions.assertThat(spotlessApply.getOutput()).contains("could not infer a parser"); + Assertions.assertThat(spotlessApply.getOutput()).contains("prettier-plugin-java"); + } + @Test void usePhpCommunityPlugin() throws IOException { setFile("build.gradle").toLines( diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index ecb67f231f..f1ab739b62 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -13,6 +13,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * The default list of type annotations used by `formatAnnotations` has had 8 more annotations from the Checker Framework added [#1494](https://github.com/diffplug/spotless/pull/1494) ### Changes * Spotless' custom build was replaced by [`maven-plugin-development`](https://github.com/britter/maven-plugin-development). ([#1496](https://github.com/diffplug/spotless/pull/1496) fixes [#554](https://github.com/diffplug/spotless/issues/554)) +* Prettier will now suggest to install plugins if a parser cannot be inferred from the file extension ([#1511](https://github.com/diffplug/spotless/pull/1511)) + ## [2.30.0] - 2023-01-13 ### Added