wand/exec_test.go
2025-08-18 14:24:31 +02:00

46 lines
978 B
Go

package wand
import (
"bytes"
"fmt"
"strings"
"testing"
)
func testExec(impl any, env, commandLine, err string, expect ...string) func(*testing.T) {
return func(t *testing.T) {
var exitCode int
exit := func(code int) { exitCode = code }
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
cmd := wrap(impl)
e := strings.Split(env, ";")
a := strings.Split(commandLine, " ")
exec(stdout, stderr, exit, cmd, e, a)
if exitCode != 0 && err == "" {
t.Fatal("non-zero exit code:", stderr.String())
}
if err != "" && exitCode == 0 {
t.Fatal("failed to fail")
}
if err != "" && !strings.Contains(stderr.String(), err) {
t.Fatal("expected error not received:", stderr.String())
}
if exitCode != 0 {
return
}
var expstr []string
for _, e := range expect {
expstr = append(expstr, fmt.Sprint(e))
}
if stdout.String() != strings.Join(expstr, "\n")+"\n" {
t.Fatal("unexpected output:", stdout.String())
}
}
}