Fix test issues on Windows (#2693)

#### What type of PR is this?

/kind cleanup

#### What this PR does / why we need it:

Fix failing tests on Windows.

```bash
╰─$ ./gradlew check

> Task :checkstyleTest
Checkstyle rule violations were found. See the report at: file:///C:/Users/johnn/workspaces/halo-dev/halo/build/reports/checkstyle/test.html
Checkstyle files with violations: 8
Checkstyle violations by severity: [warning:20]


> Task :test
2022-11-11T14:19:41.265+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.277+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.281+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.285+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.289+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.298+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.301+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.304+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.306+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.309+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...
2022-11-11T14:19:41.311+08:00 DEBUG 5948 --- [ionShutdownHook] r.halo.app.metrics.CounterMeterHandler   : Persist counter meters to database before destroy...

BUILD SUCCESSFUL in 47s
7 actionable tasks: 2 executed, 5 up-to-date

```
#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/2675/head
John Niang 2022-11-11 18:52:11 +08:00 committed by GitHub
parent 2cd501955f
commit 8b9ea1d301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 35 deletions

View File

@ -55,6 +55,7 @@ public class PathUtils {
* *
* @param pathSegments Path segments to be combined * @param pathSegments Path segments to be combined
* @return the combined path * @return the combined path
* @apiNote This method doesn't work for Windows system currently.
*/ */
public static String combinePath(String... pathSegments) { public static String combinePath(String... pathSegments) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -3,7 +3,6 @@ package run.halo.app.plugin;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.pf4j.DevelopmentPluginClasspath; import org.pf4j.DevelopmentPluginClasspath;
@ -14,7 +13,6 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import run.halo.app.core.extension.Plugin; import run.halo.app.core.extension.Plugin;
import run.halo.app.extension.Unstructured; import run.halo.app.extension.Unstructured;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.infra.utils.YamlUnstructuredLoader; import run.halo.app.infra.utils.YamlUnstructuredLoader;
/** /**
@ -102,9 +100,7 @@ public class YamlPluginFinder {
protected Path getManifestPath(Path pluginPath, String propertiesFileName) { protected Path getManifestPath(Path pluginPath, String propertiesFileName) {
if (Files.isDirectory(pluginPath)) { if (Files.isDirectory(pluginPath)) {
for (String location : PLUGIN_CLASSPATH.getClassesDirectories()) { for (String location : PLUGIN_CLASSPATH.getClassesDirectories()) {
String s = PathUtils.combinePath(pluginPath.toString(), var path = pluginPath.resolve(location).resolve(propertiesFileName);
location, propertiesFileName);
Path path = Paths.get(s);
Resource propertyResource = new FileSystemResource(path); Resource propertyResource = new FileSystemResource(path);
if (propertyResource.exists()) { if (propertyResource.exists()) {
return path; return path;

View File

@ -1,8 +1,8 @@
package run.halo.app.core.extension; package run.halo.app.core.extension;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.json.JSONException; import org.json.JSONException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONAssert;
@ -25,7 +25,7 @@ class SettingTest {
apiVersion: v1alpha1 apiVersion: v1alpha1
kind: Setting kind: Setting
metadata: metadata:
name: setting-name name: setting-name
spec: spec:
forms: forms:
- group: basic - group: basic
@ -47,8 +47,9 @@ class SettingTest {
name: color name: color
validation: required validation: required
"""; """;
List<Unstructured> unstructureds = var unstructureds = new YamlUnstructuredLoader(
new YamlUnstructuredLoader(new InMemoryResource(settingYaml)).load(); new InMemoryResource(settingYaml.getBytes(UTF_8), "In-memory setting YAML"))
.load();
assertThat(unstructureds).hasSize(1); assertThat(unstructureds).hasSize(1);
Unstructured unstructured = unstructureds.get(0); Unstructured unstructured = unstructureds.get(0);

View File

@ -34,7 +34,8 @@ class PluginStartedListenerTest {
Set<String> extensionResources = Set<String> extensionResources =
PluginStartedListener.PluginExtensionLoaderUtils.lookupFromClasses(tempPluginPath); PluginStartedListener.PluginExtensionLoaderUtils.lookupFromClasses(tempPluginPath);
assertThat(extensionResources).containsAll(Set.of("extensions/roles.yaml")); assertThat(extensionResources)
.containsAll(Set.of(Path.of("extensions/roles.yaml").toString()));
} }
@Test @Test

View File

@ -105,10 +105,9 @@ class YamlPluginFinderTest {
@Test @Test
void findFailedWhenFileNotFound() { void findFailedWhenFileNotFound() {
Path test = Paths.get("/tmp"); var test = Paths.get("");
assertThatThrownBy(() -> { assertThatThrownBy(() -> pluginFinder.find(test))
pluginFinder.find(test); .isInstanceOf(PluginRuntimeException.class)
}).isInstanceOf(PluginRuntimeException.class)
.hasMessage("Unable to find plugin descriptor file: plugin.yaml"); .hasMessage("Unable to find plugin descriptor file: plugin.yaml");
} }

View File

@ -1,6 +1,6 @@
package run.halo.app.theme; package run.halo.app.theme;
import java.nio.file.Paths; import java.nio.file.Path;
import org.json.JSONException; import org.json.JSONException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONAssert;
@ -16,20 +16,20 @@ class ThemeContextTest {
@Test @Test
void constructorBuilderTest() throws JSONException { void constructorBuilderTest() throws JSONException {
ThemeContext testTheme = ThemeContext.builder() var path = Path.of("/tmp/themes/testTheme");
var testTheme = ThemeContext.builder()
.name("testTheme") .name("testTheme")
.path(Paths.get("/tmp/themes/testTheme")) .path(path)
.active(true) .active(true)
.build(); .build();
String s = JsonUtils.objectToJson(testTheme); var got = JsonUtils.objectToJson(testTheme);
JSONAssert.assertEquals(""" var expect = String.format("""
{ {
"name": "testTheme", "name": "testTheme",
"path": "file:///tmp/themes/testTheme", "path": "%s",
"active": true "active": true
} }
""", """, path.toUri());
s, JSONAssert.assertEquals(expect, got, false);
false);
} }
} }

View File

@ -3,8 +3,9 @@ package run.halo.app.theme.message;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Paths; import java.nio.file.Path;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -25,7 +26,7 @@ class ThemeMessageResolutionUtilsTest {
} }
@Test @Test
void resolveMessagesForTemplateForDefault() { void resolveMessagesForTemplateForDefault() throws URISyntaxException {
Map<String, String> properties = Map<String, String> properties =
ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.CHINESE, getTheme()); ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.CHINESE, getTheme());
assertThat(properties).hasSize(1); assertThat(properties).hasSize(1);
@ -33,7 +34,7 @@ class ThemeMessageResolutionUtilsTest {
} }
@Test @Test
void resolveMessagesForTemplateForEnglish() { void resolveMessagesForTemplateForEnglish() throws URISyntaxException {
Map<String, String> properties = Map<String, String> properties =
ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.ENGLISH, getTheme()); ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.ENGLISH, getTheme());
assertThat(properties).hasSize(1); assertThat(properties).hasSize(1);
@ -48,10 +49,10 @@ class ThemeMessageResolutionUtilsTest {
assertThat(s).isEqualTo("Welcome Halo to the index"); assertThat(s).isEqualTo("Welcome Halo to the index");
} }
ThemeContext getTheme() { ThemeContext getTheme() throws URISyntaxException {
return ThemeContext.builder() return ThemeContext.builder()
.name("default") .name("default")
.path(Paths.get(defaultThemeUrl.getPath())) .path(Path.of(defaultThemeUrl.toURI()))
.active(true) .active(true)
.build(); .build();
} }

View File

@ -1,8 +1,9 @@
package run.halo.app.theme.message; package run.halo.app.theme.message;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Paths; import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -45,7 +46,7 @@ public class ThemeMessageResolverIntegrationTest {
private WebTestClient webTestClient; private WebTestClient webTestClient;
@BeforeEach @BeforeEach
void setUp() throws FileNotFoundException { void setUp() throws FileNotFoundException, URISyntaxException {
defaultThemeUrl = ResourceUtils.getURL("classpath:themes/default"); defaultThemeUrl = ResourceUtils.getURL("classpath:themes/default");
otherThemeUrl = ResourceUtils.getURL("classpath:themes/other"); otherThemeUrl = ResourceUtils.getURL("classpath:themes/other");
@ -126,7 +127,7 @@ public class ThemeMessageResolverIntegrationTest {
} }
@Test @Test
void switchTheme() { void switchTheme() throws URISyntaxException {
webTestClient.get() webTestClient.get()
.uri("/index?language=zh") .uri("/index?language=zh")
.exchange() .exchange()
@ -185,18 +186,18 @@ public class ThemeMessageResolverIntegrationTest {
"""); """);
} }
ThemeContext createDefaultContext() { ThemeContext createDefaultContext() throws URISyntaxException {
return ThemeContext.builder() return ThemeContext.builder()
.name("default") .name("default")
.path(Paths.get(defaultThemeUrl.getPath())) .path(Path.of(defaultThemeUrl.toURI()))
.active(true) .active(true)
.build(); .build();
} }
ThemeContext createOtherContext() { ThemeContext createOtherContext() throws URISyntaxException {
return ThemeContext.builder() return ThemeContext.builder()
.name("other") .name("other")
.path(Paths.get(otherThemeUrl.getPath())) .path(Path.of(otherThemeUrl.toURI()))
.active(false) .active(false)
.build(); .build();
} }