-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntask_test.go
220 lines (186 loc) · 5.13 KB
/
runtask_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package runtask_test
import (
"fmt"
"io"
"os"
"strings"
"testing"
. "github.com/bartdeboer/runtask"
)
type TestTask struct {
Taskfile string
OsArgs []string
ChDir string
Contains string
Expected string
Error string
}
func runTestTask(task TestTask, t *testing.T) {
oldWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current working directory: %s", err)
}
err = os.Chdir(task.ChDir)
if err != nil {
t.Fatalf("Failed to change working directory: %s", err)
}
oldOsArgs := os.Args
os.Args = task.OsArgs
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %s", err)
}
oldStdout := os.Stdout
os.Stdout = w
defer func() {
w.Close()
os.Stdout = oldStdout
os.Args = oldOsArgs
err = os.Chdir(oldWd)
if err != nil {
t.Errorf("Failed to restore working directory: %s", err)
}
}()
// Execute the task
taskErr := RunTask()
if err != nil {
t.Fatalf("Failed testing task: %s", err)
}
w.Close()
out, err := io.ReadAll(r)
if err != nil {
t.Fatalf("Failed to read from pipe: %s", err)
}
if task.Error != "" && fmt.Sprintf("%s", taskErr) != task.Error {
t.Errorf("Expected error '%s', got '%s'", task.Error, taskErr)
}
if task.Expected != "" && string(out) != task.Expected {
t.Errorf("Expected '%s', got '%s'", task.Expected, string(out))
}
if task.Contains != "" && !strings.Contains(string(out), task.Contains) {
t.Errorf("Expected to contain '%s', got '%s'", task.Contains, string(out))
}
}
func TestTaskRunner(t *testing.T) {
// basic notexist
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "notexist"},
Error: "no such task: notexist",
}, t)
// basic
runTestTask(TestTask{
ChDir: "tests/",
OsArgs: []string{"runtask"},
Error: "could not find Taskfile",
}, t)
// basic simple
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "simple"},
Expected: "Hello, World!\n",
}, t)
// basic simple
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "simple", "arg1"},
Error: "failed to call task function Simple: wrong parameter count",
}, t)
// basic simplearg
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "simplearg"},
Error: "failed to call task function SimpleArg: wrong parameter count",
}, t)
// basic simplearg
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "simplearg", "John"},
Expected: "Hello, John!\n",
}, t)
// basic simplearg
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "simplearg", "John", "Jane"},
Error: "failed to call task function SimpleArg: wrong parameter count",
}, t)
// basic variadic
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "variadic"},
Error: "failed to call task function Variadic: wrong parameter count",
}, t)
// basic variadic
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "variadic", "Jane"},
Expected: "Hello, Jane []!\n",
}, t)
// basic variadic
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "variadic", "John", "Jane", "Johnie"},
Expected: "Hello, John [Jane, Johnie]!\n",
}, t)
// basic add1
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "add1", "1.567", "1", "2", "3", "4", "5"},
Expected: "1.567 15\n",
}, t)
// basic add2
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "add2", "1.567", "1", "2", "3", "4", "5"},
Expected: "1.567 15\n",
}, t)
// basic add2
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "add2", "1.567", "1", "2", "3", "string", "5"},
Error: `failed to call task function Add2: converting argument 4: converting string to int: strconv.ParseInt: parsing "string": invalid syntax`,
}, t)
// basic run
runTestTask(TestTask{
ChDir: "tests/basic/",
OsArgs: []string{"runtask", "run"},
Expected: "Overridden\n",
}, t)
// env simple
runTestTask(TestTask{
ChDir: "tests/env/",
OsArgs: []string{"runtask", "simple"},
Expected: "FOO: Hello, Jane!\nBAR: Hello, John!\n",
}, t)
// env simple
runTestTask(TestTask{
ChDir: "tests/script/",
OsArgs: []string{"runtask", "count"},
Expected: "6\n",
}, t)
// script count
runTestTask(TestTask{
ChDir: "tests/script/",
OsArgs: []string{"runtask", "count"},
Expected: "6\n",
}, t)
// script toupper
runTestTask(TestTask{
ChDir: "tests/script/",
OsArgs: []string{"runtask", "toupper"},
// Contains: "FIFTH LINE",
Contains: "FIFTH LINE\n",
}, t)
// script exec
runTestTask(TestTask{
ChDir: "tests/script/",
OsArgs: []string{"runtask", "execgoversion"},
Contains: "go version",
}, t)
// script text filter
runTestTask(TestTask{
ChDir: "tests/script/",
OsArgs: []string{"runtask", "textfilter"},
Contains: `scanned line: "Fifth line"` + "\n",
}, t)
}