2025-08-18 14:24:31 +02:00
|
|
|
package wand
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
2025-08-26 03:21:35 +02:00
|
|
|
"io"
|
2025-08-18 14:24:31 +02:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-26 03:21:35 +02:00
|
|
|
type testCase struct {
|
|
|
|
|
impl any
|
|
|
|
|
stdin string
|
|
|
|
|
conf string
|
|
|
|
|
env string
|
|
|
|
|
command string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testExec(test testCase, err string, expect ...string) func(*testing.T) {
|
2025-08-18 14:24:31 +02:00
|
|
|
return func(t *testing.T) {
|
|
|
|
|
var exitCode int
|
|
|
|
|
exit := func(code int) { exitCode = code }
|
2025-08-26 03:21:35 +02:00
|
|
|
|
|
|
|
|
var stdinr io.Reader
|
|
|
|
|
if test.stdin != "" {
|
|
|
|
|
stdinr = bytes.NewBuffer([]byte(test.stdin))
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 14:24:31 +02:00
|
|
|
stdout := bytes.NewBuffer(nil)
|
|
|
|
|
stderr := bytes.NewBuffer(nil)
|
2025-08-26 03:21:35 +02:00
|
|
|
cmd := wrap(test.impl)
|
|
|
|
|
e := strings.Split(test.env, ";")
|
|
|
|
|
a := strings.Split(test.command, " ")
|
|
|
|
|
exec(stdinr, stdout, stderr, exit, cmd, Config{test: test.conf}, e, a)
|
2025-08-18 14:24:31 +02:00
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 03:21:35 +02:00
|
|
|
output := stdout.String()
|
|
|
|
|
if output[len(output) - 1] != '\n' {
|
|
|
|
|
output = output + "\n"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if output != strings.Join(expstr, "\n")+"\n" {
|
2025-08-18 14:24:31 +02:00
|
|
|
t.Fatal("unexpected output:", stdout.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|