21
21
22
22
import org .junit .jupiter .api .Test ;
23
23
24
+ import com .diffplug .spotless .ResourceHarness ;
24
25
import com .diffplug .spotless .maven .MavenIntegrationHarness ;
25
26
import com .diffplug .spotless .maven .MavenRunner .Result ;
26
27
import com .diffplug .spotless .tag .NpmTest ;
27
28
28
29
@ NpmTest
29
30
class TypescriptFormatStepTest extends MavenIntegrationHarness {
30
- private void run (String kind ) throws IOException , InterruptedException {
31
- String path = prepareRun (kind );
31
+
32
+ private static final String TEST_FILE_PATH = "src/main/typescript/test.ts" ;
33
+
34
+ private void runTsfmt (String kind ) throws IOException , InterruptedException {
35
+ String path = prepareRunTsfmt (kind );
32
36
mavenRunner ().withArguments ("spotless:apply" ).runNoError ();
33
37
assertFile (path ).sameAsResource ("npm/tsfmt/" + kind + "/" + kind + ".clean" );
34
38
}
35
39
36
- private String prepareRun (String kind ) throws IOException {
37
- String path = "src/main/typescript/test.ts" ;
38
- setFile (path ).toResource ("npm/tsfmt/" + kind + "/" + kind + ".dirty" );
39
- return path ;
40
+ private String prepareRunTsfmt (String kind ) throws IOException {
41
+ setFile (TEST_FILE_PATH ).toResource ("npm/tsfmt/" + kind + "/" + kind + ".dirty" );
42
+ return TEST_FILE_PATH ;
40
43
}
41
44
42
- private Result runExpectingError (String kind ) throws IOException , InterruptedException {
43
- prepareRun (kind );
45
+ private Result runExpectingErrorTsfmt (String kind ) throws IOException , InterruptedException {
46
+ prepareRunTsfmt (kind );
44
47
return mavenRunner ().withArguments ("spotless:apply" ).runHasError ();
45
48
}
46
49
47
50
@ Test
48
51
void tslint () throws Exception {
49
52
writePomWithTypescriptSteps (
53
+ TEST_FILE_PATH ,
50
54
"<tsfmt>" ,
51
55
" <tslintFile>${basedir}/tslint.json</tslintFile>" ,
52
56
"</tsfmt>" );
53
57
setFile ("tslint.json" ).toResource ("npm/tsfmt/tslint/tslint.json" );
54
- run ("tslint" );
58
+ runTsfmt ("tslint" );
55
59
}
56
60
57
61
@ Test
58
62
void vscode () throws Exception {
59
63
writePomWithTypescriptSteps (
64
+ TEST_FILE_PATH ,
60
65
"<tsfmt>" ,
61
66
" <vscodeFile>${basedir}/vscode.json</vscodeFile>" ,
62
67
"</tsfmt>" );
63
68
setFile ("vscode.json" ).toResource ("npm/tsfmt/vscode/vscode.json" );
64
- run ("vscode" );
69
+ runTsfmt ("vscode" );
65
70
}
66
71
67
72
@ Test
68
73
void tsfmt () throws Exception {
69
74
writePomWithTypescriptSteps (
75
+ TEST_FILE_PATH ,
70
76
"<tsfmt>" ,
71
77
" <tsfmtFile>${basedir}/tsfmt.json</tsfmtFile>" ,
72
78
"</tsfmt>" );
73
79
setFile ("tsfmt.json" ).toResource ("npm/tsfmt/tsfmt/tsfmt.json" );
74
- run ("tsfmt" );
80
+ runTsfmt ("tsfmt" );
75
81
}
76
82
77
83
@ Test
78
84
void tsfmtInline () throws Exception {
79
85
writePomWithTypescriptSteps (
86
+ TEST_FILE_PATH ,
80
87
"<tsfmt>" ,
81
88
" <config>" ,
82
89
" <indentSize>1</indentSize>" ,
83
90
" <convertTabsToSpaces>true</convertTabsToSpaces>" ,
84
91
" </config>" ,
85
92
"</tsfmt>" );
86
- run ("tsfmt" );
93
+ runTsfmt ("tsfmt" );
87
94
}
88
95
89
96
@ Test
90
97
void tsconfig () throws Exception {
91
98
writePomWithTypescriptSteps (
99
+ TEST_FILE_PATH ,
92
100
"<tsfmt>" ,
93
101
" <tsconfigFile>${project.basedir}/tsconfig.json</tsconfigFile>" ,
94
102
"</tsfmt>" );
95
103
setFile ("tsconfig.json" ).toResource ("npm/tsfmt/tsconfig/tsconfig.json" );
96
- run ("tsconfig" );
104
+ runTsfmt ("tsconfig" );
97
105
}
98
106
99
107
@ Test
100
108
void testTypescript_2_Configs () throws Exception {
109
+ String path = "src/main/typescript/test.ts" ;
110
+
101
111
writePomWithTypescriptSteps (
112
+ path ,
102
113
"<tsfmt>" ,
103
114
" <vscodeFile>${basedir}/tslint.json</vscodeFile>" ,
104
115
" <tsfmtFile>${basedir}/tslint.json</tsfmtFile>" ,
105
116
"</tsfmt>" );
106
117
setFile ("vscode.json" ).toResource ("npm/tsfmt/vscode/vscode.json" );
107
118
setFile ("tsfmt.json" ).toResource ("npm/tsfmt/tsfmt/tsfmt.json" );
108
119
109
- String path = "src/main/typescript/test.ts" ;
110
120
setFile (path ).toResource ("npm/tsfmt/tsfmt/tsfmt.dirty" );
111
121
Result result = mavenRunner ().withArguments ("spotless:apply" ).runHasError ();
112
122
assertThat (result .output ()).contains ("must specify exactly one configFile or config" );
@@ -120,11 +130,12 @@ void testNpmrcIsAutoPickedUp() throws Exception {
120
130
"fetch-retry-mintimeout=250" ,
121
131
"fetch-retry-maxtimeout=250" );
122
132
writePomWithTypescriptSteps (
133
+ TEST_FILE_PATH ,
123
134
"<tsfmt>" ,
124
135
" <tslintFile>${basedir}/tslint.json</tslintFile>" ,
125
136
"</tsfmt>" );
126
137
setFile ("tslint.json" ).toResource ("npm/tsfmt/tslint/tslint.json" );
127
- Result result = runExpectingError ("tslint" );
138
+ Result result = runExpectingErrorTsfmt ("tslint" );
128
139
assertThat (result .output ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
129
140
}
130
141
@@ -136,12 +147,76 @@ void testNpmrcIsConfigurativelyPickedUp() throws Exception {
136
147
"fetch-retry-mintimeout=250" ,
137
148
"fetch-retry-maxtimeout=250" );
138
149
writePomWithTypescriptSteps (
150
+ TEST_FILE_PATH ,
139
151
"<tsfmt>" ,
140
152
" <tslintFile>${basedir}/tslint.json</tslintFile>" ,
141
153
" <npmrc>${basedir}/.custom_npmrc</npmrc>" ,
142
154
"</tsfmt>" );
143
155
setFile ("tslint.json" ).toResource ("npm/tsfmt/tslint/tslint.json" );
144
- Result result = runExpectingError ("tslint" );
156
+ Result result = runExpectingErrorTsfmt ("tslint" );
145
157
assertThat (result .output ()).containsPattern ("Running npm command.*npm install.* failed with exit code: 1" );
146
158
}
159
+
160
+ @ Test
161
+ void eslintConfigFile () throws Exception {
162
+ writePomWithTypescriptSteps (
163
+ TEST_FILE_PATH ,
164
+ "<eslint>" ,
165
+ " <configFile>.eslintrc.js</configFile>" ,
166
+ "</eslint>" );
167
+ setFile (".eslintrc.js" ).toResource ("npm/eslint/typescript/custom_rules/.eslintrc.js" );
168
+ setFile (TEST_FILE_PATH ).toResource ("npm/eslint/typescript/custom_rules/typescript.dirty" );
169
+
170
+ mavenRunner ().withArguments ("spotless:apply" ).runNoError ();
171
+ assertFile (TEST_FILE_PATH ).sameAsResource ("npm/eslint/typescript/custom_rules/typescript.clean" );
172
+ }
173
+
174
+ @ Test
175
+ void eslintConfigJs () throws Exception {
176
+ final String configJs = ResourceHarness .getTestResource ("npm/eslint/typescript/custom_rules/.eslintrc.js" )
177
+ .replace ("module.exports = " , "" );
178
+ writePomWithTypescriptSteps (
179
+ TEST_FILE_PATH ,
180
+ "<eslint>" ,
181
+ " <configJs>" + configJs + "</configJs>" ,
182
+ "</eslint>" );
183
+ setFile (TEST_FILE_PATH ).toResource ("npm/eslint/typescript/custom_rules/typescript.dirty" );
184
+
185
+ mavenRunner ().withArguments ("spotless:apply" ).runNoError ();
186
+ assertFile (TEST_FILE_PATH ).sameAsResource ("npm/eslint/typescript/custom_rules/typescript.clean" );
187
+ }
188
+
189
+ @ Test
190
+ void eslintStyleguideStandardWithTypescript () throws Exception {
191
+ writePomWithTypescriptSteps (
192
+ TEST_FILE_PATH ,
193
+ "<eslint>" ,
194
+ " <configFile>.eslintrc.js</configFile>" ,
195
+ " <styleGuide>standard-with-typescript</styleGuide>" ,
196
+ " <tsconfigFile>${basedir}/tsconfig.json</tsconfigFile>" ,
197
+ "</eslint>" );
198
+ setFile (".eslintrc.js" ).toResource ("npm/eslint/typescript/styleguide/standard_with_typescript/.eslintrc.js" );
199
+ setFile ("tsconfig.json" ).toResource ("npm/eslint/typescript/styleguide/standard_with_typescript/tsconfig.json" );
200
+ setFile (TEST_FILE_PATH ).toResource ("npm/eslint/typescript/styleguide/standard_with_typescript/typescript.dirty" );
201
+
202
+ mavenRunner ().withArguments ("spotless:apply" ).runNoError ();
203
+ assertFile (TEST_FILE_PATH ).sameAsResource ("npm/eslint/typescript/styleguide/standard_with_typescript/typescript.clean" );
204
+ }
205
+
206
+ @ Test
207
+ void eslintStyleguideXo () throws Exception {
208
+ writePomWithTypescriptSteps (
209
+ TEST_FILE_PATH ,
210
+ "<eslint>" ,
211
+ " <configFile>.eslintrc.js</configFile>" ,
212
+ " <styleGuide>xo-typescript</styleGuide>" ,
213
+ " <tsconfigFile>${basedir}/tsconfig.json</tsconfigFile>" ,
214
+ "</eslint>" );
215
+ setFile (".eslintrc.js" ).toResource ("npm/eslint/typescript/styleguide/xo/.eslintrc.js" );
216
+ setFile ("tsconfig.json" ).toResource ("npm/eslint/typescript/styleguide/xo/tsconfig.json" );
217
+ setFile (TEST_FILE_PATH ).toResource ("npm/eslint/typescript/styleguide/xo/typescript.dirty" );
218
+
219
+ mavenRunner ().withArguments ("spotless:apply" ).runNoError ();
220
+ assertFile (TEST_FILE_PATH ).sameAsResource ("npm/eslint/typescript/styleguide/xo/typescript.clean" );
221
+ }
147
222
}
0 commit comments