-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
118 lines (106 loc) · 2.59 KB
/
run_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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"context"
"testing"
"time"
"github.com/noxsios/vai/uses"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
ctx := context.Background()
fs := afero.NewMemMapFs()
store, err := uses.NewStore(fs)
require.NoError(t, err)
with := With{}
// simple happy path
err = Run(ctx, store, helloWorldWorkflow, "", with, "file:test", false)
require.NoError(t, err)
// fast failure for 404
err = Run(ctx, store, helloWorldWorkflow, "does not exist", with, "file:test", false)
require.EqualError(t, err, "task \"does not exist\" not found")
t.Run("fail on timeout - eval", func(t *testing.T) {
ctx := context.TODO()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
err := Run(ctx, store, Workflow{
"timeout-eval": {Step{Eval: `
times := import("times")
times.sleep(3 * times.second)
`}},
}, "timeout-eval", with, "file:test", false)
require.EqualError(t, err, "context deadline exceeded")
})
t.Run("fail on timeout - run", func(t *testing.T) {
ctx = context.TODO()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
err = Run(ctx, store, Workflow{
"timeout-run": {Step{Run: "sleep 3"}},
}, "timeout-run", with, "file:test", false)
require.EqualError(t, err, "signal: killed")
})
t.Run("boolean and int in with - eval", func(t *testing.T) {
ctx = context.Background()
err = Run(ctx, store, Workflow{
"default": {Step{Eval: `
fmt := import("fmt")
fmt.printf("bool: %t, int: %d\n", b, i)
`, With: map[string]WithEntry{
"b": true,
"i": 42,
}},
},
}, "", with, "file:test", false)
require.NoError(t, err)
})
t.Run("boolean and int in with - run", func(t *testing.T) {
ctx = context.Background()
err = Run(ctx, store, Workflow{
"default": {Step{Run: `
echo "bool: $B, int: $I"
`, With: map[string]WithEntry{
"b": true,
"i": 42,
}},
},
}, "", with, "file:test", false)
require.NoError(t, err)
})
}
func TestToEnvVar(t *testing.T) {
testCases := []struct {
name string
s string
expected string
}{
{
name: "empty",
},
{
name: "simple",
s: "foo",
expected: "FOO",
},
{
name: "with dash",
s: "foo-bar",
expected: "FOO_BAR",
},
{
name: "with underscore",
s: "foo_bar",
expected: "FOO_BAR",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actual := toEnvVar(tc.s)
require.Equal(t, tc.expected, actual)
})
}
}