Skip to content

Commit cd84167

Browse files
committed
1480: integration tests for maven + cleanup
1 parent de33c70 commit cd84167

File tree

6 files changed

+203
-20
lines changed

6 files changed

+203
-20
lines changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/npm/AbstractNpmFormatterStepFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.diffplug.spotless.maven.npm;
1717

1818
import java.io.File;
19+
import java.nio.file.Paths;
1920
import java.util.AbstractMap;
2021
import java.util.Arrays;
2122
import java.util.Collections;
@@ -33,6 +34,8 @@
3334

3435
public abstract class AbstractNpmFormatterStepFactory implements FormatterStepFactory {
3536

37+
public static final String SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME = "spotless-npm-install-cache";
38+
3639
@Parameter
3740
private String npmExecutable;
3841

@@ -69,9 +72,9 @@ protected File cacheDir(FormatterStepConfig stepConfig) {
6972
return null;
7073
}
7174
if ("true".equals(this.npmInstallCache.toLowerCase(Locale.ROOT))) {
72-
return new File(buildDir(stepConfig), "spotless-npm-install-cache");
75+
return new File(buildDir(stepConfig), SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME);
7376
}
74-
return stepConfig.getFileLocator().locateFile(this.npmInstallCache);
77+
return Paths.get(this.npmInstallCache).toFile();
7578
}
7679

