Skip to content

Commit 77d302e

Browse files
committed
Add tests for Gradle plugin
1 parent 97198cb commit 77d302e

File tree

3 files changed

+373
-14
lines changed

3 files changed

+373
-14
lines changed

plugin-gradle/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies {
2525
testImplementation "org.junit.jupiter:junit-jupiter:${VER_JUNIT}"
2626
testImplementation "org.assertj:assertj-core:${VER_ASSERTJ}"
2727
testImplementation "com.diffplug.durian:durian-testlib:${VER_DURIAN}"
28+
testImplementation 'org.owasp.encoder:encoder:1.2.3'
2829
}
2930

3031
apply from: rootProject.file('gradle/special-tests.gradle')
Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
/*
2+
* Copyright 2023 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.gradle.spotless;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.io.IOException;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.owasp.encoder.Encode;
26+
27+
class RomeIntegrationTest extends GradleIntegrationHarness {
28+
/**
29+
* Tests that rome can be used as a generic formatting step.
30+
*
31+
* @throws Exception When a test failure occurs.
32+
*/
33+
@Test
34+
void asGenericStep() throws IOException {
35+
setFile("build.gradle").toLines(
36+
"plugins {",
37+
" id 'com.diffplug.spotless'",
38+
"}",
39+
"repositories { mavenCentral() }",
40+
"spotless {",
41+
" format 'myrome', {",
42+
" target '**/*.js'",
43+
" rome('12.0.0')",
44+
" }",
45+
"}");
46+
setFile("rome_test.js").toResource("rome/js/fileBefore.js");
47+
48+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
49+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
50+
assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js");
51+
}
52+
53+
/**
54+
* Tests that rome can be used as a JavaScript formatting step.
55+
*
56+
* @throws Exception When a test failure occurs.
57+
*/
58+
@Test
59+
void asJavaScriptStep() throws Exception {
60+
setFile("build.gradle").toLines(
61+
"plugins {",
62+
" id 'com.diffplug.spotless'",
63+
"}",
64+
"repositories { mavenCentral() }",
65+
"spotless {",
66+
" javascript {",
67+
" target '**/*.js'",
68+
" rome('12.0.0')",
69+
" }",
70+
"}");
71+
setFile("rome_test.js").toResource("rome/js/fileBefore.js");
72+
73+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
74+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
75+
assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js");
76+
}
77+
78+
/**
79+
* Tests that rome can be used as a JSON formatting step.
80+
*
81+
* @throws Exception When a test failure occurs.
82+
*/
83+
@Test
84+
void asJsonStep() throws Exception {
85+
setFile("build.gradle").toLines(
86+
"plugins {",
87+
" id 'com.diffplug.spotless'",
88+
"}",
89+
"repositories { mavenCentral() }",
90+
"spotless {",
91+
" json {",
92+
" target '**/*.json'",
93+
" rome('12.0.0')",
94+
" }",
95+
"}");
96+
setFile("rome_test.json").toResource("rome/json/fileBefore.json");
97+
98+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
99+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
100+
assertFile("rome_test.json").sameAsResource("rome/json/fileAfter.json");
101+
}
102+
103+
/**
104+
* Tests that rome can be used as a TypeScript formatting step.
105+
*
106+
* @throws Exception When a test failure occurs.
107+
*/
108+
@Test
109+
void asTypeScriptStep() throws Exception {
110+
setFile("build.gradle").toLines(
111+
"plugins {",
112+
" id 'com.diffplug.spotless'",
113+
"}",
114+
"repositories { mavenCentral() }",
115+
"spotless {",
116+
" typescript {",
117+
" target '**/*.ts'",
118+
" rome('12.0.0')",
119+
" }",
120+
"}");
121+
setFile("rome_test.ts").toResource("rome/ts/fileBefore.ts");
122+
123+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
124+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
125+
assertFile("rome_test.ts").sameAsResource("rome/ts/fileAfter.ts");
126+
}
127+
128+
/**
129+
* Tests that the language can be specified for the generic format step.
130+
*
131+
* @throws Exception When a test failure occurs.
132+
*/
133+
@Test
134+
void canSetLanguageForGenericStep() throws Exception {
135+
setFile("build.gradle").toLines(
136+
"plugins {",
137+
" id 'com.diffplug.spotless'",
138+
"}",
139+
"repositories { mavenCentral() }",
140+
"spotless {",
141+
" format 'myrome', {",
142+
" target '**/*.nosj'",
143+
" rome('12.0.0').language('json')",
144+
" }",
145+
"}");
146+
setFile("rome_test.nosj").toResource("rome/json/fileBefore.json");
147+
148+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
149+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
150+
assertFile("rome_test.nosj").sameAsResource("rome/json/fileAfter.json");
151+
}
152+
153+
/**
154+
* Tests that an absolute config path can be specified.
155+
*
156+
* @throws Exception When a test failure occurs.
157+
*/
158+
@Test
159+
void configPathAbsolute() throws Exception {
160+
var path = newFile("configs").getAbsolutePath();
161+
setFile("build.gradle").toLines(
162+
"plugins {",
163+
" id 'com.diffplug.spotless'",
164+
"}",
165+
"repositories { mavenCentral() }",
166+
"spotless {",
167+
" format 'myrome', {",
168+
" target '**/*.js'",
169+
" rome('12.0.0').configPath('" + Encode.forJava(path) + "')",
170+
" }",
171+
"}");
172+
setFile("rome_test.js").toResource("rome/js/longLineBefore.js");
173+
setFile("configs/rome.json").toResource("rome/config/line-width-120.json");
174+
175+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
176+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
177+
assertFile("rome_test.js").sameAsResource("rome/js/longLineAfter120.js");
178+
}
179+
180+
/**
181+
* Tests that a path to the directory with the rome.json config file can be
182+
* specified. Uses a config file with a line width of 120.
183+
*
184+
* @throws Exception When a test failure occurs.
185+
*/
186+
@Test
187+
void configPathLineWidth120() throws Exception {
188+
setFile("build.gradle").toLines(
189+
"plugins {",
190+
" id 'com.diffplug.spotless'",
191+
"}",
192+
"repositories { mavenCentral() }",
193+
"spotless {",
194+
" format 'myrome', {",
195+
" target '**/*.js'",
196+
" rome('12.0.0').configPath('configs')",
197+
" }",
198+
"}");
199+
setFile("rome_test.js").toResource("rome/js/longLineBefore.js");
200+
setFile("configs/rome.json").toResource("rome/config/line-width-120.json");
201+
202+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
203+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
204+
assertFile("rome_test.js").sameAsResource("rome/js/longLineAfter120.js");
205+
}
206+
207+
/**
208+
* Tests that a path to the directory with the rome.json config file can be
209+
* specified. Uses a config file with a line width of 80.
210+
*
211+
* @throws Exception When a test failure occurs.
212+
*/
213+
@Test
214+
void configPathLineWidth80() throws Exception {
215+
setFile("build.gradle").toLines(
216+
"plugins {",
217+
" id 'com.diffplug.spotless'",
218+
"}",
219+
"repositories { mavenCentral() }",
220+
"spotless {",
221+
" format 'myrome', {",
222+
" target '**/*.js'",
223+
" rome('12.0.0').configPath('configs')",
224+
" }",
225+
"}");
226+
setFile("rome_test.js").toResource("rome/js/longLineBefore.js");
227+
setFile("configs/rome.json").toResource("rome/config/line-width-80.json");
228+
229+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
230+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
231+
assertFile("rome_test.js").sameAsResource("rome/js/longLineAfter80.js");
232+
}
233+
234+
/**
235+
* Tests that the download directory can be an absolute path.
236+
*
237+
* @throws Exception When a test failure occurs.
238+
*/
239+
@Test
240+
void downloadDirAbsolute() throws Exception {
241+
var path = newFile("target/bin/rome").getAbsoluteFile().toString();
242+
setFile("build.gradle").toLines(
243+
"plugins {",
244+
" id 'com.diffplug.spotless'",
245+
"}",
246+
"repositories { mavenCentral() }",
247+
"spotless {",
248+
" format 'myrome', {",
249+
" target '**/*.js'",
250+
" rome('12.0.0').downloadDir('" + Encode.forJava(path) + "')",
251+
" }",
252+
"}");
253+
setFile("rome_test.js").toResource("rome/js/fileBefore.js");
254+
assertTrue(!newFile("target/bin/rome").exists() || newFile("target/bin/rome").list().length == 0);
255+
256+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
257+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
258+
assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js");
259+
assertEquals(2, newFile("target/bin/rome").list().length);
260+
}
261+
262+
/**
263+
* Tests that the download directory can be changed to a path relative to the
264+
* project's base directory.
265+
*
266+
* @throws Exception When a test failure occurs.
267+
*/
268+
@Test
269+
void downloadDirRelative() throws Exception {
270+
setFile("build.gradle").toLines(
271+
"plugins {",
272+
" id 'com.diffplug.spotless'",
273+
"}",
274+
"repositories { mavenCentral() }",
275+
"spotless {",
276+
" format 'myrome', {",
277+
" target '**/*.js'",
278+
" rome('12.0.0').downloadDir('target/bin/rome')",
279+
" }",
280+
"}");
281+
setFile("rome_test.js").toResource("rome/js/fileBefore.js");
282+
assertTrue(!newFile("target/bin/rome").exists() || newFile("target/bin/rome").list().length == 0);
283+
284+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build();
285+
assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
286+
assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js");
287+
assertEquals(2, newFile("target/bin/rome").list().length);
288+
}
289+
290+
/**
291+
* Tests that the build fails when given Rome executable does not exist.
292+
*
293+
* @throws Exception When a test failure occurs.
294+
*/
295+
@Test
296+
void failureWhenExeNotFound() throws Exception {
297+
setFile("build.gradle").toLines(
298+
"plugins {",
299+
" id 'com.diffplug.spotless'",
300+
"}",
301+
"repositories { mavenCentral() }",
302+
"spotless {",
303+
" format 'myrome', {",
304+
" target '**/*.js'",
305+
" rome('12.0.0').pathToExe('rome/is/missing')",
306+
" }",
307+
"}");
308+
setFile("rome_test.js").toResource("rome/js/fileBefore.js");
309+
310+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail();
311+
assertThat(spotlessApply.getOutput()).contains("Build failed with an exception");
312+
assertFile("rome_test.js").sameAsResource("rome/js/fileBefore.js");
313+
assertThat(spotlessApply.getOutput()).contains("Could not create task ':spotlessMyromeApply'");
314+
assertThat(spotlessApply.getOutput()).contains("Rome executable does not exist");
315+
}
316+
317+
/**
318+
* Tests that the build fails when the input file could not be parsed.
319+
*
320+
* @throws Exception When a test failure occurs.
321+
*/
322+
@Test
323+
void failureWhenNotParseable() throws Exception {
324+
setFile("build.gradle").toLines(
325+
"plugins {",
326+
" id 'com.diffplug.spotless'",
327+
"}",
328+
"repositories { mavenCentral() }",
329+
"spotless {",
330+
" format 'myrome', {",
331+
" target '**/*.js'",
332+
" rome('12.0.0').language('json')",
333+
" }",
334+
"}");
335+
setFile("rome_test.js").toResource("rome/js/fileBefore.js");
336+
337+
var spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail();
338+
assertThat(spotlessApply.getOutput()).contains("spotlessMyrome FAILED");
339+
assertFile("rome_test.js").sameAsResource("rome/js/fileBefore.js");
340+
assertThat(spotlessApply.getOutput()).contains("Format with errors is disabled.");
341+
assertThat(spotlessApply.getOutput()).contains("Step 'rome' found problem in 'rome_test.js'");
342+
}
343+
}

0 commit comments

Comments
 (0)