-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig_test.go
114 lines (100 loc) · 3.35 KB
/
config_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
package config
import (
"os"
"testing"
"github.com/robertkrimen/otto"
"github.com/stretchr/testify/assert"
"github.com/picostack/pico/task"
)
func Test_applyFileTargets(t *testing.T) {
tests := []struct {
name string
script string
wantTargets task.Targets
wantErr bool
}{
{"one", `T({
name: "name",
url: "../test.local",
up: ["echo", "hello world"]
})`, task.Targets{
{
Name: "name",
RepoURL: "../test.local",
Up: []string{"echo", "hello world"},
Env: map[string]string{},
},
}, false},
{"variable", `
var url = "https://github.com/Southclaws/";
T({name: "1", url: url + "project1", up: ["sleep"]});
T({name: "2", url: url + "project2", up: ["sleep"]});
T({name: "3", url: url + "project3", up: ["sleep"]});
console.log("done!");
`, task.Targets{
{Name: "1", RepoURL: "https://github.com/Southclaws/project1", Up: []string{"sleep"}, Env: map[string]string{}},
{Name: "2", RepoURL: "https://github.com/Southclaws/project2", Up: []string{"sleep"}, Env: map[string]string{}},
{Name: "3", RepoURL: "https://github.com/Southclaws/project3", Up: []string{"sleep"}, Env: map[string]string{}},
}, false},
{"auth", `
var auther = A({
name: "auth",
path: "path",
user_key: "user_key",
pass_key: "pass_key"
});
T({
name: "name",
url: "../test.local",
up: ["echo", "hello world"],
auth: auther,
});
console.log("done!");
`, task.Targets{
{Name: "name", RepoURL: "../test.local", Up: []string{"echo", "hello world"}, Env: map[string]string{}, Auth: "auth"},
}, false},
{"envmap", `
var url = "https://github.com/Southclaws/";
T({name: "1", url: url + "project1", up: ["sleep"], env: {PASSWORD: "nope"}});
T({name: "2", url: url + "project2", up: ["sleep"], env: {PASSWORD: "nope"}});
T({name: "3", url: url + "project3", up: ["sleep"], env: {PASSWORD: "nope"}});
console.log("done!");
`, task.Targets{
{Name: "1", RepoURL: "https://github.com/Southclaws/project1", Up: []string{"sleep"}, Env: map[string]string{"PASSWORD": "nope"}},
{Name: "2", RepoURL: "https://github.com/Southclaws/project2", Up: []string{"sleep"}, Env: map[string]string{"PASSWORD": "nope"}},
{Name: "3", RepoURL: "https://github.com/Southclaws/project3", Up: []string{"sleep"}, Env: map[string]string{"PASSWORD": "nope"}},
}, false},
{"envglobal", `
E("GLOBAL", "readme");
T({
name: "name",
url: "../test.local",
up: ["sleep"],
env: {LOCAL: "hi"}
})
`, task.Targets{
{Name: "name", RepoURL: "../test.local", Up: []string{"sleep"}, Env: map[string]string{"GLOBAL": "readme", "LOCAL": "hi"}},
}, false},
{"badtype", `T({name: "name", url: "../test.local", up: 1.23})`, task.Targets{}, true},
{"missingkey", `T({name: "name", url: "../test.local"})`, task.Targets{}, true},
{"env", `console.log(ENV["TEST_ENV_KEY"])`, task.Targets{}, false},
{"hostname", `console.log(HOSTNAME)`, task.Targets{}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cb := configBuilder{
vm: otto.New(),
state: new(State),
scripts: []string{tt.script},
}
os.Setenv("TEST_ENV_KEY", "an environment variable inside the JS vm")
err := cb.construct("host")
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantTargets, cb.state.Targets)
})
}
}