2024-05-17 07:39:33 +00:00
|
|
|
package exec
|
2024-05-07 08:10:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCmdRun(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cmd *Cmd
|
|
|
|
wantErr bool
|
|
|
|
wantStdout string
|
|
|
|
wantStderr string
|
|
|
|
wantTrace string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "trace enabled",
|
|
|
|
cmd: &Cmd{
|
2024-05-17 07:39:33 +00:00
|
|
|
Trace: true,
|
2024-05-07 08:10:59 +00:00
|
|
|
Cmd: &exec.Cmd{
|
|
|
|
Path: "/usr/bin/echo",
|
|
|
|
Args: []string{"echo", "hello"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantTrace: "+ echo hello\n",
|
|
|
|
wantStdout: "hello\n",
|
|
|
|
},
|
|
|
|
{
|
2024-05-17 07:39:33 +00:00
|
|
|
name: "trace disabled",
|
2024-05-07 08:10:59 +00:00
|
|
|
cmd: &Cmd{
|
2024-05-17 07:39:33 +00:00
|
|
|
Trace: false,
|
2024-05-07 08:10:59 +00:00
|
|
|
Cmd: &exec.Cmd{
|
|
|
|
Path: "/usr/bin/echo",
|
|
|
|
Args: []string{"echo", "hello"},
|
|
|
|
},
|
|
|
|
},
|
2024-05-17 07:39:33 +00:00
|
|
|
wantStdout: "hello\n",
|
2024-05-07 08:10:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom env",
|
|
|
|
cmd: &Cmd{
|
2024-05-17 07:39:33 +00:00
|
|
|
Trace: true,
|
2024-05-07 08:10:59 +00:00
|
|
|
Cmd: &exec.Cmd{
|
|
|
|
Path: "/bin/sh",
|
|
|
|
Args: []string{"sh", "-c", "echo $TEST"},
|
|
|
|
Env: []string{"TEST=1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantTrace: "+ sh -c echo $TEST\n",
|
|
|
|
wantStdout: "1\n",
|
|
|
|
},
|
|
|
|
{
|
2024-05-17 07:39:33 +00:00
|
|
|
name: "custom stderr",
|
2024-05-07 08:10:59 +00:00
|
|
|
cmd: &Cmd{
|
2024-05-17 07:39:33 +00:00
|
|
|
Trace: true,
|
2024-05-07 08:10:59 +00:00
|
|
|
Cmd: &exec.Cmd{
|
|
|
|
Path: "/bin/sh",
|
2024-05-17 07:39:33 +00:00
|
|
|
Args: []string{"sh", "-c", "echo error >&2"},
|
|
|
|
Stderr: new(bytes.Buffer),
|
2024-05-07 08:10:59 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-17 07:39:33 +00:00
|
|
|
wantTrace: "+ sh -c echo error >&2\n",
|
|
|
|
wantStderr: "error\n",
|
2024-05-07 08:10:59 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-17 07:39:33 +00:00
|
|
|
name: "error",
|
2024-05-07 08:10:59 +00:00
|
|
|
cmd: &Cmd{
|
2024-05-17 07:39:33 +00:00
|
|
|
Trace: true,
|
2024-05-07 08:10:59 +00:00
|
|
|
Cmd: &exec.Cmd{
|
2024-05-17 07:39:33 +00:00
|
|
|
Path: "/invalid/path",
|
2024-05-07 08:10:59 +00:00
|
|
|
},
|
|
|
|
},
|
2024-05-17 07:39:33 +00:00
|
|
|
wantErr: true,
|
2024-05-07 08:10:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
traceBuf := new(bytes.Buffer)
|
|
|
|
stdoutBuf := new(bytes.Buffer)
|
|
|
|
stderrBuf := new(bytes.Buffer)
|
|
|
|
tt.cmd.TraceWriter = traceBuf
|
|
|
|
tt.cmd.Stdout = stdoutBuf
|
|
|
|
tt.cmd.Stderr = stderrBuf
|
|
|
|
|
|
|
|
err := tt.cmd.Run()
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.wantTrace, traceBuf.String())
|
|
|
|
assert.Equal(t, tt.wantStdout, stdoutBuf.String())
|
|
|
|
assert.Equal(t, tt.wantStderr, stderrBuf.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|