Skip to content

Commit 45d2122

Browse files
authored
Add more tests to SpecificFilesTest (#530)
* Add Tests Add tests for the following cases: * Empty pattern passed to spotlessFiles * A pattern that matches no files passed to spotlessFiles * Invalid regexp passed to spotlessFiles * Spotless is configured with a Kotlin build script Also a little reorganization, and a change required to support Kotlin build script was added to GradleIntegrationTest.java so that other tests can make use of it. * Make requested changes: * Remove Kotlin support from GradleIntegrationTest and move it into SpecificFilesTest * Add information about the change to the [Unreleased] tag in CHANGES.md
1 parent c007dba commit 45d2122

File tree

2 files changed

+96
-19
lines changed

2 files changed

+96
-19
lines changed

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
* Add tests to `SpecificFilesTest` to fix [#529](https://github.com/diffplug/spotless/issues/529)
67

78
## [3.27.1] - 2020-01-14
89
### Fixed

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpecificFilesTest.java

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,59 @@
1717

1818
import java.io.IOException;
1919

20+
import org.gradle.testkit.runner.*;
21+
import org.junit.Ignore;
2022
import org.junit.Test;
2123

2224
public class SpecificFilesTest extends GradleIntegrationTest {
23-
private String testFile(int number, boolean absolute) throws IOException {
24-
String rel = "src/main/java/test" + number + ".java";
25+
private String testFilePath(int number) {
26+
return testFilePath(number, true);
27+
}
28+
29+
private String testFilePath(int number, boolean absolute) {
30+
String relPath = "src/main/java/test" + number + ".java";
2531
if (absolute) {
26-
return rootFolder() + "/" + rel;
32+
return rootFolder() + "/" + relPath;
2733
} else {
28-
return rel;
34+
return relPath;
2935
}
3036
}
3137

32-
private String testFile(int number) throws IOException {
33-
return testFile(number, false);
38+
private String fixture() {
39+
return fixture(false);
3440
}
3541

3642
private String fixture(boolean formatted) {
3743
return "java/googlejavaformat/JavaCode" + (formatted ? "F" : "Unf") + "ormatted.test";
3844
}
3945

40-
private void integration(String patterns, boolean firstFormatted, boolean secondFormatted, boolean thirdFormatted)
41-
throws IOException {
46+
private void createBuildScript() throws IOException {
47+
createBuildScript(false);
48+
}
49+
50+
private void createBuildScript(boolean isKotlin) throws IOException {
51+
if (isKotlin) {
52+
setFile("build.gradle.kts").toLines(
53+
"import com.diffplug.gradle.spotless.SpotlessExtension",
54+
"buildscript {",
55+
" repositories {",
56+
" mavenCentral()",
57+
" }",
58+
" dependencies {",
59+
" classpath(\"com.diffplug.spotless:spotless-plugin-gradle:3.27.1\")",
60+
" }",
61+
"}",
62+
"plugins {",
63+
" java",
64+
" id(\"com.diffplug.gradle.spotless\")",
65+
"}",
66+
"configure<SpotlessExtension> {",
67+
" java {",
68+
" googleJavaFormat(\"1.2\")",
69+
" }",
70+
"}");
71+
return;
72+
}
4273

4374
setFile("build.gradle").toLines(
4475
"buildscript { repositories { mavenCentral() } }",
@@ -51,32 +82,77 @@ private void integration(String patterns, boolean firstFormatted, boolean second
5182
" googleJavaFormat('1.2')",
5283
" }",
5384
"}");
85+
}
5486

55-
setFile(testFile(1)).toResource(fixture(false));
56-
setFile(testFile(2)).toResource(fixture(false));
57-
setFile(testFile(3)).toResource(fixture(false));
87+
private void integration(String patterns,
88+
boolean firstFormatted, boolean secondFormatted, boolean thirdFormatted) throws IOException {
89+
integration(patterns, firstFormatted, secondFormatted, thirdFormatted, false);
90+
}
91+
92+
private void integration(String patterns,
93+
boolean firstFormatted, boolean secondFormatted, boolean thirdFormatted,
94+
boolean isKotlin) throws IOException {
95+
String testFileOne = testFilePath(1, false);
96+
String testFileTwo = testFilePath(2, false);
97+
String testFileThree = testFilePath(3, false);
5898

59-
gradleRunner()
60-
.withArguments("spotlessApply", "-PspotlessFiles=" + patterns)
61-
.build();
99+
setFile(testFileOne).toResource(fixture());
100+
setFile(testFileTwo).toResource(fixture());
101+
setFile(testFileThree).toResource(fixture());
62102

63-
assertFile(testFile(1)).sameAsResource(fixture(firstFormatted));
64-
assertFile(testFile(2)).sameAsResource(fixture(secondFormatted));
65-
assertFile(testFile(3)).sameAsResource(fixture(thirdFormatted));
103+
GradleRunner runner = gradleRunner()
104+
.withArguments("spotlessApply", "-PspotlessFiles=" + patterns);
105+
if (isKotlin) {
106+
runner.withGradleVersion("4.0");
107+
}
108+
runner.build();
109+
110+
assertFile(testFileOne).sameAsResource(fixture(firstFormatted));
111+
assertFile(testFileTwo).sameAsResource(fixture(secondFormatted));
112+
assertFile(testFileThree).sameAsResource(fixture(thirdFormatted));
66113
}
67114

68115
@Test
69116
public void singleFile() throws IOException {
70-
integration(testFile(2, true), false, true, false);
117+
createBuildScript(false);
118+
integration(testFilePath(2), false, true, false);
71119
}
72120

73121
@Test
74122
public void multiFile() throws IOException {
75-
integration(testFile(1, true) + "," + testFile(3, true), true, false, true);
123+
createBuildScript();
124+
integration(testFilePath(1) + "," + testFilePath(3),
125+
true, false, true);
126+
}
127+
128+
@Test
129+
@Ignore("When spotlessFiles is specified without a value, Spotless runs on all files. It should run on none.")
130+
public void emptyPattern_formatsNoFiles() throws IOException {
131+
createBuildScript();
132+
integration("", false, false, false);
133+
}
134+
135+
@Test
136+
public void matchesNoFiles_formatsNoFilesButDoesNotExitInError() throws IOException {
137+
createBuildScript();
138+
integration(testFilePath(4), false, false, false);
76139
}
77140

78141
@Test
79142
public void regexp() throws IOException {
143+
createBuildScript();
80144
integration(".*/src/main/java/test(1|3).java", true, false, true);
81145
}
146+
147+
@Test(expected = UnexpectedBuildFailure.class)
148+
public void invalidRegexp_exitsInError() throws IOException {
149+
createBuildScript(false);
150+
integration("./[?)!\\", false, false, false);
151+
}
152+
153+
@Test
154+
public void kotlinBuildScript() throws IOException {
155+
createBuildScript(true);
156+
integration(testFilePath(2), false, true, false, true);
157+
}
82158
}

0 commit comments

Comments
 (0)