Skip to content

Commit dd5ae78

Browse files
committed
eslint: tests for maven integration into typescript
1 parent 31462a2 commit dd5ae78

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ app.post("/eslint/format", async (req, res) => {
5555
// LintResult[] // https://eslint.org/docs/latest/developer-guide/nodejs-api#-lintresult-type
5656
const results = await eslint.lintText(format_data.file_content, lintTextOptions);
5757
if (results.length !== 1) {
58-
res.status(501).send("Error while formatting: Unexpected number of results");
58+
res.status(501).send("Error while formatting: Unexpected number of results: " + JSON.stringify(results));
5959
return;
6060
}
6161
const result = results[0];

plugin-maven/src/main/java/com/diffplug/spotless/maven/javascript/EslintJs.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected EslintConfig eslintConfig(FormatterStepConfig stepConfig) {
6969

7070
private void addStyleGuideDevDependencies(Map<String, String> devDependencies) {
7171
if (this.styleGuide != null) {
72-
EslintFormatterStep.PopularStyleGuide styleGuide = EslintFormatterStep.PopularStyleGuide.valueOf(this.styleGuide);
72+
EslintFormatterStep.PopularStyleGuide styleGuide = EslintFormatterStep.PopularStyleGuide.fromNameOrNull(this.styleGuide);
7373
validateStyleGuide(styleGuide);
7474
devDependencies.putAll(styleGuide.devDependencies());
7575
}
@@ -80,7 +80,7 @@ private void validateStyleGuide(EslintFormatterStep.PopularStyleGuide styleGuide
8080
throw new IllegalArgumentException("StyleGuide '" + this.styleGuide + "' is not supported. Supported style guides: " + supportedStyleGuides());
8181
}
8282
if (!isValidStyleGuide(styleGuide)) {
83-
throw new IllegalArgumentException("StyleGuide must be of correct type but is: " + styleGuide + ". Use one of the following: " + supportedStyleGuides());
83+
throw new IllegalArgumentException("StyleGuide must be of correct type but is: " + styleGuide.getPopularStyleGuideName() + ". Use one of the following: " + supportedStyleGuides());
8484
}
8585
}
8686

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ protected void writePomWithCppSteps(String... steps) throws IOException {
136136
writePom(groupWithSteps("cpp", steps));
137137
}
138138

139-
protected void writePomWithTypescriptSteps(String... steps) throws IOException {
140-
writePom(groupWithSteps("typescript", including("**/*.ts"), steps));
139+
protected void writePomWithTypescriptSteps(String includes, String... steps) throws IOException {
140+
writePom(groupWithSteps("typescript", including(includes), steps));
141141
}
142142

143143
protected void writePomWithSqlSteps(String... steps) throws IOException {

plugin-maven/src/test/java/com/diffplug/spotless/maven/typescript/TypescriptFormatStepTest.java

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,92 +21,102 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import com.diffplug.spotless.ResourceHarness;
2425
import com.diffplug.spotless.maven.MavenIntegrationHarness;
2526
import com.diffplug.spotless.maven.MavenRunner.Result;
2627
import com.diffplug.spotless.tag.NpmTest;
2728

2829
@NpmTest
2930
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);
3236
mavenRunner().withArguments("spotless:apply").runNoError();
3337
assertFile(path).sameAsResource("npm/tsfmt/" + kind + "/" + kind + ".clean");
3438
}
3539

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;
4043
}
4144

42-
private Result runExpectingError(String kind) throws IOException, InterruptedException {
43-
prepareRun(kind);
45+
private Result runExpectingErrorTsfmt(String kind) throws IOException, InterruptedException {
46+
prepareRunTsfmt(kind);
4447
return mavenRunner().withArguments("spotless:apply").runHasError();
4548
}
4649

4750
@Test
4851
void tslint() throws Exception {
4952
writePomWithTypescriptSteps(
53+
TEST_FILE_PATH,
5054
"<tsfmt>",
5155
" <tslintFile>${basedir}/tslint.json</tslintFile>",
5256
"</tsfmt>");
5357
setFile("tslint.json").toResource("npm/tsfmt/tslint/tslint.json");
54-
run("tslint");
58+
runTsfmt("tslint");
5559
}
5660

5761
@Test
5862
void vscode() throws Exception {
5963
writePomWithTypescriptSteps(
64+
TEST_FILE_PATH,
6065
"<tsfmt>",
6166
" <vscodeFile>${basedir}/vscode.json</vscodeFile>",
6267
"</tsfmt>");
6368
setFile("vscode.json").toResource("npm/tsfmt/vscode/vscode.json");
64-
run("vscode");
69+
runTsfmt("vscode");
6570
}
6671

6772
@Test
6873
void tsfmt() throws Exception {
6974
writePomWithTypescriptSteps(
75+
TEST_FILE_PATH,
7076
"<tsfmt>",
7177
" <tsfmtFile>${basedir}/tsfmt.json</tsfmtFile>",
7278
"</tsfmt>");
7379
setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json");
74-
run("tsfmt");
80+
runTsfmt("tsfmt");
7581
}
7682

7783
@Test
7884
void tsfmtInline() throws Exception {
7985
writePomWithTypescriptSteps(
86+
TEST_FILE_PATH,
8087
"<tsfmt>",
8188
" <config>",
8289
" <indentSize>1</indentSize>",
8390
" <convertTabsToSpaces>true</convertTabsToSpaces>",
8491
" </config>",
8592
"</tsfmt>");
86-
run("tsfmt");
93+
runTsfmt("tsfmt");
8794
}
8895

8996
@Test
9097
void tsconfig() throws Exception {
9198
writePomWithTypescriptSteps(
99+
TEST_FILE_PATH,
92100
"<tsfmt>",
93101
" <tsconfigFile>${project.basedir}/tsconfig.json</tsconfigFile>",
94102
"</tsfmt>");
95103
setFile("tsconfig.json").toResource("npm/tsfmt/tsconfig/tsconfig.json");
96-
run("tsconfig");
104+
runTsfmt("tsconfig");
97105
}
98106

99107
@Test
100108
void testTypescript_2_Configs() throws Exception {
109+
String path = "src/main/typescript/test.ts";
110+
101111
writePomWithTypescriptSteps(
112+
path,
102113
"<tsfmt>",
103114
" <vscodeFile>${basedir}/tslint.json</vscodeFile>",
104115
" <tsfmtFile>${basedir}/tslint.json</tsfmtFile>",
105116
"</tsfmt>");
106117
setFile("vscode.json").toResource("npm/tsfmt/vscode/vscode.json");
107118
setFile("tsfmt.json").toResource("npm/tsfmt/tsfmt/tsfmt.json");
108119

109-
String path = "src/main/typescript/test.ts";
110120
setFile(path).toResource("npm/tsfmt/tsfmt/tsfmt.dirty");
111121
Result result = mavenRunner().withArguments("spotless:apply").runHasError();
112122
assertThat(result.output()).contains("must specify exactly one configFile or config");
@@ -120,11 +130,12 @@ void testNpmrcIsAutoPickedUp() throws Exception {
120130
"fetch-retry-mintimeout=250",
121131
"fetch-retry-maxtimeout=250");
122132
writePomWithTypescriptSteps(
133+
TEST_FILE_PATH,
123134
"<tsfmt>",
124135
" <tslintFile>${basedir}/tslint.json</tslintFile>",
125136
"</tsfmt>");
126137
setFile("tslint.json").toResource("npm/tsfmt/tslint/tslint.json");
127-
Result result = runExpectingError("tslint");
138+
Result result = runExpectingErrorTsfmt("tslint");
128139
assertThat(result.output()).containsPattern("Running npm command.*npm install.* failed with exit code: 1");
129140
}
130141

@@ -136,12 +147,76 @@ void testNpmrcIsConfigurativelyPickedUp() throws Exception {
136147
"fetch-retry-mintimeout=250",
137148
"fetch-retry-maxtimeout=250");
138149
writePomWithTypescriptSteps(
150+
TEST_FILE_PATH,
139151
"<tsfmt>",
140152
" <tslintFile>${basedir}/tslint.json</tslintFile>",
141153
" <npmrc>${basedir}/.custom_npmrc</npmrc>",
142154
"</tsfmt>");
143155
setFile("tslint.json").toResource("npm/tsfmt/tslint/tslint.json");
144-
Result result = runExpectingError("tslint");
156+
Result result = runExpectingErrorTsfmt("tslint");
145157
assertThat(result.output()).containsPattern("Running npm command.*npm install.* failed with exit code: 1");
146158
}
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+
}
147222
}

0 commit comments

Comments
 (0)