|
| 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.spotless.maven.rome; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | +import static org.owasp.encoder.Encode.forXml; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import com.diffplug.spotless.maven.MavenIntegrationHarness; |
| 25 | + |
| 26 | +class RomeMavenTest extends MavenIntegrationHarness { |
| 27 | + /** |
| 28 | + * Tests that rome can be used as a generic formatting step. |
| 29 | + * |
| 30 | + * @throws Exception When a test failure occurs. |
| 31 | + */ |
| 32 | + @Test |
| 33 | + public void testAsGenericStep() throws Exception { |
| 34 | + writePomWithRomeSteps("**/*.js", "<rome><version>12.0.0</version></rome>"); |
| 35 | + setFile("rome_test.js").toResource("rome/js/fileBefore.js"); |
| 36 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 37 | + assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js"); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Tests that rome can be used as a JavaScript formatting step. |
| 42 | + * |
| 43 | + * @throws Exception When a test failure occurs. |
| 44 | + */ |
| 45 | + @Test |
| 46 | + public void testAsJavaScriptStep() throws Exception { |
| 47 | + writePomWithJavascriptSteps("**/*.js", "<rome><version>12.0.0</version></rome>"); |
| 48 | + setFile("rome_test.js").toResource("rome/js/fileBefore.js"); |
| 49 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 50 | + assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js"); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tests that rome can be used as a JSON formatting step. |
| 55 | + * |
| 56 | + * @throws Exception When a test failure occurs. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void testAsJsonStep() throws Exception { |
| 60 | + writePomWithJsonSteps("**/*.json", "<rome><version>12.0.0</version></rome>"); |
| 61 | + setFile("rome_test.json").toResource("rome/json/fileBefore.json"); |
| 62 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 63 | + assertFile("rome_test.json").sameAsResource("rome/json/fileAfter.json"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests that rome can be used as a TypeScript formatting step. |
| 68 | + * |
| 69 | + * @throws Exception When a test failure occurs. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void testAsTypeScriptStep() throws Exception { |
| 73 | + writePomWithTypescriptSteps("**/*.ts", "<rome><version>12.0.0</version></rome>"); |
| 74 | + setFile("rome_test.ts").toResource("rome/ts/fileBefore.ts"); |
| 75 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 76 | + assertFile("rome_test.ts").sameAsResource("rome/ts/fileAfter.ts"); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Tests that the language can be specified for the generic format step. |
| 81 | + * |
| 82 | + * @throws Exception When a test failure occurs. |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void testCanSetLanguageForGenericStep() throws Exception { |
| 86 | + writePomWithRomeSteps("**/*.nosj", "<rome><version>12.0.0</version><language>json</language></rome>"); |
| 87 | + setFile("rome_test.nosj").toResource("rome/json/fileBefore.json"); |
| 88 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 89 | + assertFile("rome_test.nosj").sameAsResource("rome/json/fileAfter.json"); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Tests that an absolute config path can be specified. |
| 94 | + * |
| 95 | + * @throws Exception When a test failure occurs. |
| 96 | + */ |
| 97 | + @Test |
| 98 | + public void testConfigPathAbsolute() throws Exception { |
| 99 | + var path = newFile("configs").getAbsolutePath(); |
| 100 | + writePomWithRomeSteps("**/*.js", |
| 101 | + "<rome><version>12.0.0</version><configPath>" + forXml(path) + "</configPath></rome>"); |
| 102 | + setFile("rome_test.js").toResource("rome/js/longLineBefore.js"); |
| 103 | + setFile("configs/rome.json").toResource("rome/config/line-width-120.json"); |
| 104 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 105 | + assertFile("rome_test.js").sameAsResource("rome/js/longLineAfter120.js"); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Tests that a path to the directory with the rome.json config file can be |
| 110 | + * specified. Uses a config file with a line width of 120. |
| 111 | + * |
| 112 | + * @throws Exception When a test failure occurs. |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void testConfigPathLineWidth120() throws Exception { |
| 116 | + writePomWithRomeSteps("**/*.js", "<rome><version>12.0.0</version><configPath>configs</configPath></rome>"); |
| 117 | + setFile("rome_test.js").toResource("rome/js/longLineBefore.js"); |
| 118 | + setFile("configs/rome.json").toResource("rome/config/line-width-120.json"); |
| 119 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 120 | + assertFile("rome_test.js").sameAsResource("rome/js/longLineAfter120.js"); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Tests that a path to the directory with the rome.json config file can be |
| 125 | + * specified. Uses a config file with a line width of 80. |
| 126 | + * |
| 127 | + * @throws Exception When a test failure occurs. |
| 128 | + */ |
| 129 | + @Test |
| 130 | + public void testConfigPathLineWidth80() throws Exception { |
| 131 | + writePomWithRomeSteps("**/*.js", "<rome><version>12.0.0</version><configPath>configs</configPath></rome>"); |
| 132 | + setFile("rome_test.js").toResource("rome/js/longLineBefore.js"); |
| 133 | + setFile("configs/rome.json").toResource("rome/config/line-width-80.json"); |
| 134 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 135 | + assertFile("rome_test.js").sameAsResource("rome/js/longLineAfter80.js"); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Tests that the download directory can be an absolute path. |
| 140 | + * |
| 141 | + * @throws Exception When a test failure occurs. |
| 142 | + */ |
| 143 | + @Test |
| 144 | + public void testDownloadDirAbsolute() throws Exception { |
| 145 | + var path = newFile("target/bin/rome").getAbsoluteFile().toString(); |
| 146 | + writePomWithRomeSteps("**/*.js", |
| 147 | + "<rome><version>12.0.0</version><downloadDir>" + forXml(path) + "</downloadDir></rome>"); |
| 148 | + setFile("rome_test.js").toResource("rome/js/fileBefore.js"); |
| 149 | + assertTrue(!newFile("target/bin/rome").exists() || newFile("target/bin/rome").list().length == 0); |
| 150 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 151 | + assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js"); |
| 152 | + assertEquals(2, newFile("target/bin/rome").list().length); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Tests that the download directory can be changed to a path relative to the |
| 157 | + * project's base directory. |
| 158 | + * |
| 159 | + * @throws Exception When a test failure occurs. |
| 160 | + */ |
| 161 | + @Test |
| 162 | + public void testDownloadDirRelative() throws Exception { |
| 163 | + writePomWithRomeSteps("**/*.js", |
| 164 | + "<rome><version>12.0.0</version><downloadDir>target/bin/rome</downloadDir></rome>"); |
| 165 | + setFile("rome_test.js").toResource("rome/js/fileBefore.js"); |
| 166 | + assertTrue(!newFile("target/bin/rome").exists() || newFile("target/bin/rome").list().length == 0); |
| 167 | + mavenRunner().withArguments("spotless:apply").runNoError(); |
| 168 | + assertFile("rome_test.js").sameAsResource("rome/js/fileAfter.js"); |
| 169 | + assertEquals(2, newFile("target/bin/rome").list().length); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Tests that the build fails when the input file could not be parsed. |
| 174 | + * |
| 175 | + * @throws Exception When a test failure occurs. |
| 176 | + */ |
| 177 | + @Test |
| 178 | + public void testFailureWhenNotParseable() throws Exception { |
| 179 | + writePomWithRomeSteps("**/*.js", "<rome><version>12.0.0</version><language>json</language></rome>"); |
| 180 | + setFile("rome_test.js").toResource("rome/js/fileBefore.js"); |
| 181 | + var result = mavenRunner().withArguments("spotless:apply").runHasError(); |
| 182 | + assertFile("rome_test.js").sameAsResource("rome/js/fileBefore.js"); |
| 183 | + assertTrue(result.stdOutUtf8().contains("Format with errors is disabled.")); |
| 184 | + assertTrue(result.stdOutUtf8().contains("Unable to format file")); |
| 185 | + assertTrue(result.stdOutUtf8().contains("Step 'rome' found problem in 'rome_test.js'")); |
| 186 | + } |
| 187 | +} |
0 commit comments