Skip to content

Commit 2095f61

Browse files
committed
test(exec): 补 UTF-8 输出解码回归测试(POSIX printf 八进制转义,Java 源码双写反斜杠)
1 parent 01a732a commit 2095f61

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/test/java/io/github/easy4j/opencli/core/OpenCliExecutorFullTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.*;
44
import io.github.easy4j.opencli.OpenCliExecutionTarget;
55
import io.github.easy4j.opencli.OpenCliProperties;
6+
import java.util.Arrays;
67
import org.apache.commons.exec.CommandLine;
78
import org.junit.jupiter.api.Test;
89

@@ -31,6 +32,23 @@ void shouldAppendQuotedKeyValueWithTrailingEquals() {
3132
assertTrue(args[args.length - 1].contains("--key=val"));
3233
}
3334

35+
@Test
36+
void shouldDecodeUtf8OutputRegardlessOfPlatformCharset() {
37+
OpenCliProperties props = new OpenCliProperties();
38+
props.setExecutable("/bin/sh");
39+
props.setCommandTimeoutMillis(30_000L);
40+
OpenCliExecutor executor = new OpenCliExecutor(props);
41+
42+
// 你 = \344\275\240, 好 = \345\245\275(POSIX printf 八进制转义)。
43+
// Java 源码里反斜杠必须双写,否则编译器会把 \344 当成八进制转义吃掉。
44+
OpenCliResult result = executor.invoke(Arrays.asList(
45+
"-c", "printf '\\344\\275\\240\\345\\245\\275'"));
46+
47+
assertTrue(result.isSuccess(), "stderr=" + result.getStderr());
48+
assertEquals("你好", result.getStdout().trim(),
49+
"UTF-8 bytes must decode with explicit UTF-8, not the platform charset");
50+
}
51+
3452
@Test
3553
void shouldConfigureRemoteMode() {
3654
OpenCliProperties props = new OpenCliProperties();

0 commit comments

Comments
 (0)