7780
protected File baseDir(FormatterStepConfig stepConfig) {
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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.spotless.maven.npm;
17+
18+
import static com.diffplug.spotless.maven.npm.AbstractNpmFormatterStepFactory.SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.file.FileVisitResult;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
import java.nio.file.SimpleFileVisitor;
27+
import java.nio.file.attribute.BasicFileAttributes;
28+
29+
import org.assertj.core.api.Assertions;
30+
import org.junit.jupiter.api.Test;
31+
32+
import com.diffplug.spotless.ProcessRunner.Result;
33+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
34+
import com.diffplug.spotless.tag.NpmTest;
35+
36+
@NpmTest
37+
public class NpmStepsWithNpmInstallCacheTest extends MavenIntegrationHarness {
38+
39+
// TODO implement tests without cache and with various cache paths
40+
// using only prettier is enough since the other cases are covered by gradle-side integration tests
41+
42+
@Test
43+
void prettierTypescriptWithoutCache() throws Exception {
44+
String suffix = "ts";
45+
writePomWithPrettierSteps("**/*." + suffix,
46+
"<prettier>",
47+
" <prettierVersion>1.16.4</prettierVersion>",
48+
" <configFile>.prettierrc.yml</configFile>",
49+
"</prettier>");
50+
Result result = run("typescript", suffix);
51+
Assertions.assertThat(result.stdOutUtf8()).doesNotContain("Caching node_modules for").doesNotContain("Using cached node_modules for");
52+
}
53+
54+
@Test
55+
void prettierTypescriptWithDefaultCache() throws Exception {
56+
String suffix = "ts";
57+
writePomWithPrettierSteps("**/*." + suffix,
58+
"<prettier>",
59+
" <prettierVersion>1.16.4</prettierVersion>",
60+
" <configFile>.prettierrc.yml</configFile>",
61+
" <npmInstallCache>true</npmInstallCache>",
62+
"</prettier>");
63+
Result result = run("typescript", suffix);
64+
Assertions.assertThat(result.stdOutUtf8())
65+
.contains("Caching node_modules for")
66+
.contains(SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME)
67+
.doesNotContain("Using cached node_modules for");
68+
}
69+
70+
@Test
71+
void prettierTypescriptWithDefaultCacheIsReusedOnSecondRun() throws Exception {
72+
String suffix = "ts";
73+
writePomWithPrettierSteps("**/*." + suffix,
74+
"<prettier>",
75+
" <prettierVersion>1.16.4</prettierVersion>",
76+
" <configFile>.prettierrc.yml</configFile>",
77+
" <npmInstallCache>true</npmInstallCache>",
78+
"</prettier>");
79+
Result result1 = run("typescript", suffix);
80+
Assertions.assertThat(result1.stdOutUtf8())
81+
.contains("Caching node_modules for")
82+
.contains(SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME)
83+
.doesNotContain("Using cached node_modules for");
84+
85+
// recursively delete target folder to simulate a fresh run (except the default cache folder)
86+
recursiveDelete(Paths.get(rootFolder().getAbsolutePath(), "target"), SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME);
87+
88+
Result result2 = run("typescript", suffix);
89+
Assertions.assertThat(result2.stdOutUtf8())
90+
.doesNotContain("Caching node_modules for")
91+
.contains(SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME)
92+
.contains("Using cached node_modules for");
93+
}
94+
95+
@Test
96+
void prettierTypescriptWithSpecificCache() throws Exception {
97+
String suffix = "ts";
98+
File cacheDir = newFolder("cache-prettier-1");
99+
writePomWithPrettierSteps("**/*." + suffix,
100+
"<prettier>",
101+
" <prettierVersion>1.16.4</prettierVersion>",
102+
" <configFile>.prettierrc.yml</configFile>",
103+
" <npmInstallCache>" + cacheDir.getAbsolutePath() + "</npmInstallCache>",
104+
"</prettier>");
105+
Result result = run("typescript", suffix);
106+
Assertions.assertThat(result.stdOutUtf8())
107+
.contains("Caching node_modules for")
108+
.contains(Path.of(cacheDir.getAbsolutePath()).toAbsolutePath().toString())
109+
.doesNotContain("Using cached node_modules for");
110+
}
111+
112+
@Test
113+
void prettierTypescriptWithSpecificCacheIsUsedOnSecondRun() throws Exception {
114+
String suffix = "ts";
115+
File cacheDir = newFolder("cache-prettier-1");
116+
writePomWithPrettierSteps("**/*." + suffix,
117+
"<prettier>",
118+
" <prettierVersion>1.16.4</prettierVersion>",
119+
" <configFile>.prettierrc.yml</configFile>",
120+
" <npmInstallCache>" + cacheDir.getAbsolutePath() + "</npmInstallCache>",
121+
"</prettier>");
122+
Result result1 = run("typescript", suffix);
123+
Assertions.assertThat(result1.stdOutUtf8())
124+
.contains("Caching node_modules for")
125+
.contains(Path.of(cacheDir.getAbsolutePath()).toAbsolutePath().toString())
126+
.doesNotContain("Using cached node_modules for");
127+
128+
// recursively delete target folder to simulate a fresh run
129+
recursiveDelete(Paths.get(rootFolder().getAbsolutePath(), "target"), null);
130+
131+
Result result2 = run("typescript", suffix);
132+
Assertions.assertThat(result2.stdOutUtf8())
133+
.doesNotContain("Caching node_modules for")
134+
.contains(Path.of(cacheDir.getAbsolutePath()).toAbsolutePath().toString())
135+
.contains("Using cached node_modules for");
136+
}
137+
138+
private void recursiveDelete(Path path, String exclusion) throws IOException {
139+
Files.walkFileTree(path, new RecursiveDelete(exclusion));
140+
}
141+
142+
private Result run(String kind, String suffix) throws IOException, InterruptedException {
143+
String path = prepareRun(kind, suffix);
144+
Result result = mavenRunner().withArguments("spotless:apply").runNoError();
145+
assertFile(path).sameAsResource("npm/prettier/filetypes/" + kind + "/" + kind + ".clean");
146+
return result;
147+
}
148+
149+
private String prepareRun(String kind, String suffix) throws IOException {
150+
String configPath = ".prettierrc.yml";
151+
setFile(configPath).toResource("npm/prettier/filetypes/" + kind + "/" + ".prettierrc.yml");
152+
String path = "src/main/" + kind + "/test." + suffix;
153+
setFile(path).toResource("npm/prettier/filetypes/" + kind + "/" + kind + ".dirty");
154+
return path;
155+
}
156+
157+
private static class RecursiveDelete extends SimpleFileVisitor<Path> {
158+
private final String exclusionDirectory;
159+
160+
public RecursiveDelete(String exclusionDirectory) {
161+
this.exclusionDirectory = exclusionDirectory;
162+
}
163+
164+
@Override
165+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
166+
if (exclusionDirectory != null && dir.toFile().getName().equals(exclusionDirectory)) {
167+
return FileVisitResult.SKIP_SUBTREE;
168+
}
169+
return super.preVisitDirectory(dir, attrs);
170+
}
171+
172+
@Override
173+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
174+
Files.delete(file);
175+
return super.visitFile(file, attrs);
176+
}
177+
178+
@Override
179+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
180+
if (dir.toFile().listFiles().length != 0) {
181+
// skip non-empty dir
182+
return super.postVisitDirectory(dir, exc);
183+
}
184+
Files.delete(dir);
185+
return super.postVisitDirectory(dir, exc);
186+
}
187+
}
188+
}

