forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_test.go
286 lines (232 loc) · 7.41 KB
/
update_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
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package update
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Mock exec.Command
type mockExec struct {
name string
args []string
}
func (me *mockExec) Run(name string, arg ...string) error {
me.name = name
me.args = append([]string{name}, arg...)
return nil
}
func mustRead(filename string) []byte {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return data
}
func mustVersion(s string) SemVer {
v, _ := NewSemVer(s)
return v
}
type testSource struct {
dls []Download
}
func (src testSource) Downloads() ([]Download, error) { return src.dls, nil }
type testFailSource struct{}
func (src testFailSource) Downloads() ([]Download, error) { return nil, errors.New("fail") }
func withTempDir(fn func(dir string)) {
dir, err := ioutil.TempDir("", "aw-")
if err != nil {
panic(err)
}
defer panicOnError(os.RemoveAll(dir))
fn(dir)
}
var (
testSrc1 = &testSource{
dls: []Download{
{Version: mustVersion("0.5.0-beta"), Prerelease: true, Filename: "Dummy.alfredworkflow"},
{Version: mustVersion("0.1"), Prerelease: false, Filename: "Dummy.alfredworkflow"},
{Version: mustVersion("0.4"), Prerelease: false, Filename: "Dummy.alfredworkflow"},
{Version: mustVersion("0.2"), Prerelease: false, Filename: "Dummy.alfredworkflow"},
{Version: mustVersion("0.3"), Prerelease: false, Filename: "Dummy.alfredworkflow"},
},
}
testSrc2 = &testSource{
dls: []Download{
{Version: mustVersion("0.5.0-beta"), Prerelease: true, Filename: "Dummy.alfredworkflow"},
{Version: mustVersion("0.4.0-beta"), Prerelease: true, Filename: "Dummy.alfredworkflow"},
{Version: mustVersion("0.3.0-beta"), Prerelease: true, Filename: "Dummy.alfredworkflow"},
},
}
)
func TestUpdater(t *testing.T) {
withTempDir(func(dir string) {
vStr := "4.0.4"
oldVal := os.Getenv("alfred_version")
defer panicOnError(os.Setenv("alfred_version", oldVal))
panicOnError(os.Setenv("alfred_version", vStr))
u, err := NewUpdater(testSrc1, "0.2.2", dir)
assert.Nil(t, err, "create updater failed")
assert.Nil(t, u.CheckForUpdate(), "get releases failed")
u.CurrentVersion = mustVersion("1")
assert.False(t, u.UpdateAvailable(), "unexpected update")
u.CurrentVersion = mustVersion("0.5")
assert.False(t, u.UpdateAvailable(), "unexpected update")
u.CurrentVersion = mustVersion("0.4.5")
assert.False(t, u.UpdateAvailable(), "unexpected update")
u.Prereleases = true
assert.True(t, u.UpdateAvailable(), "unexpected update")
sv, _ := NewSemVer(vStr)
assert.True(t, sv.Eq(u.AlfredVersion), "unexpected Alfred version")
// Empty cache directory
_, err = NewUpdater(testSrc1, "0.2.2", "")
assert.NotNil(t, err, "Updater accepted empty cacheDir")
})
}
// TestUpdaterPreOnly tests that updater works with only pre-releases available
func TestUpdaterPreOnly(t *testing.T) {
t.Parallel()
withTempDir(func(dir string) {
u, err := NewUpdater(testSrc2, "0.2.2", dir)
require.Nil(t, err, "create updater failed")
require.Nil(t, u.CheckForUpdate(), "get releases failed")
u.CurrentVersion = mustVersion("1")
assert.False(t, u.UpdateAvailable(), "unexpected update")
u.Prereleases = true
u.CurrentVersion = mustVersion("0.4.5")
assert.True(t, u.UpdateAvailable(), "unexpected update")
})
}
// TestUpdateInterval tests caching of LastCheck.
func TestUpdateInterval(t *testing.T) {
t.Parallel()
t.Run("UpdateIntervalOnSuccess", func(t *testing.T) {
t.Parallel()
testUpdateInterval(testSource{}, false, t)
})
t.Run("UpdateIntervalOnFailure", func(t *testing.T) {
t.Parallel()
testUpdateInterval(testFailSource{}, true, t)
})
}
func testUpdateInterval(src Source, fail bool, t *testing.T) {
withTempDir(func(dir string) {
u, err := NewUpdater(src, "0.2.2", dir)
require.Nil(t, err, "create updater failed")
// UpdateInterval is set
assert.True(t, u.LastCheck.IsZero(), "LastCheck is not zero")
assert.True(t, u.CheckDue(), "update check is not due")
// LastCheck is updated
if fail {
assert.NotNil(t, u.CheckForUpdate(), "fetch releases succeeded")
} else {
assert.Nil(t, u.CheckForUpdate(), "fetch releases failed")
}
assert.False(t, u.LastCheck.IsZero(), "LastCheck is zero")
assert.False(t, u.CheckDue(), "update check is due")
// Changing UpdateInterval
u.updateInterval = time.Nanosecond
assert.True(t, u.CheckDue(), "update check is not due")
})
}
func TestUpdater_Install(t *testing.T) {
origRun := runCommand
origDownload := download
defer func() {
runCommand = origRun
download = origDownload
}()
me := &mockExec{}
runCommand = me.Run
download = func(URL, path string) error { return nil }
withTempDir(func(dir string) {
u, err := NewUpdater(testSrc1, "0.2.2", dir)
require.Nil(t, err, "create updater failed")
assert.False(t, u.UpdateAvailable(), "empty updater has update")
assert.NotNil(t, u.Install(), "empty updater installed")
assert.Nil(t, u.CheckForUpdate(), "get releases failed")
assert.Nil(t, u.Install(), "install failed")
assert.Equal(t, "open", me.name, "wrong command called")
})
}
func TestHTTPClient(t *testing.T) {
t.Parallel()
t.Run("HTTP(hello)", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprintln(w, "hello"); err != nil {
panic(err)
}
}))
defer ts.Close()
data, err := getURL(ts.URL)
require.Nil(t, err, "getURL failed")
ts.Close()
assert.Equal(t, "hello\n", string(data), "unexpected response")
})
t.Run("HTTP(404)", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
}))
defer ts.Close()
_, err := getURL(ts.URL)
assert.NotNil(t, err, "404 request succeeded")
ts.Close()
})
t.Run("HTTP(fail)", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}))
URL := ts.URL
ts.Close()
_, err := getURL(URL)
assert.NotNil(t, err, "bad request succeeded")
ts.Close()
})
t.Run("HTTP(download)", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprintln(w, "contents"); err != nil {
panic(err)
}
}))
defer ts.Close()
f, err := ioutil.TempFile("", "awgo-*-test")
require.Nil(t, err, "create tempfile failed")
defer panicOnError(f.Close())
err = download(ts.URL, f.Name())
require.Nil(t, err, "download failed")
data, err := ioutil.ReadFile(f.Name())
require.Nil(t, err, "read file failed")
assert.Equal(t, "contents\n", string(data), "unexpected file contents")
})
t.Run("HTTP(download fails)", func(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprintln(w, "contents"); err != nil {
panic(err)
}
}))
URL := ts.URL
ts.Close()
err := download(URL, "")
require.NotNil(t, err, "bad download succeeded")
})
}
func TestRunCommand(t *testing.T) {
assert.Nil(t, runCommand("/usr/bin/true"), `exec "/usr/bin/true" returned error`)
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}