|
| 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.npm; |
| 17 | + |
| 18 | +import static com.diffplug.spotless.maven.npm.AbstractNpmFormatterStepFactory.SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.FileVisitResult; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.nio.file.SimpleFileVisitor; |
| 27 | +import java.nio.file.attribute.BasicFileAttributes; |
| 28 | + |
| 29 | +import org.assertj.core.api.Assertions; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import com.diffplug.spotless.ProcessRunner.Result; |
| 33 | +import com.diffplug.spotless.maven.MavenIntegrationHarness; |
| 34 | +import com.diffplug.spotless.tag.NpmTest; |
| 35 | + |
| 36 | +@NpmTest |
| 37 | +public class NpmStepsWithNpmInstallCacheTest extends MavenIntegrationHarness { |
| 38 | + |
| 39 | + // TODO implement tests without cache and with various cache paths |
| 40 | + // using only prettier is enough since the other cases are covered by gradle-side integration tests |
| 41 | + |
| 42 | + @Test |
| 43 | + void prettierTypescriptWithoutCache() throws Exception { |
| 44 | + String suffix = "ts"; |
| 45 | + writePomWithPrettierSteps("**/*." + suffix, |
| 46 | + "<prettier>", |
| 47 | + " <prettierVersion>1.16.4</prettierVersion>", |
| 48 | + " <configFile>.prettierrc.yml</configFile>", |
| 49 | + "</prettier>"); |
| 50 | + Result result = run("typescript", suffix); |
| 51 | + Assertions.assertThat(result.stdOutUtf8()).doesNotContain("Caching node_modules for").doesNotContain("Using cached node_modules for"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void prettierTypescriptWithDefaultCache() throws Exception { |
| 56 | + String suffix = "ts"; |
| 57 | + writePomWithPrettierSteps("**/*." + suffix, |
| 58 | + "<prettier>", |
| 59 | + " <prettierVersion>1.16.4</prettierVersion>", |
| 60 | + " <configFile>.prettierrc.yml</configFile>", |
| 61 | + " <npmInstallCache>true</npmInstallCache>", |
| 62 | + "</prettier>"); |
| 63 | + Result result = run("typescript", suffix); |
| 64 | + Assertions.assertThat(result.stdOutUtf8()) |
| 65 | + .contains("Caching node_modules for") |
| 66 | + .contains(SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME) |
| 67 | + .doesNotContain("Using cached node_modules for"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void prettierTypescriptWithDefaultCacheIsReusedOnSecondRun() throws Exception { |
| 72 | + String suffix = "ts"; |
| 73 | + writePomWithPrettierSteps("**/*." + suffix, |
| 74 | + "<prettier>", |
| 75 | + " <prettierVersion>1.16.4</prettierVersion>", |
| 76 | + " <configFile>.prettierrc.yml</configFile>", |
| 77 | + " <npmInstallCache>true</npmInstallCache>", |
| 78 | + "</prettier>"); |
| 79 | + Result result1 = run("typescript", suffix); |
| 80 | + Assertions.assertThat(result1.stdOutUtf8()) |
| 81 | + .contains("Caching node_modules for") |
| 82 | + .contains(SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME) |
| 83 | + .doesNotContain("Using cached node_modules for"); |
| 84 | + |
| 85 | + // recursively delete target folder to simulate a fresh run (except the default cache folder) |
| 86 | + recursiveDelete(Paths.get(rootFolder().getAbsolutePath(), "target"), SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME); |
| 87 | + |
| 88 | + Result result2 = run("typescript", suffix); |
| 89 | + Assertions.assertThat(result2.stdOutUtf8()) |
| 90 | + .doesNotContain("Caching node_modules for") |
| 91 | + .contains(SPOTLESS_NPM_INSTALL_CACHE_DEFAULT_NAME) |
| 92 | + .contains("Using cached node_modules for"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void prettierTypescriptWithSpecificCache() throws Exception { |
| 97 | + String suffix = "ts"; |
| 98 | + File cacheDir = newFolder("cache-prettier-1"); |
| 99 | + writePomWithPrettierSteps("**/*." + suffix, |
| 100 | + "<prettier>", |
| 101 | + " <prettierVersion>1.16.4</prettierVersion>", |
| 102 | + " <configFile>.prettierrc.yml</configFile>", |
| 103 | + " <npmInstallCache>" + cacheDir.getAbsolutePath() + "</npmInstallCache>", |
| 104 | + "</prettier>"); |
| 105 | + Result result = run("typescript", suffix); |
| 106 | + Assertions.assertThat(result.stdOutUtf8()) |
| 107 | + .contains("Caching node_modules for") |
| 108 | + .contains(Path.of(cacheDir.getAbsolutePath()).toAbsolutePath().toString()) |
| 109 | + .doesNotContain("Using cached node_modules for"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void prettierTypescriptWithSpecificCacheIsUsedOnSecondRun() throws Exception { |
| 114 | + String suffix = "ts"; |
| 115 | + File cacheDir = newFolder("cache-prettier-1"); |
| 116 | + writePomWithPrettierSteps("**/*." + suffix, |
| 117 | + "<prettier>", |
| 118 | + " <prettierVersion>1.16.4</prettierVersion>", |
| 119 | + " <configFile>.prettierrc.yml</configFile>", |
| 120 | + " <npmInstallCache>" + cacheDir.getAbsolutePath() + "</npmInstallCache>", |
| 121 | + "</prettier>"); |
| 122 | + Result result1 = run("typescript", suffix); |
| 123 | + Assertions.assertThat(result1.stdOutUtf8()) |
| 124 | + .contains("Caching node_modules for") |
| 125 | + .contains(Path.of(cacheDir.getAbsolutePath()).toAbsolutePath().toString()) |
| 126 | + .doesNotContain("Using cached node_modules for"); |
| 127 | + |
| 128 | + // recursively delete target folder to simulate a fresh run |
| 129 | + recursiveDelete(Paths.get(rootFolder().getAbsolutePath(), "target"), null); |
| 130 | + |
| 131 | + Result result2 = run("typescript", suffix); |
| 132 | + Assertions.assertThat(result2.stdOutUtf8()) |
| 133 | + .doesNotContain("Caching node_modules for") |
| 134 | + .contains(Path.of(cacheDir.getAbsolutePath()).toAbsolutePath().toString()) |
| 135 | + .contains("Using cached node_modules for"); |
| 136 | + } |
| 137 | + |
| 138 | + private void recursiveDelete(Path path, String exclusion) throws IOException { |
| 139 | + Files.walkFileTree(path, new RecursiveDelete(exclusion)); |
| 140 | + } |
| 141 | + |
| 142 | + private Result run(String kind, String suffix) throws IOException, InterruptedException { |
| 143 | + String path = prepareRun(kind, suffix); |
| 144 | + Result result = mavenRunner().withArguments("spotless:apply").runNoError(); |
| 145 | + assertFile(path).sameAsResource("npm/prettier/filetypes/" + kind + "/" + kind + ".clean"); |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + private String prepareRun(String kind, String suffix) throws IOException { |
| 150 | + String configPath = ".prettierrc.yml"; |
| 151 | + setFile(configPath).toResource("npm/prettier/filetypes/" + kind + "/" + ".prettierrc.yml"); |
| 152 | + String path = "src/main/" + kind + "/test." + suffix; |
| 153 | + setFile(path).toResource("npm/prettier/filetypes/" + kind + "/" + kind + ".dirty"); |
| 154 | + return path; |
| 155 | + } |
| 156 | + |
| 157 | + private static class RecursiveDelete extends SimpleFileVisitor<Path> { |
| 158 | + private final String exclusionDirectory; |
| 159 | + |
| 160 | + public RecursiveDelete(String exclusionDirectory) { |
| 161 | + this.exclusionDirectory = exclusionDirectory; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 166 | + if (exclusionDirectory != null && dir.toFile().getName().equals(exclusionDirectory)) { |
| 167 | + return FileVisitResult.SKIP_SUBTREE; |
| 168 | + } |
| 169 | + return super.preVisitDirectory(dir, attrs); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 174 | + Files.delete(file); |
| 175 | + return super.visitFile(file, attrs); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 180 | + if (dir.toFile().listFiles().length != 0) { |
| 181 | + // skip non-empty dir |
| 182 | + return super.postVisitDirectory(dir, exc); |
| 183 | + } |
| 184 | + Files.delete(dir); |
| 185 | + return super.postVisitDirectory(dir, exc); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments