Skip to content

Commit 9ea39ff

Browse files
committed
chore: add more integration-tests for prettier options
1 parent c4c5822 commit 9ea39ff

File tree

3 files changed

+211
-1
lines changed

3 files changed

+211
-1
lines changed

app/src/test/java/com/diffplug/spotless/cli/steps/PrettierTest.java

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.cli.steps;
1717

18+
import java.io.File;
1819
import java.io.IOException;
1920

2021
import org.junit.jupiter.api.Test;
@@ -25,6 +26,8 @@
2526
import com.diffplug.spotless.tag.CliProcessNpmTest;
2627
import com.diffplug.spotless.tag.NpmTest;
2728

29+
import static org.assertj.core.api.Assertions.assertThat;
30+
2831
@NpmTest
2932
@CliProcessNpmTest
3033
@CliNativeNpmTest
@@ -57,4 +60,165 @@ void itRunsPrettierForTsFilesWithOptionFile() throws Exception {
5760

5861
selfie().expectResource("test.ts").toMatchDisk();
5962
}
63+
64+
@Test
65+
void itRunsPrettierWithoutAnyOptions() throws IOException {
66+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
67+
68+
SpotlessCLIRunner.Result result =
69+
cliRunner().withTargets("test.ts").withStep(Prettier.class).run();
70+
71+
selfie().expectResource("test.ts").toMatchDisk();
72+
}
73+
74+
@Test
75+
void itRunsSpecificPrettierVersion2x() throws IOException {
76+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
77+
78+
SpotlessCLIRunner.Result result = cliRunner()
79+
.withTargets("test.ts")
80+
.withStep(Prettier.class)
81+
.withOption("--dev-dependency", "prettier=2.8.7")
82+
.run();
83+
84+
selfie().expectResource("test.ts").toMatchDisk();
85+
}
86+
87+
@Test
88+
void itUsesACacheDir() throws IOException {
89+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
90+
91+
File cacheDir = newFolder("cachedir");
92+
SpotlessCLIRunner.Result result = cliRunner()
93+
.withTargets("test.ts")
94+
.withStep(Prettier.class)
95+
.withOption("--npm-install-cache-dir", cacheDir.getPath())
96+
.run();
97+
98+
selfie().expectResource("test.ts").toMatchDisk();
99+
100+
assertThat(cacheDir).isNotEmptyDirectory();
101+
}
102+
103+
@Test
104+
void itUsesNpmExec() {
105+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
106+
File npmOut = newFile("npmoutput.txt");
107+
File npm = setFile("npm")
108+
.toContent(
109+
"""
110+
#!/bin/sh
111+
echo "npm exec" > %s
112+
exit -99
113+
"""
114+
.formatted(npmOut.getAbsolutePath()))
115+
.makeExecutable()
116+
.getFile();
117+
118+
File nodeOut = newFile("nodeoutput.txt");
119+
setFile("node")
120+
.toContent(
121+
"""
122+
#!/bin/sh
123+
echo "node exec" > %s
124+
exit -100
125+
"""
126+
.formatted(nodeOut.getAbsolutePath()))
127+
.makeExecutable();
128+
129+
SpotlessCLIRunner.Result result = cliRunner()
130+
.withTargets("test.ts")
131+
.withStep(Prettier.class)
132+
.withOption("--npm-exec", npm.getPath())
133+
.runAndFail();
134+
135+
selfie().expectFile(npmOut).toBe("""
136+
npm exec
137+
""");
138+
selfie().expectFile(nodeOut).toBe("");
139+
}
140+
141+
@Test
142+
void itUsesNodeExec() {
143+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
144+
File npmOut = newFile("npmoutput.txt");
145+
setFile("npm")
146+
.toContent(
147+
"""
148+
#!/bin/sh
149+
echo "npm exec" > %s
150+
exit -99
151+
"""
152+
.formatted(npmOut.getAbsolutePath()))
153+
.makeExecutable();
154+
155+
File nodeOut = newFile("nodeoutput.txt");
156+
File node = setFile("node")
157+
.toContent(
158+
"""
159+
#!/bin/sh
160+
echo "node exec" > %s
161+
exit -100
162+
"""
163+
.formatted(nodeOut.getAbsolutePath()))
164+
.makeExecutable()
165+
.getFile();
166+
167+
SpotlessCLIRunner.Result result = cliRunner()
168+
.withTargets("test.ts")
169+
.withStep(Prettier.class)
170+
.withOption("--node-exec", node.getPath())
171+
.runAndFail();
172+
173+
selfie().expectFile(npmOut).toBe("""
174+
npm exec
175+
""");
176+
selfie().expectFile(nodeOut).toBe("");
177+
}
178+
179+
@Test
180+
void itUsesNpmrcFile() {
181+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
182+
183+
File npmrc = setFile(".custom_npmrc")
184+
.toLines(
185+
"registry=https://i.do.not.exist.com",
186+
"fetch-timeout=250",
187+
"fetch-retry-mintimeout=250",
188+
"fetch-retry-maxtimeout=250")
189+
.getFile();
190+
191+
SpotlessCLIRunner.Result result = cliRunner()
192+
.withTargets("test.ts")
193+
.withStep(Prettier.class)
194+
.withOption("--npmrc-file", npmrc.getPath())
195+
.runAndFail();
196+
197+
assertThat(result.exitCode()).isNotZero();
198+
assertThat(result.stdErr()).containsPattern(".*Running npm command.*NpmInstall.* failed with exit code: 1");
199+
}
200+
201+
@Test
202+
void itUsesNpmrcFileInAdditionalLocation() throws IOException {
203+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
204+
205+
File additionalLocation = newFolder("additionalLocation");
206+
207+
File npmrc = setFile(additionalLocation.getName() + "/.npmrc")
208+
.toLines(
209+
"registry=https://i.do.not.exist.com",
210+
"fetch-timeout=250",
211+
"fetch-retry-mintimeout=250",
212+
"fetch-retry-maxtimeout=250")
213+
.getFile();
214+
215+
SpotlessCLIRunner.Result result = cliRunner()
216+
.withTargets("test.ts")
217+
.withStep(Prettier.class)
218+
.withOption("--additional-npmrc-location", additionalLocation.getPath())
219+
.runAndFail();
220+
221+
assertThat(result.exitCode()).isNotZero();
222+
assertThat(result.stdErr()).containsPattern(".*Running npm command.*NpmInstall.* failed with exit code: 1");
223+
}
60224
}