testlib/src/test/java/com/diffplug/spotless/npm/EslintFormatterStepTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void formattingUsingRulesetsFile(String ruleSetName) throws Exception {
6464
TestProvisioner.mavenCentral(),
6565
projectDir(),
6666
buildDir(),
67-
cacheDir(),
67+
null,
6868
npmPathResolver(),
6969
new EslintConfig(eslintRc, null));
7070

@@ -108,7 +108,7 @@ void formattingUsingRulesetsFile(String ruleSetName) throws Exception {
108108
TestProvisioner.mavenCentral(),
109109
projectDir(),
110110
buildDir(),
111-
cacheDir(),
111+
null,
112112
npmPathResolver(),
113113
new EslintTypescriptConfig(eslintRc, null, tsconfigFile));
114114

@@ -166,7 +166,7 @@ void formattingUsingInlineXoConfig() throws Exception {
166166
TestProvisioner.mavenCentral(),
167167
projectDir(),
168168
buildDir(),
169-
cacheDir(),
169+
null,
170170
npmPathResolver(),
171171
new EslintTypescriptConfig(null, esLintConfig, tsconfigFile));
172172

testlib/src/test/java/com/diffplug/spotless/npm/NpmFormatterStepCommonTests.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,4 @@ protected File projectDir() throws IOException {
5757
return this.projectDir;
5858
}
5959

60-
private File cacheDir = null;
61-
62-
protected File cacheDir() throws IOException {
63-
if (this.cacheDir == null) {
64-
this.cacheDir = newFolder("cache-dir");
65-
}
66-
return this.cacheDir;
67-
}
6860
}

testlib/src/test/java/com/diffplug/spotless/npm/PrettierFormatterStepTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void formattingUsingConfigFile(String fileType) throws Exception {
5252
TestProvisioner.mavenCentral(),
5353
projectDir(),
5454
buildDir(),
55-
cacheDir(),
55+
null,
5656
npmPathResolver(),
5757
new PrettierConfig(prettierRc, null));
5858

@@ -78,7 +78,7 @@ void parserInferenceBasedOnExplicitFilepathIsWorking() throws Exception {
7878
TestProvisioner.mavenCentral(),
7979
projectDir(),
8080
buildDir(),
81-
cacheDir(),
81+
null,
8282
npmPathResolver(),
8383
new PrettierConfig(null, ImmutableMap.of("filepath", "anyname.json"))); // should select parser based on this name
8484

@@ -99,7 +99,7 @@ void parserInferenceBasedOnFilenameIsWorking() throws Exception {
9999
TestProvisioner.mavenCentral(),
100100
projectDir(),
101101
buildDir(),
102-
cacheDir(),
102+
null,
103103
npmPathResolver(),
104104
new PrettierConfig(null, Collections.emptyMap()));
105105

@@ -115,7 +115,7 @@ void verifyPrettierErrorMessageIsRelayed() throws Exception {
115115
TestProvisioner.mavenCentral(),
116116
projectDir(),
117117
buildDir(),
118-
cacheDir(),
118+
null,
119119
npmPathResolver(),
120120
new PrettierConfig(null, ImmutableMap.of("parser", "postcss")));
121121
try (StepHarnessWithFile stepHarness = StepHarnessWithFile.forStep(this, formatterStep)) {
@@ -141,7 +141,7 @@ void runFormatTest(PrettierConfig config, String cleanFileNameSuffix) throws Exc
141141
TestProvisioner.mavenCentral(),
142142
projectDir(),
143143
buildDir(),
144-
cacheDir(),
144+
null,
145145
npmPathResolver(),
146146
config); // should select parser based on this name
147147

testlib/src/test/java/com/diffplug/spotless/npm/TsFmtFormatterStepTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void formattingUsingConfigFile(String formattingConfigFile) throws Exception {
5959
TestProvisioner.mavenCentral(),
6060
projectDir(),
6161
buildDir(),
62-
cacheDir(),
62+
null,
6363
npmPathResolver(),
6464
TypedTsFmtConfigFile.named(configFileNameWithoutExtension, configFile),
6565
Collections.emptyMap());
@@ -83,7 +83,7 @@ void formattingUsingInlineConfigWorks() throws Exception {
8383
TestProvisioner.mavenCentral(),
8484
projectDir(),
8585
buildDir(),
86-
cacheDir(),
86+
null,
8787
npmPathResolver(),
8888
null,
8989
inlineConfig);

0 commit comments

Comments
 (0)