Skip to content

Commit 3a20930

Browse files
committed
1480: add integration test for gradle side
1 parent c2c46bb commit 3a20930

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile, String de
522522
}
523523

524524
public abstract static class NpmStepConfig<T extends NpmStepConfig<?>> {
525+
526+
public static final String SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME = "spotless-npm-install-cache";
527+
525528
@Nullable
526529
protected Object npmFile;
527530

@@ -570,7 +573,7 @@ public T npmInstallCache(final Object npmInstallCache) {
570573
}
571574

572575
public T npmInstallCache() {
573-
this.npmInstallCache = new File(project.getBuildDir(), "spotless-npm-install-cache");
576+
this.npmInstallCache = new File(project.getBuildDir(), SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME);
574577
replaceStep();
575578
return (T) this;
576579
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 com.diffplug.gradle.spotless.FormatExtension.NpmStepConfig.SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.nio.file.Paths;
23+
24+
import org.assertj.core.api.Assertions;
25+
import org.gradle.testkit.runner.BuildResult;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Order;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.io.TempDir;
30+
31+
import com.diffplug.common.base.Errors;
32+
import com.diffplug.spotless.tag.NpmTest;
33+
34+
@NpmTest
35+
class NpmInstallCacheIntegrationTests extends GradleIntegrationHarness {
36+
37+
static File pertainingCacheDir;
38+
39+
private static final File DEFAULT_DIR_FOR_NPM_INSTALL_CACHE_DO_NEVER_WRITE_TO_THIS = new File(".");
40+
41+
@BeforeAll
42+
static void beforeAll(@TempDir File pertainingCacheDir) {
43+
NpmInstallCacheIntegrationTests.pertainingCacheDir = Errors.rethrow().get(pertainingCacheDir::getCanonicalFile);
44+
}
45+
46+
@Test
47+
void prettierWithDefaultInstallCacheTest() throws IOException {
48+
File dir1 = newFolder("npm-prettier-1");
49+
File cacheDir = DEFAULT_DIR_FOR_NPM_INSTALL_CACHE_DO_NEVER_WRITE_TO_THIS;
50+
BuildResult result = runPhpPrettierOnDir(dir1, cacheDir);
51+
Assertions.assertThat(result.getOutput())
52+
.doesNotContain("Using cached node_modules for")
53+
.contains("Caching node_modules for ")
54+
.contains(Paths.get(dir1.getAbsolutePath(), "build", SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME).toString());
55+
56+
}
57+
58+
@Test
59+
void prettierWithSpecificInstallCacheTest() throws IOException {
60+
File dir1 = newFolder("npm-prettier-1");
61+
File cacheDir = newFolder("npm-prettier-cache");
62+
BuildResult result = runPhpPrettierOnDir(dir1, cacheDir);
63+
Assertions.assertThat(result.getOutput()).doesNotContainPattern("Using cached node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E");
64+
File dir2 = newFolder("npm-prettier-2");
65+
BuildResult result2 = runPhpPrettierOnDir(dir2, cacheDir);
66+
Assertions.assertThat(result2.getOutput()).containsPattern("Using cached node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E");
67+
}
68+
69+
@Test
70+
@Order(1)
71+
void prettierWithSpecificGlobalInstallCacheTest() throws IOException {
72+
File dir1 = newFolder("npm-prettier-global-1");
73+
File cacheDir = pertainingCacheDir;
74+
BuildResult result = runPhpPrettierOnDir(dir1, cacheDir);
75+
Assertions.assertThat(result.getOutput())
76+
.doesNotContainPattern("Using cached node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E")
77+
.containsPattern("Caching node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E");
78+
}
79+
80+
@Test
81+
@Order(2)
82+
void prettierWithSpecificGlobalInstallCacheTest2() throws IOException {
83+
File dir2 = newFolder("npm-prettier-global-2");
84+
File cacheDir = pertainingCacheDir;
85+
BuildResult result = runPhpPrettierOnDir(dir2, cacheDir);
86+
Assertions.assertThat(result.getOutput())
87+
.containsPattern("Using cached node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E")
88+
.doesNotContainPattern("Caching node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E");
89+
}
90+
91+
private BuildResult runPhpPrettierOnDir(File projDir, File cacheDir) throws IOException {
92+
String baseDir = projDir.getName();
93+
String cacheDirEnabled = cacheDirEnabledStringForCacheDir(cacheDir);
94+
setFile(baseDir + "/build.gradle").toLines(
95+
"plugins {",
96+
" id 'com.diffplug.spotless'",
97+
"}",
98+
"repositories { mavenCentral() }",
99+
"def prettierConfig = [:]",
100+
"prettierConfig['tabWidth'] = 3",
101+
"prettierConfig['parser'] = 'php'",
102+
"def prettierPackages = [:]",
103+
"prettierPackages['prettier'] = '2.0.5'",
104+
"prettierPackages['@prettier/plugin-php'] = '0.14.2'",
105+
"spotless {",
106+
" format 'php', {",
107+
" target 'php-example.php'",
108+
" prettier(prettierPackages).config(prettierConfig)" + cacheDirEnabled,
109+
" }",
110+
"}");
111+
setFile(baseDir + "/php-example.php").toResource("npm/prettier/plugins/php.dirty");
112+
final BuildResult spotlessApply = gradleRunner().withProjectDir(projDir).withArguments("--stacktrace", "--info", "spotlessApply").build();
113+
Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL");
114+
assertFile(baseDir + "/php-example.php").sameAsResource("npm/prettier/plugins/php.clean");
115+
return spotlessApply;
116+
}
117+
118+
private static String cacheDirEnabledStringForCacheDir(File cacheDir) {
119+
String cacheDirEnabled;
120+
if (cacheDir == null) {
121+
cacheDirEnabled = "";
122+
} else if (cacheDir == DEFAULT_DIR_FOR_NPM_INSTALL_CACHE_DO_NEVER_WRITE_TO_THIS) {
123+
cacheDirEnabled = ".npmInstallCache()";
124+
} else {
125+
cacheDirEnabled = ".npmInstallCache('" + cacheDir.getAbsolutePath() + "')";
126+
}
127+
return cacheDirEnabled;
128+
}
129+
}

0 commit comments

Comments
 (0)