-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
142 lines (128 loc) · 3.13 KB
/
command_test.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package fab
import (
"bytes"
"context"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestCommand(t *testing.T) {
t.Parallel()
tmpdir, err := os.MkdirTemp("", "fab")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
var (
con = NewController("")
ctx = context.Background()
)
ctx = WithVerbose(ctx, true)
hw, err := os.ReadFile("_testdata/hw")
if err != nil {
t.Fatal(err)
}
f1 := filepath.Join(tmpdir, "f1")
t.Run("stdoutfile", func(t *testing.T) {
c1 := &Command{Cmd: "cat", Args: []string{"_testdata/hw"}, StdoutFile: f1}
if err = con.Run(ctx, c1); err != nil {
t.Fatal(err)
}
got1, err := os.ReadFile(f1)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got1, hw) {
t.Errorf("got %s, want %s", string(got1), string(hw))
}
})
t.Run("stdoutfile_append", func(t *testing.T) {
hwhw := append([]byte{}, hw...)
hwhw = append(hwhw, hw...)
c2 := &Command{Cmd: "cat", Args: []string{"_testdata/hw"}, StdoutFile: ">>" + f1}
if err = con.Run(ctx, c2); err != nil {
t.Fatal(err)
}
got2, err := os.ReadFile(f1)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got2, hwhw) {
t.Errorf("got %s, want %s", string(got2), string(hwhw))
}
})
const dne = "_a_file_that_does_not_exist_"
var (
f3 = filepath.Join(tmpdir, "f3")
f3size int64
)
t.Run("stderrfile", func(t *testing.T) {
c3 := &Command{Cmd: "cat", Args: []string{dne}, StderrFile: f3}
err = con.Run(ctx, c3)
if err == nil { // sic
t.Fatal("got no error but expected one")
}
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatal(err)
}
// Make sure f3 exists and has non-zero size.
info, err := os.Stat(f3)
if err != nil {
t.Fatal(err)
}
f3size = info.Size()
if f3size == 0 {
t.Error("f3 exists but has size zero")
}
})
t.Run("stderrfile_append", func(t *testing.T) {
c4 := &Command{Cmd: "cat", Args: []string{dne}, StderrFile: ">>" + f3}
err = con.Run(ctx, c4)
if err == nil { // sic
t.Fatal("got no error but expected one")
}
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatal(err)
}
// Make sure f3 has a greater size than before.
info, err := os.Stat(f3)
if err != nil {
t.Fatal(err)
}
if info.Size() <= f3size {
t.Errorf("got f3 size %d, want some number greater than %d", info.Size(), f3size)
}
})
t.Run("stdoutfn", func(t *testing.T) {
buf := new(bytes.Buffer)
fn := func(context.Context, *Controller) io.Writer { return buf }
c5 := &Command{Cmd: "cat", Args: []string{"_testdata/hw"}, StdoutFn: fn}
if err = con.Run(ctx, c5); err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf.Bytes(), hw) {
t.Errorf("got %s, want %s", buf, string(hw))
}
})
t.Run("stderrfn", func(t *testing.T) {
buf := new(bytes.Buffer)
fn := func(context.Context, *Controller) io.Writer { return buf }
c6 := &Command{Cmd: "cat", Args: []string{dne}, StderrFn: fn}
err = con.Run(ctx, c6)
if err == nil { // sic
t.Fatal("got no error but expected one")
}
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatal(err)
}
if buf.Len() == 0 {
t.Error("no stderr bytes captured")
}
})
}