forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
184 lines (162 loc) · 4.46 KB
/
helper_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
package astilectron
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"github.com/asticode/go-astitools/context"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
// mockedHandler is a mocked handler
type mockedHandler struct {
e bool
}
func (h *mockedHandler) readFile(rw http.ResponseWriter, path string) {
var b, err = ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Write(b)
}
// ServeHTTP implements the http.Handler interface
func (h *mockedHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if h.e {
rw.WriteHeader(http.StatusInternalServerError)
return
}
switch r.URL.Path {
case "/provisioner/astilectron":
h.readFile(rw, "testdata/provisioner/astilectron/astilectron.zip")
case "/provisioner/electron/darwin":
h.readFile(rw, "testdata/provisioner/electron/darwin/electron.zip")
case "/provisioner/electron/linux":
h.readFile(rw, "testdata/provisioner/electron/linux/electron.zip")
case "/provisioner/electron/windows":
h.readFile(rw, "testdata/provisioner/electron/windows/electron.zip")
default:
rw.Write([]byte("body"))
}
}
var tempPathCount int
func mockedTempPath() string {
tempPathCount++
return fmt.Sprintf("testdata/tmp/%d", tempPathCount)
}
func TestDownload(t *testing.T) {
// Init
var mh = &mockedHandler{e: true}
var s = httptest.NewServer(mh)
var dst = mockedTempPath()
// Test failed download
err := Download(context.Background(), &http.Client{}, s.URL, dst)
assert.Contains(t, err.Error(), "returned 500 status code")
_, err = os.Stat(dst)
assert.True(t, os.IsNotExist(err))
// Test successful download
mh.e = false
err = Download(context.Background(), &http.Client{}, s.URL, dst)
assert.NoError(t, err)
defer os.Remove(dst)
b, err := ioutil.ReadFile(dst)
assert.NoError(t, err)
assert.Equal(t, "body", string(b))
}
// mockedDisembedder is a mocked disembedder
func mockedDisembedder(src string) ([]byte, error) {
switch src {
case "astilectron":
return ioutil.ReadFile("testdata/provisioner/astilectron/astilectron.zip")
case "electron/linux":
return ioutil.ReadFile("testdata/provisioner/electron/linux/electron.zip")
case "test":
return []byte("body"), nil
default:
return []byte{}, errors.New("invalid")
}
}
func TestDisembed(t *testing.T) {
// Init
var dst = mockedTempPath()
// Test failed disembed
err := Disembed(context.Background(), mockedDisembedder, "invalid", dst)
assert.EqualError(t, err, "disembedding invalid failed: invalid")
// Test successful disembed
err = Disembed(context.Background(), mockedDisembedder, "test", dst)
assert.NoError(t, err)
defer os.Remove(dst)
b, err := ioutil.ReadFile(dst)
assert.NoError(t, err)
assert.Equal(t, "body", string(b))
}
func TestPtr(t *testing.T) {
assert.Equal(t, true, *PtrBool(true))
assert.Equal(t, 1, *PtrInt(1))
assert.Equal(t, "1", *PtrStr("1"))
}
// mockedListenable is a mocked listenable
type mockedListenable struct {
d *dispatcher
id string
}
// On implements the listenable interface
func (m *mockedListenable) On(eventName string, l Listener) {
m.d.addListener(m.id, eventName, l)
}
func TestSynchronousFunc(t *testing.T) {
// Init
var d = newDispatcher()
var c = asticontext.NewCanceller()
var l = &mockedListenable{d: d, id: "1"}
var done bool
var m sync.Mutex
l.On("done", func(e Event) bool {
m.Lock()
defer m.Unlock()
done = true
return false
})
// Test canceller cancel
var _ = synchronousFunc(c, l, func() { c.Cancel() }, "done")
assert.False(t, done)
// Test done event
c = asticontext.NewCanceller()
var ed = Event{Name: "done", TargetID: "1"}
var e = synchronousFunc(c, l, func() { d.dispatch(ed) }, "done")
m.Lock()
assert.True(t, done)
m.Unlock()
assert.Equal(t, ed, e)
}
func TestSynchronousEvent(t *testing.T) {
// Init
var d = newDispatcher()
var ed = Event{Name: "done", TargetID: "1"}
var mw = &mockedWriter{fn: func() { d.dispatch(ed) }}
var w = newWriter(mw)
var c = asticontext.NewCanceller()
var l = &mockedListenable{d: d, id: "1"}
var done bool
var m sync.Mutex
l.On("done", func(e Event) bool {
m.Lock()
defer m.Unlock()
done = true
return false
})
var ei = Event{Name: "order", TargetID: "1"}
// Test successful synchronous event
var e, err = synchronousEvent(c, l, w, ei, "done")
assert.NoError(t, err)
m.Lock()
assert.True(t, done)
m.Unlock()
assert.Equal(t, ed, e)
assert.Equal(t, []string{"{\"name\":\"order\",\"targetID\":\"1\"}\n"}, mw.w)
}