Skip to content

Commit 11448d9

Browse files
committed
Add a LintSuppressionTest
1 parent 03493f4 commit 11448d9

File tree

2 files changed

+92
-14
lines changed

2 files changed

+92
-14
lines changed

lib/src/main/java/com/diffplug/spotless/LintSuppression.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,15 @@ public void setShortCode(String shortCode) {
4949
this.shortCode = Objects.requireNonNull(shortCode);
5050
}
5151

52-
public boolean suppresses(String relativePath, FormatterStep step, Lint lint) {
53-
return !allows(relativePath, step, lint);
54-
}
55-
56-
private boolean allows(String relativePath, FormatterStep step, Lint lint) {
57-
if (!file.equals(ALL) && !file.equals(relativePath)) {
58-
return false;
59-
}
60-
if (!this.step.equals(ALL) && !this.step.equals(step.getName())) {
61-
return false;
62-
}
63-
if (!this.shortCode.equals(ALL) && !this.shortCode.equals(lint.getRuleId())) {
64-
return false;
52+
public boolean suppresses(String relativePath, FormatterStep formatterStep, Lint lint) {
53+
if (file.equals(ALL) || file.equals(relativePath)) {
54+
if (step.equals(ALL) || formatterStep.getName().equals(this.step)) {
55+
if (shortCode.equals(ALL) || lint.getRuleId().equals(this.shortCode)) {
56+
return true;
57+
}
58+
}
6559
}
66-
return true;
60+
return false;
6761
}
6862

6963
public void ensureDoesNotSuppressAll() {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2024 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 com.diffplug.spotless;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.function.Consumer;
24+
25+
import org.assertj.core.api.AbstractBooleanAssert;
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.diffplug.spotless.generic.EndWithNewlineStep;
29+
30+
public class LintSuppressionTest {
31+
private LintState dummyLintState() {
32+
var lints = new ArrayList<Lint>();
33+
lints.add(Lint.atLine(2, "66", "Order 66"));
34+
var perStep = new ArrayList<List<Lint>>();
35+
perStep.add(lints);
36+
return new LintState(DirtyState.clean(), perStep);
37+
}
38+
39+
private Formatter formatter() {
40+
return Formatter.builder()
41+
.lineEndingsPolicy(LineEnding.UNIX.createPolicy())
42+
.encoding(StandardCharsets.UTF_8)
43+
.steps(List.of(EndWithNewlineStep.create()))
44+
.build();
45+
}
46+
47+
@Test
48+
public void testMatchSingle() {
49+
var noSuppressions = dummyLintState();
50+
noSuppressions.removeSuppressedLints(formatter(), "file", List.of());
51+
assertThat(noSuppressions.isHasLints()).isTrue();
52+
removesLint(s -> s.setStep("blah")).isFalse();
53+
removesLint(s -> s.setStep("endWithNewline")).isTrue();
54+
removesLint(s -> s.setFile("blah")).isFalse();
55+
removesLint(s -> s.setFile("testFile")).isTrue();
56+
removesLint(s -> s.setShortCode("blah")).isFalse();
57+
removesLint(s -> s.setShortCode("66")).isTrue();
58+
}
59+
60+
@Test
61+
public void testMatchDouble() {
62+
removesLint(s -> {
63+
s.setStep("endWithNewline");
64+
s.setShortCode("blah");
65+
}).isFalse();
66+
removesLint(s -> {
67+
s.setStep("blah");
68+
s.setShortCode("66");
69+
}).isFalse();
70+
removesLint(s -> {
71+
s.setStep("endWithNewline");
72+
s.setShortCode("66");
73+
}).isTrue();
74+
}
75+
76+
private AbstractBooleanAssert<?> removesLint(Consumer<LintSuppression> suppression) {
77+
var s = new LintSuppression();
78+
suppression.accept(s);
79+
80+
var ls = dummyLintState();
81+
ls.removeSuppressedLints(formatter(), "testFile", List.of(s));
82+
return assertThat(!ls.isHasLints());
83+
}
84+
}

0 commit comments

Comments
 (0)