| 1 | package hook |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "golang.org/x/text/encoding/simplifiedchinese" |
| 8 | ) |
| 9 | |
| 10 | // cmd.exe stderr cut short inside a character is still code-page text. |
| 11 | func TestDecodeHookOutputReadsTruncatedCodePageText(t *testing.T) { |
| 12 | gb, _ := simplifiedchinese.GB18030.NewEncoder().String("'sh' 不是内部或外部命令,也不是可运行的程序") |
| 13 | raw := []byte(gb)[:len(gb)-1] |
| 14 | if got := decodeHookOutput(raw, true); !strings.HasPrefix(got, "'sh' 不是内部或外部命令") { |
| 15 | t.Fatalf("decoded hook stderr = %q", got) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | // CP936 output carrying its single-byte euro is read as CP936, not passed |
| 20 | // through as raw bytes. |
| 21 | func TestDecodeHookOutputReadsCP936Euro(t *testing.T) { |
| 22 | want := "'sh' 不是内部或外部命令 €" |
| 23 | raw, err := simplifiedchinese.GBK.NewEncoder().String(want) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if got := decodeHookOutput([]byte(raw), false); got != want { |
| 28 | t.Fatalf("decoded hook stderr = %q, want %q", got, want) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Output that ends where the hook stopped writing, not where a bound cut it, |
| 33 | // keeps a code-page character at either edge. |
| 34 | func TestDecodeHookOutputKeepsUncutCodePageEdges(t *testing.T) { |
| 35 | cases := map[string]string{ |
| 36 | "\xbc\xdb": "价", |
| 37 | "\xb0\xa1\r\n": "啊", |
| 38 | "ok \xe4\xa1": "ok 洹", |
| 39 | "\xb0\xa1 ok": "啊 ok", |
| 40 | } |
| 41 | for raw, want := range cases { |
| 42 | if got := decodeHookOutput([]byte(raw), false); got != want { |
| 43 | t.Fatalf("decodeHookOutput(% x) = %q, want %q", raw, got, want) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 |