Skip to content

Commit 96fdf89

Browse files
ensure powershell commands return utf-8 output
1 parent 4d9a5bb commit 96fdf89

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎checks/cli.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ func runCLICommandWithOutputLimit(
9494
result.FinalCommand = finalCommand
9595
result.Command = command
9696

97+
if shell.commandFlag == "-Command" {
98+
finalCommand = "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false); " + finalCommand
99+
}
97100
cmd := exec.Command(shell.path, shell.commandFlag, finalCommand)
98101

99102
cmd.Env = append(os.Environ(), "LANG=en_US.UTF-8")

‎checks/cli_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package checks
22

33
import (
4+
"encoding/json"
45
"runtime"
56
"testing"
67

@@ -167,3 +168,29 @@ func TestParseStdoutVariablesRejectsInvalidConfiguration(t *testing.T) {
167168
})
168169
}
169170
}
171+
172+
func TestRunCLICommandPowerShellUTF8(t *testing.T) {
173+
shell := defaultShell()
174+
if runtime.GOOS != "windows" {
175+
var err error
176+
shell, err = resolveShell("pwsh")
177+
if err != nil {
178+
t.Skipf("PowerShell is unavailable: %v", err)
179+
}
180+
}
181+
182+
result := runCLICommand(api.CLIStepCLICommand{
183+
Command: `'• Żółć' | ConvertTo-Json -Compress`,
184+
}, map[string]string{}, shell)
185+
if result.Err != "" || result.ExitCode != 0 {
186+
t.Fatalf("error = %q, exit code = %d, stderr = %q", result.Err, result.ExitCode, result.Stderr)
187+
}
188+
189+
var got string
190+
if err := json.Unmarshal([]byte(result.Stdout), &got); err != nil {
191+
t.Fatalf("invalid JSON output %q: %v", result.Stdout, err)
192+
}
193+
if got != "• Żółć" {
194+
t.Fatalf("decoded output = %q, want %q", got, "• Żółć")
195+
}
196+
}

0 commit comments

Comments
 (0)