-
Notifications
You must be signed in to change notification settings - Fork 46
/
workflow_test.go
356 lines (310 loc) · 8.56 KB
/
workflow_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.deanishe.net/env"
"github.com/deanishe/awgo/util"
)
// TestWorkflowValues tests workflow name, bundle ID etc.
func TestWorkflowValues(t *testing.T) {
t.Parallel()
withTestWf(func(wf *Workflow) {
assert.Equal(t, tName, wf.Name(), "unexpected name")
assert.Equal(t, tBundleID, wf.BundleID(), "unexpected bundle ID")
})
}
// TestInvalidEnv executes workflow in an invalid environment.
func TestInvalidEnv(t *testing.T) {
assert.Panics(t, func() { NewFromEnv(env.MapEnv{}) })
}
// Options correctly alter Workflow.
func TestNew(t *testing.T) {
t.Parallel()
tests := []struct {
opt Option // option to set
test func(wf *Workflow) bool // function to verify change was made
desc string // test title
}{
{
HelpURL("http://www.example.com"),
func(wf *Workflow) bool { return wf.helpURL == "http://www.example.com" },
"Set HelpURL"},
{
MaxResults(10),
func(wf *Workflow) bool { return wf.maxResults == 10 },
"Set MaxResults"},
{
LogPrefix("blah"),
func(wf *Workflow) bool { return wf.logPrefix == "blah" },
"Set LogPrefix"},
{
SessionName("SESH"),
func(wf *Workflow) bool { return wf.sessionName == "SESH" },
"Set SessionName"},
{
SortOptions(),
func(wf *Workflow) bool { return wf.sortOptions == nil },
"Set SortOptions"},
{
SuppressUIDs(true),
func(wf *Workflow) bool { return wf.Feedback.NoUIDs == true },
"Set SuppressUIDs"},
{
MagicPrefix("aw:"),
func(wf *Workflow) bool { return wf.magicPrefix == "aw:" },
"Set MagicPrefix"},
{
MaxLogSize(2048),
func(wf *Workflow) bool { return wf.maxLogSize == 2048 },
"Set MaxLogSize"},
{
TextErrors(true),
func(wf *Workflow) bool { return wf.textErrors == true },
"Set TextErrors"},
{
AddMagic(&mockMA{}),
func(wf *Workflow) bool { return wf.magicActions.actions["test"] != nil },
"Add Magic"},
{
RemoveMagic(logMA{}),
func(wf *Workflow) bool { return wf.magicActions.actions["log"] == nil },
"Remove Magic"},
}
for _, td := range tests {
td := td // capture variable
t.Run(fmt.Sprintf("Option(%#v)", td.opt), func(t *testing.T) {
t.Parallel()
wf := New(td.opt)
assert.True(t, td.test(wf), "option failed")
})
}
}
func TestWorkflow_Run(t *testing.T) {
withTestWf(func(wf *Workflow) {
var called bool
run := func() { called = true }
wf.Run(run)
assert.True(t, called, "run wasn't called")
})
}
func TestWorkflow_Run_Rescue(t *testing.T) {
withTestWf(func(wf *Workflow) {
me := &mockExit{}
exitFunc = me.Exit
defer func() { exitFunc = os.Exit }()
wf.Run(func() { panic("aaaargh!") })
assert.Equal(t, 1, me.code, "workflow did not catch panic")
})
}
// TestWorkflowDir verifies that AwGo finds the right directory.
func TestWorkflow_Dir(t *testing.T) {
t.Parallel()
var (
cwd string
err error
)
cwd, err = os.Getwd()
require.Nil(t, err, "Getwd failed")
tests := []struct {
in, x string
}{
{"testdata", "testdata"},
{"testdata/subdir", "testdata"},
{".", "."},
{"", ""},
}
for _, td := range tests {
td := td
t.Run(fmt.Sprintf("findWorkflowRoot(%q)", td.in), func(t *testing.T) {
t.Parallel()
assert.Equal(t, td.x, findWorkflowRoot(td.in), "unexpected root")
})
}
wf := New()
assert.Equal(t, cwd, wf.Dir(), "unexpected workflow dir")
}
// Check that AW's directories exist
func TestWorkflow_awDirs(t *testing.T) {
t.Parallel()
withTestWf(func(wf *Workflow) {
p := wf.awCacheDir()
assert.True(t, util.PathExists(p), "AW cache dir does not exist")
assert.True(t, strings.HasSuffix(p, "_aw"), "AW cache is not called '_aw'")
p = wf.awDataDir()
assert.True(t, util.PathExists(p), "AW data dir does not exist")
assert.True(t, strings.HasSuffix(p, "_aw"), "AW data is not called '_aw'")
})
}
// Check log is rotated
func TestWorkflow_logRotate(t *testing.T) {
logInitialized = false // ensure log is created
withTestWf(func(wf *Workflow) {
wf.Configure(MaxLogSize(10))
log.Print("more than 10 bytes")
assert.True(t, util.PathExists(wf.LogFile()), "log file does not exist")
logInitialized = false // ensure log is created
wf.initializeLogging()
assert.True(t, util.PathExists(wf.LogFile()+".1"), "log file not rotated")
})
}
// Variables are correctly set
func TestWorkflow_Vars(t *testing.T) {
t.Parallel()
vars := map[string]string{
"key1": "val1",
"key2": "val2",
"key3": "val3",
"key4": "val4",
"key5": "val5",
}
withTestWf(func(wf *Workflow) {
for k, v := range vars {
wf.Var(k, v)
}
assert.Equal(t, vars, wf.Vars(), "Unexpected Vars")
})
}
func TestWorkflow_Rerun(t *testing.T) {
t.Parallel()
withTestWf(func(wf *Workflow) {
v := 0.1
wf.Rerun(v)
assert.Equal(t, v, wf.Feedback.rerun, "Unexpected Rerun")
})
}
func TestWorkflow_Fatal(t *testing.T) {
var exit bool
exitFunc = func(code int) { exit = true }
withTestWf(func(wf *Workflow) {
wf.Fatal("")
assert.True(t, exit, "Fatal did not exit")
})
exit = false
withTestWf(func(wf *Workflow) {
wf.FatalError(errors.New("some error"))
assert.True(t, exit, "FatalError did not exit")
})
exit = false
withTestWf(func(wf *Workflow) {
wf.Fatalf("die")
assert.True(t, exit, "Fatalf did not exit")
})
}
func TestRunCommand(t *testing.T) {
t.Parallel()
err := runCommand("/usr/bin/true")
assert.Nil(t, err, `call "/usr/bin/true" failed`)
err = runCommand("/does/not/exist")
assert.NotNil(t, err, `call to "/does/not/exist" returned no error`)
}
// New initialises a Workflow with the default settings. Name,
// bundle ID, version etc. are read from the environment variables set by Alfred.
func ExampleNew() {
wf := New()
// Name is read from environment
fmt.Println(wf.Name())
// BundleID is read from environment
fmt.Println(wf.BundleID())
// Version is from info.plist
fmt.Println(wf.Version())
// Output:
// AwGo
// net.deanishe.awgo
// 1.2.0
}
// Pass one or more Options to New() to configure the created Workflow.
func ExampleNew_withOptions() {
wf := New(HelpURL("http://www.example.com"), MaxResults(200))
fmt.Println(wf.helpURL)
fmt.Println(wf.maxResults)
// Output:
// http://www.example.com
// 200
}
// The normal way to create a new Item, but not the normal way to use it.
//
// Typically, when you're done adding Items, you call SendFeedback() to
// send the results to Alfred.
func ExampleWorkflow_NewItem() {
wf := New()
// Create a new item via the Workflow object, which will
// track the Item and send it to Alfred when you call
// Workflow.SendFeedback()
//
// Title is the only required value.
it := wf.NewItem("First Result").
Subtitle("Some details here")
// Just to see what it looks like...
data, _ := json.Marshal(it)
fmt.Println(string(data))
// Output: {"title":"First Result","subtitle":"Some details here","valid":false}
}
// Change Workflow's configuration after creation, then revert it.
func ExampleWorkflow_Configure() {
wf := New()
// Default settings (false and 0)
fmt.Println(wf.textErrors)
fmt.Println(wf.maxResults)
// Turn text errors on, set max results and save Option to revert
// to previous configuration
previous := wf.Configure(TextErrors(true), MaxResults(200))
fmt.Println(wf.textErrors)
fmt.Println(wf.maxResults)
// Revert to previous configuration
wf.Configure(previous)
fmt.Println(wf.textErrors)
fmt.Println(wf.maxResults)
// Output:
// false
// 0
// true
// 200
// false
// 0
}
func ExampleWorkflow_Warn() {
wf := New()
// Add some items
wf.NewItem("Item One").
Subtitle("Subtitle one")
wf.NewItem("Item Two").
Subtitle("Subtitle two")
// Delete existing items, add a warning, then
// immediately send feedback
wf.Warn("Bad Items", "Those items are boring")
// Output:
// {
// "variables": {
// "AW_SESSION_ID": "test-session-id"
// },
// "items": [
// {
// "title": "Bad Items",
// "subtitle": "Those items are boring",
// "valid": false,
// "icon": {
// "path": "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns"
// }
// }
// ]
// }
}
func ExampleArgVars() {
// Set workflow variables from Alfred's Run Script Action
av := NewArgVars()
av.Arg("baz") // Set output (i.e. next action's {query}) to "baz"
av.Var("foo", "bar") // Set workflow variable "foo" to "bar"
if err := av.Send(); err != nil {
panic(err)
}
// Output: {"alfredworkflow":{"arg":"baz","variables":{"foo":"bar"}}}
}