Skip to content

Commit

Permalink
Fix test issues on Windows (#2693)
Browse files Browse the repository at this point in the history
#### 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
```
  • Loading branch information
JohnNiang authored Nov 11, 2022
1 parent 2cd5019 commit 8b9ea1d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/main/java/run/halo/app/infra/utils/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static boolean isAbsoluteUri(final String uriString) {
*
* @param pathSegments Path segments to be combined
* @return the combined path
* @apiNote This method doesn't work for Windows system currently.
*/
public static String combinePath(String... pathSegments) {
StringBuilder sb = new StringBuilder();
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/run/halo/app/plugin/YamlPluginFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.DevelopmentPluginClasspath;
Expand All @@ -14,7 +13,6 @@
import org.springframework.core.io.Resource;
import run.halo.app.core.extension.Plugin;
import run.halo.app.extension.Unstructured;
import run.halo.app.infra.utils.PathUtils;
import run.halo.app.infra.utils.YamlUnstructuredLoader;

/**
Expand Down Expand Up @@ -102,9 +100,7 @@ protected Plugin unstructuredToPlugin(Resource propertyResource) {
protected Path getManifestPath(Path pluginPath, String propertiesFileName) {
if (Files.isDirectory(pluginPath)) {
for (String location : PLUGIN_CLASSPATH.getClassesDirectories()) {
String s = PathUtils.combinePath(pluginPath.toString(),
location, propertiesFileName);
Path path = Paths.get(s);
var path = pluginPath.resolve(location).resolve(propertiesFileName);
Resource propertyResource = new FileSystemResource(path);
if (propertyResource.exists()) {
return path;
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/run/halo/app/core/extension/SettingTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package run.halo.app.core.extension;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand All @@ -25,7 +25,7 @@ void setting() throws JSONException {
apiVersion: v1alpha1
kind: Setting
metadata:
name: setting-name
name: setting-name
spec:
forms:
- group: basic
Expand All @@ -47,8 +47,9 @@ void setting() throws JSONException {
name: color
validation: required
""";
List<Unstructured> unstructureds =
new YamlUnstructuredLoader(new InMemoryResource(settingYaml)).load();
var unstructureds = new YamlUnstructuredLoader(
new InMemoryResource(settingYaml.getBytes(UTF_8), "In-memory setting YAML"))
.load();
assertThat(unstructureds).hasSize(1);
Unstructured unstructured = unstructureds.get(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void lookupFromClasses() throws IOException {

Set<String> extensionResources =
PluginStartedListener.PluginExtensionLoaderUtils.lookupFromClasses(tempPluginPath);
assertThat(extensionResources).containsAll(Set.of("extensions/roles.yaml"));
assertThat(extensionResources)
.containsAll(Set.of(Path.of("extensions/roles.yaml").toString()));
}

@Test
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/run/halo/app/plugin/YamlPluginFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ void unstructuredToPluginTest() throws JSONException {

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

Expand Down
18 changes: 9 additions & 9 deletions src/test/java/run/halo/app/theme/ThemeContextTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package run.halo.app.theme;

import java.nio.file.Paths;
import java.nio.file.Path;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand All @@ -16,20 +16,20 @@ class ThemeContextTest {

@Test
void constructorBuilderTest() throws JSONException {
ThemeContext testTheme = ThemeContext.builder()
var path = Path.of("/tmp/themes/testTheme");
var testTheme = ThemeContext.builder()
.name("testTheme")
.path(Paths.get("/tmp/themes/testTheme"))
.path(path)
.active(true)
.build();
String s = JsonUtils.objectToJson(testTheme);
JSONAssert.assertEquals("""
var got = JsonUtils.objectToJson(testTheme);
var expect = String.format("""
{
"name": "testTheme",
"path": "file:///tmp/themes/testTheme",
"path": "%s",
"active": true
}
""",
s,
false);
""", path.toUri());
JSONAssert.assertEquals(expect, got, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -25,15 +26,15 @@ void setUp() throws FileNotFoundException {
}

@Test
void resolveMessagesForTemplateForDefault() {
void resolveMessagesForTemplateForDefault() throws URISyntaxException {
Map<String, String> properties =
ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.CHINESE, getTheme());
assertThat(properties).hasSize(1);
assertThat(properties).containsEntry("index.welcome", "欢迎来到首页");
}

@Test
void resolveMessagesForTemplateForEnglish() {
void resolveMessagesForTemplateForEnglish() throws URISyntaxException {
Map<String, String> properties =
ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.ENGLISH, getTheme());
assertThat(properties).hasSize(1);
Expand All @@ -48,10 +49,10 @@ void messageFormat() {
assertThat(s).isEqualTo("Welcome Halo to the index");
}

ThemeContext getTheme() {
ThemeContext getTheme() throws URISyntaxException {
return ThemeContext.builder()
.name("default")
.path(Paths.get(defaultThemeUrl.getPath()))
.path(Path.of(defaultThemeUrl.toURI()))
.active(true)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package run.halo.app.theme.message;

import java.io.FileNotFoundException;
import java.net.URISyntaxException;
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.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class ThemeMessageResolverIntegrationTest {
private WebTestClient webTestClient;

@BeforeEach
void setUp() throws FileNotFoundException {
void setUp() throws FileNotFoundException, URISyntaxException {
defaultThemeUrl = ResourceUtils.getURL("classpath:themes/default");
otherThemeUrl = ResourceUtils.getURL("classpath:themes/other");

Expand Down Expand Up @@ -126,7 +127,7 @@ void shouldUseDefaultWhenLanguageNotSupport() {
}

@Test
void switchTheme() {
void switchTheme() throws URISyntaxException {
webTestClient.get()
.uri("/index?language=zh")
.exchange()
Expand Down Expand Up @@ -185,18 +186,18 @@ void switchTheme() {
""");
}

ThemeContext createDefaultContext() {
ThemeContext createDefaultContext() throws URISyntaxException {
return ThemeContext.builder()
.name("default")
.path(Paths.get(defaultThemeUrl.getPath()))
.path(Path.of(defaultThemeUrl.toURI()))
.active(true)
.build();
}

ThemeContext createOtherContext() {
ThemeContext createOtherContext() throws URISyntaxException {
return ThemeContext.builder()
.name("other")
.path(Paths.get(otherThemeUrl.getPath()))
.path(Path.of(otherThemeUrl.toURI()))
.active(false)
.build();
}
Expand Down

0 comments on commit 8b9ea1d

Please sign in to comment.