-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
130 lines (100 loc) · 2.78 KB
/
handler_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
package concourse
// TODO: we need to move stdin/stdout to a sub package. Somewhere in internal, to break this cycle
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"pkg.loki.codes/concourse-resource/internal"
"pkg.loki.codes/concourse-resource/test"
"pkg.loki.codes/concourse-resource/test/dummy"
)
var resStdin = new(bytes.Buffer)
var resStdout = new(bytes.Buffer)
func init() {
internal.StdIn = resStdin
internal.StdOut = resStdout
}
func TestConfig(t *testing.T) {
resStdin.Reset()
resStdout.Reset()
assert := assert.New(t)
resStdin.Write([]byte(fmt.Sprintf(`{%s}`, dummy.SourceFixture)))
getCommand = func() string {
return "check"
}
var tRes any
h := New[dummy.Config](func(config dummy.Config) (res any, err error) {
res, err = dummy.New(config)
tRes = res
return
})
_ = h.Run()
tRes.(test.MockResource[dummy.Config]).Validate(assert, dummy.SourceExpected)
}
func TestCheck(t *testing.T) {
resStdin.Reset()
resStdout.Reset()
assert := assert.New(t)
resStdin.Write([]byte(fmt.Sprintf(`{%s, %s}`, dummy.SourceFixture, dummy.VersionFixture)))
getCommand = func() string {
return "check"
}
var tRes any
h := New[dummy.Config](func(config dummy.Config) (res any, err error) {
res, err = dummy.New(config)
tRes = res
return
})
if !assert.NoError(h.Run()) {
return
}
tRes.(test.MockCheckResource).ValidateCheck(assert, dummy.VersionExpected)
assert.JSONEq(`[{"ref":"61cbef"},{"ref":"d74e01"},{"ref":"7154fe"}]`, resStdout.String())
}
func TestIn(t *testing.T) {
resStdin.Reset()
resStdout.Reset()
assert := assert.New(t)
resStdin.Write([]byte(fmt.Sprintf(`{%s, %s, %s}`, dummy.SourceFixture, dummy.VersionFixture, dummy.ParamsFixture)))
getCommand = func() string {
return "in"
}
getArg = func() string {
return "/dummy"
}
var tRes any
h := New[dummy.Config](func(config dummy.Config) (res any, err error) {
res, err = dummy.New(config)
tRes = res
return
})
if !assert.NoError(h.Run()) {
return
}
tRes.(test.MockInResource).ValidateIn(assert, dummy.VersionExpected, dummy.ParamsExpected)
assert.JSONEq(`{"metadata":{"test":"value"},"version":{"ref":"61cbef"}}`, resStdout.String())
}
func TestOut(t *testing.T) {
resStdin.Reset()
resStdout.Reset()
assert := assert.New(t)
resStdin.Write([]byte(fmt.Sprintf(`{%s, %s}`, dummy.SourceFixture, dummy.ParamsFixture)))
getCommand = func() string {
return "out"
}
getArg = func() string {
return "/dummy"
}
var tRes any
h := New[dummy.Config](func(config dummy.Config) (res any, err error) {
res, err = dummy.New(config)
tRes = res
return
})
if !assert.NoError(h.Run()) {
return
}
tRes.(test.MockOutResource).ValidateOut(assert, dummy.ParamsExpected)
assert.JSONEq(`{"metadata":{"test":"value"},"version":{"ref":"61cbef"}}`, resStdout.String())
}