forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_test.go
129 lines (119 loc) · 2.48 KB
/
action_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
package gb
import (
"reflect"
"sort"
"testing"
)
// assert that anonymous and named functions can be
// converted to a Task via TaskFn
func f() error { return nil }
var _ Task = TaskFn(func() error { return nil })
var _ Task = TaskFn(f)
func TestBuildAction(t *testing.T) {
actions := func(a ...*Action) []*Action {
return a
}
var tests = []struct {
pkg string
action *Action
err error
}{{
pkg: "a",
action: &Action{
Name: "build: a",
Deps: actions(&Action{Name: "compile: a"}),
},
}, {
pkg: "b",
action: &Action{
Name: "build: b",
Deps: []*Action{
&Action{
Name: "link: b",
Deps: []*Action{
&Action{
Name: "compile: b",
Deps: []*Action{
&Action{
Name: "compile: a",
}},
},
}},
},
},
}, {
pkg: "c",
action: &Action{
Name: "build: c",
Deps: []*Action{
&Action{
Name: "compile: c",
Deps: []*Action{
&Action{
Name: "compile: a",
}, &Action{
Name: "compile: d.v1",
}},
}},
},
}}
for _, tt := range tests {
ctx := testContext(t)
defer ctx.Destroy()
pkg, err := ctx.ResolvePackage(tt.pkg)
if !sameErr(err, tt.err) {
t.Errorf("ctx.ResolvePackage(%v): want %v, got %v", tt.pkg, tt.err, err)
continue
}
if err != nil {
continue
}
got, err := BuildPackages(pkg)
if !sameErr(err, tt.err) {
t.Errorf("BuildAction(%v): want %v, got %v", tt.pkg, tt.err, err)
continue
}
deleteTasks(got)
if !reflect.DeepEqual(tt.action, got) {
t.Errorf("BuildAction(%v): want %#v, got %#v", tt.pkg, tt.action, got)
}
// double underpants
sameAction(t, got, tt.action)
}
}
func sameAction(t *testing.T, want, got *Action) {
if want.Name != got.Name {
t.Errorf("sameAction: names do not match, want: %v, got %v", want.Name, got.Name)
return
}
if len(want.Deps) != len(got.Deps) {
t.Errorf("sameAction(%v, %v): deps: len(want): %v, len(got): %v", want.Name, got.Name, len(want.Deps), len(got.Deps))
return
}
w, g := make(map[string]*Action), make(map[string]*Action)
for _, a := range want.Deps {
w[a.Name] = a
}
for _, a := range got.Deps {
g[a.Name] = a
}
var wk []string
for k := range w {
wk = append(wk, k)
}
sort.Strings(wk)
for _, a := range wk {
g, ok := g[a]
if !ok {
t.Errorf("sameAction(%v, %v): deps: want %v, got nil", want.Name, got.Name, a)
continue
}
sameAction(t, w[a], g)
}
}
func deleteTasks(a *Action) {
for _, d := range a.Deps {
deleteTasks(d)
}
a.Task = nil
}