Skip to content

Commit 3c4b5bc

Browse files
committed
1480: add integration test for tsfmt (and fix api issue)
1 parent 3a20930 commit 3c4b5bc

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,33 @@ public class TypescriptFormatExtension extends NpmStepConfig<TypescriptFormatExt
8282
this.devDependencies = Objects.requireNonNull(devDependencies);
8383
}
8484

85-
public void config(final Map<String, Object> config) {
85+
public TypescriptFormatExtension config(final Map<String, Object> config) {
8686
this.config = new TreeMap<>(requireNonNull(config));
8787
replaceStep();
88+
return this;
8889
}
8990

90-
public void tsconfigFile(final Object path) {
91-
configFile(TsConfigFileType.TSCONFIG, path);
91+
public TypescriptFormatExtension tsconfigFile(final Object path) {
92+
return configFile(TsConfigFileType.TSCONFIG, path);
9293
}
9394

94-
public void tslintFile(final Object path) {
95-
configFile(TsConfigFileType.TSLINT, path);
95+
public TypescriptFormatExtension tslintFile(final Object path) {
96+
return configFile(TsConfigFileType.TSLINT, path);
9697
}
9798

98-
public void vscodeFile(final Object path) {
99-
configFile(TsConfigFileType.VSCODE, path);
99+
public TypescriptFormatExtension vscodeFile(final Object path) {
100+
return configFile(TsConfigFileType.VSCODE, path);
100101
}
101102

102-
public void tsfmtFile(final Object path) {
103-
configFile(TsConfigFileType.TSFMT, path);
103+
public TypescriptFormatExtension tsfmtFile(final Object path) {
104+
return configFile(TsConfigFileType.TSFMT, path);
104105
}
105106

106-
private void configFile(TsConfigFileType filetype, Object path) {
107+
private TypescriptFormatExtension configFile(TsConfigFileType filetype, Object path) {
107108
this.configFileType = requireNonNull(filetype);
108109
this.configFilePath = requireNonNull(path);
109110
replaceStep();
111+
return this;
110112
}
111113

112114
public FormatterStep createStep() {

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
import org.assertj.core.api.Assertions;
2525
import org.gradle.testkit.runner.BuildResult;
2626
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
2728
import org.junit.jupiter.api.Order;
2829
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.TestMethodOrder;
2931
import org.junit.jupiter.api.io.TempDir;
3032

3133
import com.diffplug.common.base.Errors;
3234
import com.diffplug.spotless.tag.NpmTest;
3335

36+
@TestMethodOrder(OrderAnnotation.class)
3437
@NpmTest
3538
class NpmInstallCacheIntegrationTests extends GradleIntegrationHarness {
3639

@@ -115,6 +118,51 @@ private BuildResult runPhpPrettierOnDir(File projDir, File cacheDir) throws IOEx
115118
return spotlessApply;
116119
}
117120

121+
@Test
122+
@Order(3)
123+
void tsfmtWithSpecificGlobalInstallCacheTest() throws IOException {
124+
File dir1 = newFolder("npm-tsfmt-global-1");
125+
File cacheDir = pertainingCacheDir;
126+
BuildResult result = runTsfmtOnDir(dir1, cacheDir);
127+
Assertions.assertThat(result.getOutput())
128+
.doesNotContainPattern("Using cached node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E")
129+
.containsPattern("Caching node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E");
130+
}
131+
132+
@Test
133+
@Order(4)
134+
void tsfmtWithSpecificGlobalInstallCacheTest2() throws IOException {
135+
File dir2 = newFolder("npm-tsfmt-global-2");
136+
File cacheDir = pertainingCacheDir;
137+
BuildResult result = runTsfmtOnDir(dir2, cacheDir);
138+
Assertions.assertThat(result.getOutput())
139+
.containsPattern("Using cached node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E")
140+
.doesNotContainPattern("Caching node_modules for .*\\Q" + cacheDir.getAbsolutePath() + "\\E");
141+
}
142+
143+
private BuildResult runTsfmtOnDir(File projDir, File cacheDir) throws IOException {
144+
String baseDir = projDir.getName();
145+
String cacheDirEnabled = cacheDirEnabledStringForCacheDir(cacheDir);
146+
setFile(baseDir + "/build.gradle").toLines(
147+
"plugins {",
148+
" id 'com.diffplug.spotless'",
149+
"}",
150+
"repositories { mavenCentral() }",
151+
"def tsfmtconfig = [:]",
152+
"tsfmtconfig['indentSize'] = 1",
153+
"tsfmtconfig['convertTabsToSpaces'] = true",
154+
"spotless {",
155+
" typescript {",
156+
" target 'test.ts'",
157+
" tsfmt().config(tsfmtconfig)" + cacheDirEnabled,
158+
" }",
159+
"}");
160+
setFile(baseDir + "/test.ts").toResource("npm/tsfmt/tsfmt/tsfmt.dirty");
161+
final BuildResult spotlessApply = gradleRunner().withProjectDir(projDir).withArguments("--stacktrace", "--info", "spotlessApply").build();
162+
assertFile(baseDir + "/test.ts").sameAsResource("npm/tsfmt/tsfmt/tsfmt.clean");
163+
return spotlessApply;
164+
}
165+
118166
private static String cacheDirEnabledStringForCacheDir(File cacheDir) {
119167
String cacheDirEnabled;
120168
if (cacheDir == null) {

0 commit comments

Comments
 (0)