|
| 1 | +/* |
| 2 | + * Copyright 2025 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.npm; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import com.diffplug.spotless.ResourceHarness; |
| 28 | + |
| 29 | +class NodeServerLayoutTest extends ResourceHarness { |
| 30 | + |
| 31 | + @Test |
| 32 | + void itCalculatesSameNodeModulesDirForSameContent() throws IOException { |
| 33 | + File testDir = newFolder("build"); |
| 34 | + String packageJsonContent = prettierPackageJson(Collections.emptyMap()); |
| 35 | + String serveJsContent = "fun main() { console.log('Hello, world!'); }"; |
| 36 | + NodeServerLayout layout1 = new NodeServerLayout(testDir, packageJsonContent, serveJsContent); |
| 37 | + NodeServerLayout layout2 = new NodeServerLayout(testDir, packageJsonContent, serveJsContent); |
| 38 | + |
| 39 | + assertThat(layout1.nodeModulesDir()).isEqualTo(layout2.nodeModulesDir()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void itCalculatesDifferentNodeModulesDirForDifferentPackageJson() throws IOException { |
| 44 | + File testDir = newFolder("build"); |
| 45 | + String packageJsonContent1 = prettierPackageJson(Collections.singletonMap("prettier-plugin-xy", "^2.0.0")); |
| 46 | + String packageJsonContent2 = prettierPackageJson(Collections.singletonMap("prettier-plugin-xy", "^2.1.0")); |
| 47 | + String serveJsContent = "fun main() { console.log('Hello, world!'); }"; |
| 48 | + |
| 49 | + NodeServerLayout layout1 = new NodeServerLayout(testDir, packageJsonContent1, serveJsContent); |
| 50 | + NodeServerLayout layout2 = new NodeServerLayout(testDir, packageJsonContent2, serveJsContent); |
| 51 | + |
| 52 | + assertThat(layout1.nodeModulesDir()).isNotEqualTo(layout2.nodeModulesDir()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void itCalculatesDifferentNodeModulesDirForDifferentServeJs() throws IOException { |
| 57 | + File testDir = newFolder("build"); |
| 58 | + String packageJsonContent = prettierPackageJson(Collections.emptyMap()); |
| 59 | + String serveJsContent1 = "fun main() { console.log('Hello, world!'); }"; |
| 60 | + String serveJsContent2 = "fun main() { console.log('Goodbye, world!'); }"; |
| 61 | + |
| 62 | + NodeServerLayout layout1 = new NodeServerLayout(testDir, packageJsonContent, serveJsContent1); |
| 63 | + NodeServerLayout layout2 = new NodeServerLayout(testDir, packageJsonContent, serveJsContent2); |
| 64 | + |
| 65 | + assertThat(layout1.nodeModulesDir()).isNotEqualTo(layout2.nodeModulesDir()); |
| 66 | + } |
| 67 | + |
| 68 | + static String prettierPackageJson(Map<String, String> dependencies) { |
| 69 | + String templateContent = NpmResourceHelper.readUtf8StringFromClasspath(NodeServerLayoutTest.class, "/com/diffplug/spotless/npm/prettier-package.json"); |
| 70 | + String dependenciesList = dependencies.entrySet().stream() |
| 71 | + .map(entry -> String.format("\"%s\": \"%s\"", entry.getKey(), entry.getValue())) |
| 72 | + .reduce((a, b) -> a + ",\n " + b) |
| 73 | + .orElse(""); |
| 74 | + |
| 75 | + return templateContent.replace("${devDependencies}", dependenciesList); |
| 76 | + } |
| 77 | +} |
0 commit comments