|
15 | 15 | */ |
16 | 16 | package com.diffplug.spotless.cli.steps; |
17 | 17 |
|
| 18 | +import java.io.File; |
18 | 19 | import java.io.IOException; |
19 | 20 |
|
20 | 21 | import org.junit.jupiter.api.Test; |
|
25 | 26 | import com.diffplug.spotless.tag.CliProcessNpmTest; |
26 | 27 | import com.diffplug.spotless.tag.NpmTest; |
27 | 28 |
|
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
28 | 31 | @NpmTest |
29 | 32 | @CliProcessNpmTest |
30 | 33 | @CliNativeNpmTest |
@@ -57,4 +60,165 @@ void itRunsPrettierForTsFilesWithOptionFile() throws Exception { |
57 | 60 |
|
58 | 61 | selfie().expectResource("test.ts").toMatchDisk(); |
59 | 62 | } |
| 63 | + |
| 64 | + @Test |
| 65 | + void itRunsPrettierWithoutAnyOptions() throws IOException { |
| 66 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 67 | + |
| 68 | + SpotlessCLIRunner.Result result = |
| 69 | + cliRunner().withTargets("test.ts").withStep(Prettier.class).run(); |
| 70 | + |
| 71 | + selfie().expectResource("test.ts").toMatchDisk(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void itRunsSpecificPrettierVersion2x() throws IOException { |
| 76 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 77 | + |
| 78 | + SpotlessCLIRunner.Result result = cliRunner() |
| 79 | + .withTargets("test.ts") |
| 80 | + .withStep(Prettier.class) |
| 81 | + .withOption("--dev-dependency", "prettier=2.8.7") |
| 82 | + .run(); |
| 83 | + |
| 84 | + selfie().expectResource("test.ts").toMatchDisk(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void itUsesACacheDir() throws IOException { |
| 89 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 90 | + |
| 91 | + File cacheDir = newFolder("cachedir"); |
| 92 | + SpotlessCLIRunner.Result result = cliRunner() |
| 93 | + .withTargets("test.ts") |
| 94 | + .withStep(Prettier.class) |
| 95 | + .withOption("--npm-install-cache-dir", cacheDir.getPath()) |
| 96 | + .run(); |
| 97 | + |
| 98 | + selfie().expectResource("test.ts").toMatchDisk(); |
| 99 | + |
| 100 | + assertThat(cacheDir).isNotEmptyDirectory(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void itUsesNpmExec() { |
| 105 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 106 | + File npmOut = newFile("npmoutput.txt"); |
| 107 | + File npm = setFile("npm") |
| 108 | + .toContent( |
| 109 | + """ |
| 110 | + #!/bin/sh |
| 111 | + echo "npm exec" > %s |
| 112 | + exit -99 |
| 113 | + """ |
| 114 | + .formatted(npmOut.getAbsolutePath())) |
| 115 | + .makeExecutable() |
| 116 | + .getFile(); |
| 117 | + |
| 118 | + File nodeOut = newFile("nodeoutput.txt"); |
| 119 | + setFile("node") |
| 120 | + .toContent( |
| 121 | + """ |
| 122 | + #!/bin/sh |
| 123 | + echo "node exec" > %s |
| 124 | + exit -100 |
| 125 | + """ |
| 126 | + .formatted(nodeOut.getAbsolutePath())) |
| 127 | + .makeExecutable(); |
| 128 | + |
| 129 | + SpotlessCLIRunner.Result result = cliRunner() |
| 130 | + .withTargets("test.ts") |
| 131 | + .withStep(Prettier.class) |
| 132 | + .withOption("--npm-exec", npm.getPath()) |
| 133 | + .runAndFail(); |
| 134 | + |
| 135 | + selfie().expectFile(npmOut).toBe(""" |
| 136 | +npm exec |
| 137 | +"""); |
| 138 | + selfie().expectFile(nodeOut).toBe(""); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void itUsesNodeExec() { |
| 143 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 144 | + File npmOut = newFile("npmoutput.txt"); |
| 145 | + setFile("npm") |
| 146 | + .toContent( |
| 147 | + """ |
| 148 | + #!/bin/sh |
| 149 | + echo "npm exec" > %s |
| 150 | + exit -99 |
| 151 | + """ |
| 152 | + .formatted(npmOut.getAbsolutePath())) |
| 153 | + .makeExecutable(); |
| 154 | + |
| 155 | + File nodeOut = newFile("nodeoutput.txt"); |
| 156 | + File node = setFile("node") |
| 157 | + .toContent( |
| 158 | + """ |
| 159 | + #!/bin/sh |
| 160 | + echo "node exec" > %s |
| 161 | + exit -100 |
| 162 | + """ |
| 163 | + .formatted(nodeOut.getAbsolutePath())) |
| 164 | + .makeExecutable() |
| 165 | + .getFile(); |
| 166 | + |
| 167 | + SpotlessCLIRunner.Result result = cliRunner() |
| 168 | + .withTargets("test.ts") |
| 169 | + .withStep(Prettier.class) |
| 170 | + .withOption("--node-exec", node.getPath()) |
| 171 | + .runAndFail(); |
| 172 | + |
| 173 | + selfie().expectFile(npmOut).toBe(""" |
| 174 | +npm exec |
| 175 | +"""); |
| 176 | + selfie().expectFile(nodeOut).toBe(""); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void itUsesNpmrcFile() { |
| 181 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 182 | + |
| 183 | + File npmrc = setFile(".custom_npmrc") |
| 184 | + .toLines( |
| 185 | + "registry=https://i.do.not.exist.com", |
| 186 | + "fetch-timeout=250", |
| 187 | + "fetch-retry-mintimeout=250", |
| 188 | + "fetch-retry-maxtimeout=250") |
| 189 | + .getFile(); |
| 190 | + |
| 191 | + SpotlessCLIRunner.Result result = cliRunner() |
| 192 | + .withTargets("test.ts") |
| 193 | + .withStep(Prettier.class) |
| 194 | + .withOption("--npmrc-file", npmrc.getPath()) |
| 195 | + .runAndFail(); |
| 196 | + |
| 197 | + assertThat(result.exitCode()).isNotZero(); |
| 198 | + assertThat(result.stdErr()).containsPattern(".*Running npm command.*NpmInstall.* failed with exit code: 1"); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void itUsesNpmrcFileInAdditionalLocation() throws IOException { |
| 203 | + setFile("test.ts").toResource("npm/prettier/config/typescript.dirty"); |
| 204 | + |
| 205 | + File additionalLocation = newFolder("additionalLocation"); |
| 206 | + |
| 207 | + File npmrc = setFile(additionalLocation.getName() + "/.npmrc") |
| 208 | + .toLines( |
| 209 | + "registry=https://i.do.not.exist.com", |
| 210 | + "fetch-timeout=250", |
| 211 | + "fetch-retry-mintimeout=250", |
| 212 | + "fetch-retry-maxtimeout=250") |
| 213 | + .getFile(); |
| 214 | + |
| 215 | + SpotlessCLIRunner.Result result = cliRunner() |
| 216 | + .withTargets("test.ts") |
| 217 | + .withStep(Prettier.class) |
| 218 | + .withOption("--additional-npmrc-location", additionalLocation.getPath()) |
| 219 | + .runAndFail(); |
| 220 | + |
| 221 | + assertThat(result.exitCode()).isNotZero(); |
| 222 | + assertThat(result.stdErr()).containsPattern(".*Running npm command.*NpmInstall.* failed with exit code: 1"); |
| 223 | + } |
60 | 224 | } |
0 commit comments