Skip to content

Improve gradle spotless apply suggestion #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Previously, the `check` and `apply` tasks were just marker tasks, and they called `setCheck` and `setApply` on the "worker" task. Now `check` and `apply` are real tasks in their own right, so the marker-task kludge is no longer necessary.
### Changed
* (Power users only) **BREAKING** `SpotlessTask FormatExtension::createIndependentTask` has been removed, and replaced with `SpotlessApply::createIndependentApplyTask`. ([#576](https://github.com/diffplug/spotless/pull/576))

* Improve suggested gradle invocation for running `spotlessApply`. ([#578](https://github.com/diffplug/spotless/pull/578))

## [3.30.0] - 2020-05-11
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
Expand All @@ -30,6 +31,7 @@
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

import com.diffplug.common.base.StandardSystemProperty;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

Expand Down Expand Up @@ -77,12 +79,23 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
}

/** Returns an exception which indicates problem files nicely. */
private static GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) {
private GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) {
return new GradleException(DiffMessageFormatter.builder()
.runToFix("Run 'gradlew spotlessApply' to fix these violations.")
.runToFix("Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.")
.formatter(formatter)
.problemFiles(problemFiles)
.getMessage());
}

private String getTaskPathPrefix() {
return getProject().getPath().equals(":")
? ":"
: getProject().getPath() + ":";
}

private static String calculateGradleCommand() {
return StandardSystemProperty.OS_NAME.value().toLowerCase(Locale.US).contains("win")
? "gradlew.bat"
: "./gradlew";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void assertCheckFailure(Bundle spotless, String... expectedLines) throws
String msg = spotless.checkFailureMsg();

String firstLine = "The following files had format violations:\n";
String lastLine = "\nRun 'gradlew spotlessApply' to fix these violations.";
String lastLine = "\n" + TestFixtures.EXPECTED_RUN_SPOTLESS_APPLY_SUGGESTION;
Assertions.assertThat(msg).startsWith(firstLine).endsWith(lastLine);

String middle = msg.substring(firstLine.length(), msg.length() - lastLine.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;

import org.gradle.api.Project;
import org.junit.Assert;
import org.junit.Test;

import com.diffplug.common.base.StandardSystemProperty;
import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
Expand All @@ -37,10 +35,9 @@
import com.diffplug.spotless.TestProvisioner;

public class PaddedCellTaskTest extends ResourceHarness {
private static final boolean IS_WIN = StandardSystemProperty.OS_NAME.value().toLowerCase(Locale.US).contains("win");

private static String slashify(String input) {
return IS_WIN ? input.replace('/', '\\') : input;
return TestFixtures.IS_WIN ? input.replace('/', '\\') : input;
}

private class Bundle {
Expand Down Expand Up @@ -218,7 +215,7 @@ public void paddedCellCheckCycleFailureMsg() throws IOException {
" @@ -1 +1 @@",
" -CCC",
" +A",
"Run 'gradlew spotlessApply' to fix these violations.");
TestFixtures.EXPECTED_RUN_SPOTLESS_APPLY_SUGGESTION);
}

@Test
Expand All @@ -228,7 +225,7 @@ public void paddedCellCheckConvergeFailureMsg() throws IOException {
slashify(" src/test.converge"),
" @@ -1 +0,0 @@",
" -CCC",
"Run 'gradlew spotlessApply' to fix these violations.");
TestFixtures.EXPECTED_RUN_SPOTLESS_APPLY_SUGGESTION);
}

private void assertFailureMessage(Bundle bundle, String... expectedOutput) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2016 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.util.Locale;

import com.diffplug.common.base.StandardSystemProperty;

interface TestFixtures {
boolean IS_WIN = StandardSystemProperty.OS_NAME.value().toLowerCase(Locale.US).contains("win");
String EXPECTED_RUN_SPOTLESS_APPLY_SUGGESTION = IS_WIN
? "Run 'gradlew.bat :spotlessApply' to fix these violations."
: "Run './gradlew :spotlessApply' to fix these violations.";
}