-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_test.go
142 lines (112 loc) · 2.39 KB
/
runner_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"
"fmt"
"os"
"sync/atomic"
"testing"
"github.com/bobg/go-generics/v2/set"
"github.com/bradleyjkemp/cupaloy/v2"
)
func TestRunTarget(t *testing.T) {
t.Parallel()
var (
con = NewController("")
ctx = context.Background()
)
ctx = WithVerbose(ctx, true)
var (
ct = &countTarget{}
target = Files(ct, nil, []string{"/dev/null"})
targets []Target
)
for i := 0; i < 1000; i++ {
targets = append(targets, target)
}
err := con.Run(ctx, targets...)
if err != nil {
t.Fatal(err)
}
if ct.count != 1 {
t.Errorf("got %d, want 1", ct.count)
}
db := memHashDB{s: set.New[string]()}
ctx = WithHashDB(ctx, &db)
con = NewController("")
err = con.Run(ctx, targets...)
if err != nil {
t.Fatal(err)
}
if ct.count != 2 {
t.Errorf("got %d, want 2", ct.count)
}
con = NewController("")
err = con.Run(ctx, targets...)
if err != nil {
t.Fatal(err)
}
if ct.count != 2 {
t.Errorf("got %d, want 2", ct.count)
}
}
type countTarget struct {
count uint32
}
func (ct *countTarget) Run(context.Context, *Controller) error {
atomic.AddUint32(&ct.count, 1)
return nil
}
func (*countTarget) Desc() string {
return "count"
}
type memHashDB struct {
s set.Of[string]
}
func (m *memHashDB) Has(_ context.Context, h []byte) (bool, error) {
return m.s.Has(string(h)), nil
}
func (m *memHashDB) Add(_ context.Context, h []byte) error {
m.s.Add(string(h))
return nil
}
func TestIndentingCopier(t *testing.T) {
t.Parallel()
b, err := os.ReadFile("_testdata/indenting_copier.input")
if err != nil {
t.Fatal(err)
}
text := string(b)
var (
con = NewController("")
buf = new(bytes.Buffer)
w = con.IndentingCopier(buf, "> ")
)
fmt.Fprint(w, text)
con.incDepth()
w = con.IndentingCopier(buf, "> ")
fmt.Fprint(w, text)
con.incDepth()
w = con.IndentingCopier(buf, "> ")
fmt.Fprint(w, text)
con.decDepth()
w = con.IndentingCopier(buf, "> ")
fmt.Fprint(w, text)
snaps := cupaloy.New(cupaloy.SnapshotSubdirectory("_testdata"))
snaps.SnapshotT(t, buf.String())
}
func TestIndentf(t *testing.T) {
t.Parallel()
con := NewController("")
buf := new(bytes.Buffer)
con.indentf(buf, "foo")
if got := buf.String(); got != "foo\n" {
t.Errorf("got %s, want foo\\n", buf.String())
}
buf.Reset()
con.incDepth()
con.indentf(buf, "bar")
if got := buf.String(); got != " bar\n" {
t.Errorf("got %s, want \" bar\\n\"", buf.String())
}
}