|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 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 java.io.IOException; |
| 19 | + |
| 20 | +import org.assertj.core.api.Assertions; |
| 21 | +import org.gradle.testkit.runner.BuildResult; |
| 22 | +import org.junit.jupiter.api.Nested; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.ValueSource; |
| 26 | + |
| 27 | +import com.diffplug.spotless.tag.NpmTest; |
| 28 | + |
| 29 | +@NpmTest |
| 30 | +class JavascriptExtensionTest extends GradleIntegrationHarness { |
| 31 | + |
| 32 | + @NpmTest |
| 33 | + @Nested |
| 34 | + class EslintGeneralJavascriptTests extends GradleIntegrationHarness { |
| 35 | + @Test |
| 36 | + void supportsEslintFormattingForJavascript() throws IOException { |
| 37 | + setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); |
| 38 | + setFile("build.gradle").toLines( |
| 39 | + "plugins {", |
| 40 | + " id 'com.diffplug.spotless'", |
| 41 | + "}", |
| 42 | + "repositories { mavenCentral() }", |
| 43 | + "spotless {", |
| 44 | + " javascript {", |
| 45 | + " target 'test.js'", |
| 46 | + " eslint().configFile('.eslintrc.js').styleGuide('standard')", |
| 47 | + " }", |
| 48 | + "}"); |
| 49 | + setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); |
| 50 | + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); |
| 51 | + assertFile("test.js").sameAsResource("npm/eslint/javascript/styleguide/standard/javascript-es6.clean"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void eslintDoesNotAllowToUseTsStyleGuideForJavascript() throws IOException { |
| 56 | + setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); |
| 57 | + setFile("build.gradle").toLines( |
| 58 | + "plugins {", |
| 59 | + " id 'com.diffplug.spotless'", |
| 60 | + "}", |
| 61 | + "repositories { mavenCentral() }", |
| 62 | + "spotless {", |
| 63 | + " javascript {", |
| 64 | + " target 'test.js'", |
| 65 | + " eslint().configFile('.eslintrc.js').styleGuide('xo-typescript')", |
| 66 | + " }", |
| 67 | + "}"); |
| 68 | + setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); |
| 69 | + final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail(); |
| 70 | + Assertions.assertThat(spotlessApply.getOutput()).contains("Unknown style guide: xo-typescript"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void eslintAllowsToSpecifyEslintVersionForJavascript() throws IOException { |
| 75 | + setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); |
| 76 | + setFile("build.gradle").toLines( |
| 77 | + "plugins {", |
| 78 | + " id 'com.diffplug.spotless'", |
| 79 | + "}", |
| 80 | + "repositories { mavenCentral() }", |
| 81 | + "spotless {", |
| 82 | + " javascript {", |
| 83 | + " target 'test.js'", |
| 84 | + " eslint('8.28.0').configFile('.eslintrc.js').styleGuide('standard')", |
| 85 | + " }", |
| 86 | + "}"); |
| 87 | + setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); |
| 88 | + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); |
| 89 | + assertFile("test.js").sameAsResource("npm/eslint/javascript/styleguide/standard/javascript-es6.clean"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void esllintAllowsToSpecifyInlineConfig() throws IOException { |
| 94 | + final String eslintConfigJs = String.join("\n", |
| 95 | + "{", |
| 96 | + " env: {", |
| 97 | + " browser: true,", |
| 98 | + " es2021: true", |
| 99 | + " },", |
| 100 | + " extends: 'standard',", |
| 101 | + " overrides: [", |
| 102 | + " ],", |
| 103 | + " parserOptions: {", |
| 104 | + " ecmaVersion: 'latest',", |
| 105 | + " sourceType: 'module'", |
| 106 | + " },", |
| 107 | + " rules: {", |
| 108 | + " }", |
| 109 | + "}"); |
| 110 | + setFile("build.gradle").toLines( |
| 111 | + "plugins {", |
| 112 | + " id 'com.diffplug.spotless'", |
| 113 | + "}", |
| 114 | + "repositories { mavenCentral() }", |
| 115 | + "spotless {", |
| 116 | + " javascript {", |
| 117 | + " target 'test.js'", |
| 118 | + " eslint().configJs('''" + eslintConfigJs + "''').styleGuide('standard')", |
| 119 | + " }", |
| 120 | + "}"); |
| 121 | + setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); |
| 122 | + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); |
| 123 | + assertFile("test.js").sameAsResource("npm/eslint/javascript/styleguide/standard/javascript-es6.clean"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void eslintRequiresAnExplicitEslintConfig() throws IOException { |
| 128 | + setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); |
| 129 | + setFile("build.gradle").toLines( |
| 130 | + "plugins {", |
| 131 | + " id 'com.diffplug.spotless'", |
| 132 | + "}", |
| 133 | + "repositories { mavenCentral() }", |
| 134 | + "spotless {", |
| 135 | + " javascript {", |
| 136 | + " target 'test.js'", |
| 137 | + " eslint().styleGuide('standard')", |
| 138 | + " }", |
| 139 | + "}"); |
| 140 | + setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); |
| 141 | + BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").buildAndFail(); |
| 142 | + Assertions.assertThat(spotlessApply.getOutput()).contains("ESLint must be configured"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void eslintAllowsSpecifyingCustomLibraryVersions() throws IOException { |
| 147 | + setFile(".eslintrc.js").toResource("npm/eslint/javascript/styleguide/standard/.eslintrc.js"); |
| 148 | + setFile("build.gradle").toLines( |
| 149 | + "plugins {", |
| 150 | + " id 'com.diffplug.spotless'", |
| 151 | + "}", |
| 152 | + "repositories { mavenCentral() }", |
| 153 | + "spotless {", |
| 154 | + " javascript {", |
| 155 | + " target 'test.js'", |
| 156 | + " eslint([", |
| 157 | + " 'eslint': '8.28.0',", |
| 158 | + " 'eslint-config-standard': '17.0.0',", |
| 159 | + " 'eslint-plugin-import': '2.26.0',", |
| 160 | + " 'eslint-plugin-n': '15.6.0',", |
| 161 | + " 'eslint-plugin-promise': '6.1.1'", |
| 162 | + " ]).configFile('.eslintrc.js')", |
| 163 | + " }", |
| 164 | + "}"); |
| 165 | + setFile("test.js").toResource("npm/eslint/javascript/styleguide/standard/javascript-es6.dirty"); |
| 166 | + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); |
| 167 | + assertFile("test.js").sameAsResource("npm/eslint/javascript/styleguide/standard/javascript-es6.clean"); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + @NpmTest |
| 173 | + @Nested |
| 174 | + class EslintPopularJsStyleGuideTests extends GradleIntegrationHarness { |
| 175 | + @ParameterizedTest(name = "{index}: eslint can be applied using styleguide {0}") |
| 176 | + @ValueSource(strings = {"airbnb", "google", "standard", "xo"}) |
| 177 | + void formattingUsingStyleguide(String styleguide) throws Exception { |
| 178 | + |
| 179 | + final String styleguidePath = "npm/eslint/javascript/styleguide/" + styleguide + "/"; |
| 180 | + |
| 181 | + setFile(".eslintrc.js").toResource(styleguidePath + ".eslintrc.js"); |
| 182 | + setFile("build.gradle").toLines( |
| 183 | + "plugins {", |
| 184 | + " id 'com.diffplug.spotless'", |
| 185 | + "}", |
| 186 | + "repositories { mavenCentral() }", |
| 187 | + "spotless {", |
| 188 | + " javascript {", |
| 189 | + " target 'test.js'", |
| 190 | + " eslint().configFile('.eslintrc.js').styleGuide('" + styleguide + "')", |
| 191 | + " }", |
| 192 | + "}"); |
| 193 | + setFile("test.js").toResource(styleguidePath + "javascript-es6.dirty"); |
| 194 | + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); |
| 195 | + assertFile("test.js").sameAsResource(styleguidePath + "javascript-es6.clean"); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @NpmTest |
| 200 | + @Nested |
| 201 | + class JavascriptPrettierTests extends GradleIntegrationHarness { |
| 202 | + @Test |
| 203 | + void supportsPrettierFormattingForJavascript() throws IOException { |
| 204 | + setFile("build.gradle").toLines( |
| 205 | + "plugins {", |
| 206 | + " id 'com.diffplug.spotless'", |
| 207 | + "}", |
| 208 | + "repositories { mavenCentral() }", |
| 209 | + "spotless {", |
| 210 | + " javascript {", |
| 211 | + " target 'test.js'", |
| 212 | + " prettier()", |
| 213 | + " }", |
| 214 | + "}"); |
| 215 | + setFile("test.js").toResource("npm/prettier/filetypes/javascript-es6/javascript-es6.dirty"); |
| 216 | + gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); |
| 217 | + assertFile("test.js").sameAsResource("npm/prettier/filetypes/javascript-es6/javascript-es6.clean"); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | +} |
0 commit comments