app/src/test/resources/com/diffplug/spotless/cli/steps/PrettierTest.ss

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,46 @@ export class MyVeryOwnControllerWithARatherLongNameThatIsNotReallyNecessary
3838
//...
3939
}
4040

41+
╔═ itRunsPrettierWithoutAnyOptions ═╗
42+
export class MyVeryOwnControllerWithARatherLongNameThatIsNotReallyNecessary
43+
extends AbstractController
44+
implements DisposeAware, CallbackAware
45+
{
46+
public myValue: string[];
47+
48+
constructor(private myService: Service, name: string, private field: any) {
49+
super(name);
50+
}
51+
52+
//...
53+
}
54+
55+
╔═ itRunsSpecificPrettierVersion2x ═╗
56+
export class MyVeryOwnControllerWithARatherLongNameThatIsNotReallyNecessary
57+
extends AbstractController
58+
implements DisposeAware, CallbackAware
59+
{
60+
public myValue: string[];
61+
62+
constructor(private myService: Service, name: string, private field: any) {
63+
super(name);
64+
}
65+
66+
//...
67+
}
68+
69+
╔═ itUsesACacheDir ═╗
70+
export class MyVeryOwnControllerWithARatherLongNameThatIsNotReallyNecessary
71+
extends AbstractController
72+
implements DisposeAware, CallbackAware
73+
{
74+
public myValue: string[];
75+
76+
constructor(private myService: Service, name: string, private field: any) {
77+
super(name);
78+
}
79+
80+
//...
81+
}
82+
4183
╔═ [end of file] ═╗

testlib/src/main/java/com/diffplug/spotless/SelfieExpectations.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static SelfieExpectations create(@NotNull File rootFolder) {
4545
private static final Camera<Resource> RESOURCE_CAMERA = (Resource resource) -> {
4646
File file = new File(resource.rootFolder(), resource.resourcePath());
4747
if (!file.exists()) {
48-
throw new IllegalArgumentException("Resource not found: " + file);
48+
return Snapshot.of("");
4949
}
5050
return Snapshot.of(ThrowingEx.get(() -> Files.readString(file.toPath())));
5151
};
@@ -55,4 +55,8 @@ record Resource(File rootFolder, String resourcePath) {}
5555
public StringSelfie expectResource(String resourcePath) {
5656
return Selfie.expectSelfie(new Resource(rootFolder, resourcePath), RESOURCE_CAMERA);
5757
}
58+
59+
public StringSelfie expectFile(File file) {
60+
return Selfie.expectSelfie(new Resource(file.getParentFile(), file.getName()), RESOURCE_CAMERA);
61+
}
5862
}

0 commit comments

Comments
 (0)