/
env.go
34 lines (28 loc) · 852 Bytes
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package testexec
import (
"context"
"syscall"
"testing"
)
func Run[In, Out any](t testing.TB, prg ProgramHandle[In, Out], req In, resp Out, args ...string) {
err := CommandContext(t, prg, nil, req, resp, args...).Run()
if err != nil {
t.Errorf("program execute failed: %s", err)
}
}
func Command[In, Out any](t testing.TB, prg ProgramHandle[In, Out], req In, resp Out, args ...string) *Cmd {
return CommandContext(t, prg, nil, req, resp, args...)
}
func CommandContext[In, Out any](t testing.TB, prg ProgramHandle[In, Out], ctx context.Context, req In, resp Out, args ...string) *Cmd {
if !calledFromMain {
panic("to use testexec.Main must be called")
}
cmd := prg.exec(ctx, req, resp, args)
t.Cleanup(func() {
if cmd.Cmd.Process != nil {
_ = cmd.Cmd.Process.Signal(syscall.SIGTERM)
}
_ = cmd.Cmd.Wait()
})
return cmd
}