diff --git a/CHANGES.md b/CHANGES.md index 5a3c98ad78..8287369291 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 +- Add `DiffMessageFormatter.Builder.showAllFailures(boolean)` to list every failing file instead of only counting them. ([#2418](https://github.com/diffplug/spotless/issues/2418)) ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java index fb9d176eac..4e229680ba 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * Copyright 2016-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -118,6 +118,7 @@ private Builder() {} private String runToFix; private CleanProvider formatter; private List problemFiles; + private boolean showAllFailures; /** "Run 'gradlew spotlessApply' to fix these violations." */ public Builder runToFix(String runToFix) { @@ -141,13 +142,22 @@ public Builder problemFiles(List problemFiles) { return this; } + /** + * If true, every problem file which didn't fit in the diff is listed by name, instead of + * only counting them once there are {@link DiffMessageFormatter#MAX_FILES_TO_LIST} or more. Defaults to false. + */ + public Builder showAllFailures(boolean showAllFailures) { + this.showAllFailures = showAllFailures; + return this; + } + /** Returns the error message. */ public String getMessage() { try { Objects.requireNonNull(runToFix, "runToFix"); Objects.requireNonNull(formatter, "formatter"); Objects.requireNonNull(problemFiles, "problemFiles"); - DiffMessageFormatter diffFormater = new DiffMessageFormatter(formatter, problemFiles); + DiffMessageFormatter diffFormater = new DiffMessageFormatter(formatter, problemFiles, showAllFailures); return "The following files had format violations:\n" + diffFormater.buffer + runToFix; @@ -165,7 +175,7 @@ public String getMessage() { private final CleanProvider formatter; - private DiffMessageFormatter(CleanProvider formatter, List problemFiles) throws IOException { + private DiffMessageFormatter(CleanProvider formatter, List problemFiles, boolean showAllFailures) throws IOException { this.formatter = Objects.requireNonNull(formatter, "formatter"); ListIterator problemIter = problemFiles.listIterator(); while (problemIter.hasNext() && numLines < MAX_CHECK_MESSAGE_LINES) { @@ -174,7 +184,7 @@ private DiffMessageFormatter(CleanProvider formatter, List problemFiles) t } if (problemIter.hasNext()) { int remainingFiles = problemFiles.size() - problemIter.nextIndex(); - if (remainingFiles >= MAX_FILES_TO_LIST) { + if (remainingFiles >= MAX_FILES_TO_LIST && !showAllFailures) { buffer.append("Violations also present in ").append(remainingFiles).append(" other files.\n"); } else { buffer.append("Violations also present in:\n"); diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67e6658fad..970d04910e 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +- `spotlessCheck` lists every failing file instead of only counting them when run with `-PspotlessShowAllFailures`. ([#2418](https://github.com/diffplug/spotless/issues/2418)) ### Fixed - Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190)) - Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 82c9dc377c..0e14bf669f 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -2235,6 +2235,14 @@ tasks.withType(com.diffplug.gradle.spotless.SpotlessApply).configureEach { `changedFilesReport` is an output of the apply task, so another task can declare it as an input (e.g. `inputs.file(tasks.named('spotlessJavaApply').flatMap { it.changedFilesReport })`) and Gradle will run the apply task first, without an explicit `dependsOn`. +### Which files does `spotlessCheck` complain about? + +When many files are misformatted, `spotlessCheck` shows a diff for the first few and then only says `Violations also present in N other files.` To list every one of them by name, pass `-PspotlessShowAllFailures` (or put `spotlessShowAllFailures=true` in `gradle.properties`): + +```console +user@machine repo % ./gradlew spotlessCheck -PspotlessShowAllFailures +``` + ## Can I apply Spotless to specific files? The Gradle plugin does not read a `spotlessFiles` property like the Maven plugin does (`-PspotlessFiles` was deprecated in `4.3.0` and removed in `5.0.0`). To format one or a few files, pass their absolute paths to the [IDE hook](IDE_HOOK.md) as a comma-separated list: diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessCheck.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessCheck.java index 1bd3d779a4..fdb3682461 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessCheck.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessCheck.java @@ -51,6 +51,12 @@ public abstract class SpotlessCheck extends SpotlessTaskService.ClientTask { @Input public abstract Property getRunToFixMessage(); + /** Lists every failing file instead of a count once there are too many, defaults to {@code -PspotlessShowAllFailures}. */ + @Input + public abstract Property getShowAllFailures(); + + static final String SHOW_ALL_FAILURES = "spotlessShowAllFailures"; + public void performActionTest() throws IOException { performAction(true); } @@ -79,6 +85,7 @@ private void performAction(boolean isTest) throws IOException { getSpotlessCleanDirectory().get().toPath(), getEncoding().get()) .problemFiles(unformattedFiles) + .showAllFailures(getShowAllFailures().get()) .getMessage()); } else if (!lintsFiles.isEmpty()) { // We only show lints if there are no unformatted files. @@ -146,6 +153,8 @@ void init(TaskProvider impl) { getEncoding().set(impl.map(SpotlessTask::getEncoding)); getRunToFixMessage().convention( "Run '" + calculateGradleCommand() + " " + spotlessApplySelector(getProject()) + "' to fix all violations."); + String showAllFailures = GradleCompat.findOptionalProperty(getProject(), SHOW_ALL_FAILURES); + getShowAllFailures().convention(showAllFailures != null && !showAllFailures.equalsIgnoreCase("false")); } private static String calculateGradleCommand() { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java index 7f8459b02a..4084640e75 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * Copyright 2016-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -317,6 +317,33 @@ void manyManyFiles() throws Exception { "Violations also present in " + DiffMessageFormatter.MAX_FILES_TO_LIST + " other files."); } + @Test + void manyManyFilesShowAllFailures() throws Exception { + List testFiles = new ArrayList<>(); + for (int i = 0; i < 9 + DiffMessageFormatter.MAX_FILES_TO_LIST; i++) { + String fileName = "%02d".formatted(i) + ".txt"; + testFiles.add(setFile(fileName).toContent("1\r\n2\r\n")); + } + Bundle spotless = create(testFiles); + spotless.check.get().getShowAllFailures().set(true); + String msg = spotless.checkFailureMsg(); + Assertions.assertThat(msg) + .doesNotContain("other files.") + .contains(StringPrinter.buildStringFromLines( + " ... (2 more lines that didn't fit)", + "Violations also present in:", + " 09.txt", + " 10.txt", + " 11.txt", + " 12.txt", + " 13.txt", + " 14.txt", + " 15.txt", + " 16.txt", + " 17.txt", + " 18.txt")); + } + @Test void longFile() throws Exception { StringBuilder builder = new StringBuilder(); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ShowAllFailuresTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ShowAllFailuresTest.java new file mode 100644 index 0000000000..c43996e9f7 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ShowAllFailuresTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2026 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.gradle.spotless; + +import java.io.IOException; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ShowAllFailuresTest extends GradleIntegrationHarness { + private static final int FILE_COUNT = 30; + + @BeforeEach + void createProjectWithManyBadFiles() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " format 'misc', {", + " target '*.txt'", + " leadingTabsToSpaces(2)", + " }", + "}"); + for (int i = 0; i < FILE_COUNT; i++) { + setFile("%02d.txt".formatted(i)).toContent("\thello\n"); + } + } + + @Test + void summaryByDefault() throws IOException { + String output = gradleRunner().withArguments("spotlessCheck").buildAndFail().getOutput(); + Assertions.assertThat(output) + .contains(" other files.") + .doesNotContain(" %02d.txt".formatted(FILE_COUNT - 1)); + } + + @Test + void listsEveryFileWithProperty() throws IOException { + String output = gradleRunner().withArguments("spotlessCheck", "-PspotlessShowAllFailures").buildAndFail().getOutput(); + Assertions.assertThat(output) + .doesNotContain(" other files.") + .contains("Violations also present in:"); + for (int i = 0; i < FILE_COUNT; i++) { + Assertions.assertThat(output).contains(" %02d.txt".formatted(i)); + } + } + + @Test + void listsEveryFileWithGradleProperties() throws IOException { + setFile("gradle.properties").toContent("spotlessShowAllFailures=true"); + String output = gradleRunner().withArguments("spotlessCheck").buildAndFail().getOutput(); + Assertions.assertThat(output) + .doesNotContain(" other files.") + .contains(" %02d.txt".formatted(FILE_COUNT - 1)); + } + + @Test + void summaryWhenPropertyIsFalse() throws IOException { + String output = gradleRunner().withArguments("spotlessCheck", "-PspotlessShowAllFailures=false").buildAndFail().getOutput(); + Assertions.assertThat(output).contains(" other files."); + } +}