generated from golang-templates/seed
/
task_test.go
161 lines (126 loc) · 4.68 KB
/
task_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package goyek_test
import (
"context"
"io/ioutil"
"reflect"
"runtime"
"testing"
"github.com/goyek/goyek/v2"
)
func TestDefinedTask_SetName(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
called := false
task := flow.Define(goyek.Task{Name: "one", Action: func(*goyek.A) { called = true }})
task.SetName("new")
got := task.Name()
assertEqual(t, got, "new", "should update the name")
err := flow.Execute(context.Background(), []string{"new"})
assertPass(t, err, "should pass")
assertTrue(t, called, "should call the action")
}
func TestDefinedTask_SetName_for_default(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
called := false
task := flow.Define(goyek.Task{Name: "one", Action: func(*goyek.A) { called = true }})
flow.SetDefault(task)
task.SetName("new")
got := task.Name()
assertEqual(t, got, "new", "should update the name")
err := flow.Execute(context.Background(), nil)
assertPass(t, err, "should pass")
assertTrue(t, called, "should call the action")
}
func TestDefinedTask_SetName_for_depenency(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
called := false
task := flow.Define(goyek.Task{Name: "one", Action: func(*goyek.A) { called = true }})
flow.Define(goyek.Task{Name: "two", Deps: goyek.Deps{task}})
task.SetName("new")
err := flow.Execute(context.Background(), []string{"two"})
assertPass(t, err, "should pass")
assertTrue(t, called, "should call the dependency with changed name")
}
func TestDefinedTask_SetName_conflict(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
task := flow.Define(goyek.Task{Name: "one"})
flow.Define(goyek.Task{Name: "two"})
act := func() { task.SetName("two") }
assertPanics(t, act, "should not allow setting existing task name")
}
func TestDefinedTask_SetUsage(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
task := flow.Define(goyek.Task{Name: "one"})
task.SetUsage("good task")
got := flow.Tasks()[0].Usage()
assertEqual(t, got, "good task", "should update the usage")
}
func TestDefinedTask_SetAction(t *testing.T) {
getFuncName := func(fn func(a *goyek.A)) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
}
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
originalNotCalled := true
task := flow.Define(goyek.Task{Name: "one", Action: func(*goyek.A) { originalNotCalled = false }})
newCalled := false
fn := func(*goyek.A) { newCalled = true }
task.SetAction(fn)
want := getFuncName(fn)
got := getFuncName(task.Action())
assertEqual(t, got, want, "should update the action")
err := flow.Execute(context.Background(), []string{"one"})
assertPass(t, err, "should pass")
assertTrue(t, originalNotCalled, "should not call the previous action")
assertTrue(t, newCalled, "should not call the new action")
}
func TestDefinedTask_SetDeps(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
called := false
t1 := flow.Define(goyek.Task{Name: "one", Action: func(*goyek.A) { called = true }})
t2 := flow.Define(goyek.Task{Name: "two", Deps: goyek.Deps{t1}})
t3 := flow.Define(goyek.Task{Name: "three"})
t3.SetDeps(goyek.Deps{t1, t2})
got := t3.Deps()
assertEqual(t, got, goyek.Deps{t1, t2}, "should update the dependencies")
err := flow.Execute(context.Background(), []string{"three"})
assertPass(t, err, "should pass")
assertTrue(t, called, "should call transitive dependency of t3")
}
func TestDefinedTask_SetDeps_clear(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
notCalled := true
t1 := flow.Define(goyek.Task{Name: "one", Action: func(*goyek.A) { notCalled = false }})
t2 := flow.Define(goyek.Task{Name: "two", Deps: goyek.Deps{t1}})
t2.SetDeps(nil)
got := t2.Deps()
assertEqual(t, got, noDeps, "should clear the dependencies")
err := flow.Execute(context.Background(), []string{"two"})
assertPass(t, err, "should pass")
assertTrue(t, notCalled, "should not call any dependency")
}
func TestDefinedTask_SetDeps_circular(t *testing.T) {
flow := &goyek.Flow{}
flow.SetOutput(ioutil.Discard)
t1 := flow.Define(goyek.Task{Name: "one"})
t2 := flow.Define(goyek.Task{Name: "two", Deps: goyek.Deps{t1}})
t3 := flow.Define(goyek.Task{Name: "three", Deps: goyek.Deps{t2}})
act := func() {
t1.SetDeps(goyek.Deps{t3})
}
assertPanics(t, act, "should panic in case of a cyclic dependency")
}
func TestDefinedTask_SetDeps_dep(t *testing.T) {
flow := &goyek.Flow{}
task := flow.Define(goyek.Task{Name: "my-task"})
otherFlow := &goyek.Flow{}
otherTask := otherFlow.Define(goyek.Task{Name: "different-flow"})
act := func() { task.SetDeps(goyek.Deps{otherTask}) }
assertPanics(t, act, "should not be possible use dependencies from different flow")
}