Skip to content

Commit

Permalink
set-props: write properties file as UTF-8 (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Oct 18, 2023
1 parent 4052788 commit 0033826
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package me.itzg.helpers.properties;

import com.fasterxml.jackson.core.type.TypeReference;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -80,7 +80,7 @@ public Integer call() throws Exception {
if (changes > 0) {
log.info("Created/updated {} propert{} in {}", changes, changes != 1 ? "ies":"y", propertiesFile);

try (OutputStream propsOut = Files.newOutputStream(propertiesFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
try (Writer propsOut = Files.newBufferedWriter(propertiesFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
properties.store(propsOut, String.format("Updated %s by mc-image-helper", Instant.now()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine;
import picocli.CommandLine.ExitCode;

Expand Down Expand Up @@ -174,9 +176,10 @@ void handlesModifiedCustomProperties() throws IOException {
.containsEntry("key2", "value2");
}

@Test
void handlesExistingUnicodePropertyValue() throws IOException {
final Path outputProperties = preparePropertiesFile("with-unicode.txt");
@ParameterizedTest
@ValueSource(strings = {"with-unicode.txt", "with-escapes.txt"})
void handlesExistingUnicodePropertyValue(String filename) throws IOException {
final Path outputProperties = preparePropertiesFile(filename);

final int exitCode = new CommandLine(new SetPropertiesCommand())
.execute(
Expand All @@ -188,7 +191,7 @@ void handlesExistingUnicodePropertyValue() throws IOException {

assertThat(outputProperties)
.content(StandardCharsets.UTF_8)
.containsIgnoringNewLines("motd=\\u00A7c\\u00A7lT\\u00A76\\u00A7le\\u00A7e\\u00A7ls\\u00A7a\\u00A7lt\\u00A73\\u00A7li\\u00A79\\u00A7ln\\u00A75\\u00A7lg \\u00A76\\u00A7l1\\u00A7e\\u00A7l2\\u00A7a\\u00A7l3")
.containsIgnoringNewLines("motd=§c§lT§6§le§e§ls§a§lt§3§li§9§ln§5§lg §6§l1§e§l2§a§l3")
.containsIgnoringNewLines("key1=value1");
}

Expand Down
1 change: 1 addition & 0 deletions src/test/resources/properties/with-escapes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
motd=\u00A7c\u00A7lT\u00A76\u00A7le\u00A7e\u00A7ls\u00A7a\u00A7lt\u00A73\u00A7li\u00A79\u00A7ln\u00A75\u00A7lg \u00A76\u00A7l1\u00A7e\u00A7l2\u00A7a\u00A7l3

0 comments on commit 0033826

Please sign in to comment.