43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
package windows
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildCommandLine_HP_CMD_01(t *testing.T) {
|
|
t.Parallel()
|
|
cases := []struct {
|
|
name string
|
|
arguments []string
|
|
want string
|
|
}{
|
|
{"empty vector", nil, ""},
|
|
{"ordinary", []string{"-File", `C:\ProgramData\RVBox\work\run.ps1`}, `-File C:\ProgramData\RVBox\work\run.ps1`},
|
|
{"empty argument", []string{"-Command", ""}, `-Command ""`},
|
|
{"spaces", []string{"-File", `C:\path with spaces\run.ps1`}, `-File "C:\path with spaces\run.ps1"`},
|
|
{"embedded quote", []string{`say "hello"`}, `"say \"hello\""`},
|
|
{"trailing slash", []string{`C:\path with spaces\`}, `"C:\path with spaces\\"`},
|
|
{"unicode", []string{"-File", `C:\工作\脚本.ps1`}, `-File C:\工作\脚本.ps1`},
|
|
}
|
|
for _, test := range cases {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := BuildCommandLine(test.arguments)
|
|
if err != nil || got != test.want {
|
|
t.Fatalf("BuildCommandLine(%q) = (%q, %v), want %q", test.arguments, got, err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildCommandLineRejectsNUL_BH_CMD_01(t *testing.T) {
|
|
t.Parallel()
|
|
if _, err := BuildCommandLine([]string{"safe", "bad\x00argument"}); !errors.Is(err, ErrInvalidCommandLineArgument) {
|
|
t.Fatalf("NUL argument error = %v", err)
|
|
}
|
|
if _, err := BuildCommandLine([]string{string([]byte{0xff})}); !errors.Is(err, ErrInvalidCommandLineArgument) {
|
|
t.Fatalf("invalid UTF-8 argument error = %v", err)
|
|
}
|
|
}
|