Skip to content

Commit 1098702

Browse files
committed
Use MavenSelfie to assert linting error messages.
1 parent 4352a36 commit 1098702

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected void process(String name, Iterable<File> files, Formatter formatter, U
9797
lint.addWarningMessageTo(message, stepName, true);
9898
}
9999
}
100-
message.append("\n Resolve these lints or suppress with `suppressLintsFor`");
100+
message.append("\n Resolve these lints or suppress with `<lintSuppressions>`");
101101
throw new MojoExecutionException(message.toString());
102102
}
103103
} catch (IOException | RuntimeException e) {

plugin-maven/src/test/java/com/diffplug/spotless/maven/biome/BiomeMavenTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertTrue;
2121
import static org.owasp.encoder.Encode.forXml;
22+
import static selfie.MavenSelfie.expectSelfieErrorMsg;
2223

2324
import java.io.File;
2425

@@ -247,9 +248,11 @@ void failureWhenNotParseable() throws Exception {
247248
setFile("biome_test.js").toResource("biome/js/fileBefore.js");
248249
var result = mavenRunner().withArguments("spotless:apply").runHasError();
249250
assertFile("biome_test.js").sameAsResource("biome/js/fileBefore.js");
250-
assertThat(result.stdOutUtf8()).contains("Format with errors is disabled.");
251-
assertThat(result.stdOutUtf8()).contains("Unable to format file");
252-
assertThat(result.stdOutUtf8()).contains("Step 'biome' found problem in 'biome_test.js'");
251+
expectSelfieErrorMsg(result).toBe("""
252+
Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:VERSION:apply (default-cli) on project spotless-maven-plugin-tests: There were 1 lint error(s), they must be fixed or suppressed.
253+
biome_test.js:LINE_UNDEFINED biome(java.lang.RuntimeException) > arguments: [/Users/ntwigg/.m2/repository/com/diffplug/spotless/spotless-data/biome/biome-mac_os-arm64-1.2.0, format, --stdin-file-path, file.json] (...)
254+
Resolve these lints or suppress with `<lintSuppressions>`
255+
""");
253256
}
254257

255258
/**

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.maven.java;
1717

18+
import static selfie.MavenSelfie.expectSelfieErrorMsg;
19+
1820
import org.junit.jupiter.api.Test;
1921

2022
import com.diffplug.spotless.maven.MavenIntegrationHarness;
@@ -27,7 +29,20 @@ void testRemoveWildcardImports() throws Exception {
2729

2830
String path = "src/main/java/test.java";
2931
setFile(path).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
30-
mavenRunner().withArguments("spotless:apply").runNoError();
32+
expectSelfieErrorMsg(mavenRunner().withArguments("spotless:apply").runHasError()).toBe("""
33+
Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:VERSION:apply (default-cli) on project spotless-maven-plugin-tests: There were 5 lint error(s), they must be fixed or suppressed.
34+
src/main/java/test.java:L1 removeWildcardImports(import java.util.*;
35+
) Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
36+
src/main/java/test.java:L2 removeWildcardImports(import static java.util.Collections.*;
37+
) Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
38+
src/main/java/test.java:L5 removeWildcardImports(import io.quarkus.maven.dependency.*;
39+
) Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
40+
src/main/java/test.java:L6 removeWildcardImports(import static io.quarkus.vertx.web.Route.HttpMethod.*;
41+
) Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
42+
src/main/java/test.java:L7 removeWildcardImports(import static org.springframework.web.reactive.function.BodyInserters.*;
43+
) Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
44+
Resolve these lints or suppress with `<lintSuppressions>`
45+
""");
3146
assertFile(path).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
3247
}
3348
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package selfie;
17+
18+
import java.util.stream.Collectors;
19+
20+
import com.diffplug.selfie.Selfie;
21+
import com.diffplug.selfie.StringSelfie;
22+
import com.diffplug.spotless.ProcessRunner;
23+
24+
public class MavenSelfie {
25+
private static final String ERROR_PREFIX = "[ERROR] ";
26+
27+
public static StringSelfie expectSelfieErrorMsg(ProcessRunner.Result result) {
28+
String concatenatedError = result.stdOutUtf8().lines()
29+
.map(line -> line.startsWith(ERROR_PREFIX) ? line.substring(ERROR_PREFIX.length()) : null)
30+
.filter(line -> line != null)
31+
.collect(Collectors.joining("\n"));
32+
33+
String sanitizedVersion = concatenatedError.replaceFirst("com\\.diffplug\\.spotless:spotless-maven-plugin:([^:]+):", "com.diffplug.spotless:spotless-maven-plugin:VERSION:");
34+
35+
int help1 = sanitizedVersion.indexOf("-> [Help 1]");
36+
String trimTrailingString = sanitizedVersion.substring(0, help1);
37+
38+
return Selfie.expectSelfie(trimTrailingString);
39+
}
40+
}

0 commit comments

Comments
 (0)