-
Notifications
You must be signed in to change notification settings - Fork 81
/
main_test.go
465 lines (359 loc) · 10.2 KB
/
main_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package main
import (
"bytes"
"flag"
"io"
"io/ioutil"
"os"
"runtime/pprof"
"github.com/coyim/coyim/config"
"github.com/coyim/coyim/gui"
"github.com/coyim/coyim/gui/settings"
"github.com/coyim/gotk3adapter/gdk_mock"
"github.com/coyim/gotk3adapter/glib_mock"
"github.com/coyim/gotk3adapter/glibi"
"github.com/coyim/gotk3adapter/gtk_mock"
"github.com/coyim/gotk3adapter/gtki"
"github.com/coyim/gotk3adapter/pango_mock"
"github.com/prashantv/gostub"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
. "gopkg.in/check.v1"
)
type MainSuite struct{}
var _ = Suite(&MainSuite{})
func (s *MainSuite) Test_mainInit_setsVersionFromCommit(c *C) {
origBuildTag := BuildTag
origBuildCommit := BuildCommit
origCoyimVersion := coyimVersion
defer func() {
BuildTag = origBuildTag
BuildCommit = origBuildCommit
coyimVersion = origCoyimVersion
}()
BuildTag = "(no tag)"
BuildCommit = "hello abc"
mainInit()
c.Assert(coyimVersion, Equals, "hello abc")
BuildTag = ""
BuildCommit = "hello def"
mainInit()
c.Assert(coyimVersion, Equals, "hello def")
BuildTag = "v42"
BuildCommit = "hello def"
mainInit()
c.Assert(coyimVersion, Equals, "v42")
}
type mockLooper struct {
called bool
}
func (m *mockLooper) Loop() {
m.called = true
}
func captureStdout(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func captureStderr(f func()) string {
old := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
f()
w.Close()
os.Stderr = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func captureStdoutAndStderr(f func()) (stdout, stderr string) {
stdout = captureStdout(func() {
stderr = captureStderr(f)
})
return
}
func (s *MainSuite) Test_main_parsesFlagsAndRunsClient(c *C) {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
defer gostub.Stub(&flag.CommandLine, fs).Reset()
ll := log.StandardLogger()
orgLevel := ll.Level
defer func() {
ll.SetLevel(orgLevel)
}()
ll.SetLevel(log.PanicLevel)
orgCreateGTK := createGTK
defer func() {
createGTK = orgCreateGTK
}()
called1 := false
ml := &mockLooper{}
createGTK = func(g gui.Graphics) looper {
called1 = true
return ml
}
stdout, _ := captureStdoutAndStderr(main)
c.Assert(stdout, Equals, "\n")
c.Assert(fs.Parsed(), Equals, true)
c.Assert(called1, Equals, true)
c.Assert(ml.called, Equals, true)
}
type gtkMockWithSettingsAndBuilder struct {
*gtk_mock.Mock
}
func (*gtkMockWithSettingsAndBuilder) ApplicationNew(appID string, flags glibi.ApplicationFlags) (gtki.Application, error) {
return >k_mock.MockApplication{}, nil
}
func (*gtkMockWithSettingsAndBuilder) SettingsGetDefault() (gtki.Settings, error) {
return >k_mock.MockSettings{}, nil
}
func (*gtkMockWithSettingsAndBuilder) BuilderNew() (gtki.Builder, error) {
return &mockBuilderWithGetObject{}, nil
}
type mockBuilderWithGetObject struct {
*gtk_mock.MockBuilder
}
func (*mockBuilderWithGetObject) GetObject(s string) (glibi.Object, error) {
return &mockListBoxWithStyleContext{}, nil
}
type mockListBoxWithStyleContext struct {
gtk_mock.MockListBox
}
func (*mockListBoxWithStyleContext) GetStyleContext() (gtki.StyleContext, error) {
return &mockStyleContextWithGetProperty2{}, nil
}
type mockStyleContextWithGetProperty2 struct {
gtk_mock.MockStyleContext
}
func (*mockStyleContextWithGetProperty2) GetProperty2(string, gtki.StateFlags) (interface{}, error) {
return &gdk_mock.MockRgba{}, nil
}
type mockGlib struct {
glib_mock.Mock
schemaSourceToReturn glibi.SettingsSchemaSource
settingsNewFullReturn glibi.Settings
settingsNewReturn glibi.Settings
}
type mockSimpleAction struct {
*glib_mock.MockObject
}
func (*mockSimpleAction) GetName() string { return "" }
func (*mockSimpleAction) GetEnabled() bool { return false }
func (*mockSimpleAction) GetState() glibi.Variant { return nil }
func (*mockSimpleAction) GetStateHint() glibi.Variant { return nil }
func (*mockSimpleAction) GetParameterType() glibi.VariantType { return nil }
func (*mockSimpleAction) GetStateType() glibi.VariantType { return nil }
func (*mockSimpleAction) ChangeState(value glibi.Variant) {}
func (*mockSimpleAction) Activate(parameter glibi.Variant) {}
func (*mockSimpleAction) SetEnabled(enabled bool) {}
func (*mockSimpleAction) SetState(value glibi.Variant) {}
func (m *mockGlib) SimpleActionNew(name string, parameterType glibi.VariantType) glibi.SimpleAction {
return &mockSimpleAction{}
}
func (m *mockGlib) SettingsSchemaSourceNewFromDirectory(v1 string, v2 glibi.SettingsSchemaSource, v3 bool) glibi.SettingsSchemaSource {
return m.schemaSourceToReturn
}
func (m *mockGlib) SettingsNewFull(v1 glibi.SettingsSchema, v2 glibi.SettingsBackend, v3 string) glibi.Settings {
return m.settingsNewFullReturn
}
func (m *mockGlib) SettingsNew(string) glibi.Settings {
return m.settingsNewReturn
}
type mockSettingsSchema struct {
glib_mock.MockSettingsSchema
}
type mockSettingsSchemaSource struct {
glib_mock.MockSettingsSchemaSource
lookupReturn glibi.SettingsSchema
}
func (m *mockSettingsSchemaSource) Lookup(v1 string, v2 bool) glibi.SettingsSchema {
return m.lookupReturn
}
func (s *MainSuite) Test_createGTK_works(c *C) {
oldSysDataDir := config.SystemDataDir
defer func() {
config.SystemDataDir = oldSysDataDir
}()
config.SystemDataDir = func() string {
return "somewhere-hopefully-not-existing"
}
orgHooks := hooks
defer func() {
hooks = orgHooks
}()
hooks = func() gui.OSHooks {
return &gui.NoHooks{}
}
ss := &mockSettingsSchemaSource{}
sch := &mockSettingsSchema{}
ss.lookupReturn = sch
mg := &mockGlib{
schemaSourceToReturn: ss,
settingsNewReturn: &glib_mock.MockSettings{},
}
settings.InitSettings(mg)
res := createGTK(gui.CreateGraphics(
>kMockWithSettingsAndBuilder{},
mg,
&gdk_mock.Mock{},
&pango_mock.Mock{},
nil,
))
c.Assert(res, Not(IsNil))
}
func (s *MainSuite) Test_main_printsVersionAndQuits(c *C) {
defer gostub.Stub(config.VersionFlag, true).Reset()
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
defer gostub.Stub(&flag.CommandLine, fs).Reset()
stdout, _ := captureStdoutAndStderr(main)
c.Assert(stdout, Matches, "CoyIM version .*\n")
}
func (s *MainSuite) Test_initLog_setsDebugFlag(c *C) {
ll := log.StandardLogger()
orgLevel := ll.Level
defer func() {
ll.SetLevel(orgLevel)
}()
ll.SetLevel(log.PanicLevel)
orgDebugFlag := *config.DebugFlag
defer func() {
*config.DebugFlag = orgDebugFlag
}()
*config.DebugFlag = true
initLog()
c.Assert(ll.Level, Equals, log.DebugLevel)
}
func (s *MainSuite) Test_initLog_setsTraceFlag(c *C) {
ll := log.StandardLogger()
orgLevel := ll.Level
defer func() {
ll.SetLevel(orgLevel)
}()
ll.SetLevel(log.PanicLevel)
orgDebugFlag := *config.DebugFlag
defer func() {
*config.DebugFlag = orgDebugFlag
}()
*config.DebugFlag = true
orgTraceFlag := *config.TraceFlag
defer func() {
*config.TraceFlag = orgTraceFlag
}()
*config.TraceFlag = true
initLog()
c.Assert(ll.Level, Equals, log.TraceLevel)
}
func (s *MainSuite) Test_stopProfileIfNecessary_works(c *C) {
orgCPUProfileFlag := *config.CPUProfile
defer func() {
*config.CPUProfile = orgCPUProfileFlag
}()
*config.CPUProfile = ""
stopProfileIfNecessary()
*config.CPUProfile = "somewhere-not-existing-hopefully"
stopProfileIfNecessary()
}
func (s *MainSuite) Test_startProfileIfNecessary_failsOnBadFile(c *C) {
defer pprof.StopCPUProfile()
orgCPUProfileFlag := *config.CPUProfile
defer func() {
*config.CPUProfile = orgCPUProfileFlag
}()
*config.CPUProfile = "somewhere/that/hopefully/doesnt/exists"
ll := log.StandardLogger()
orgExitFunc := ll.ExitFunc
defer func() {
ll.ExitFunc = orgExitFunc
}()
called := false
ll.ExitFunc = func(int) {
called = true
}
orgLevel := ll.Level
defer func() {
ll.SetLevel(orgLevel)
}()
ll.SetLevel(log.DebugLevel)
hook := test.NewGlobal()
startProfileIfNecessary()
c.Assert(called, Equals, true)
c.Assert(len(hook.Entries), Equals, 1)
c.Assert(hook.LastEntry().Level, Equals, log.FatalLevel)
c.Assert(hook.LastEntry().Message, Matches, "could not create CPU profile")
c.Assert(hook.LastEntry().Data["error"], ErrorMatches, ".*(no such file or directory|cannot find the (file|path) specified).*")
}
func (s *MainSuite) Test_startProfileIfNecessary_failsOnStarting(c *C) {
defer pprof.StopCPUProfile()
orgCPUProfileFlag := *config.CPUProfile
defer func() {
*config.CPUProfile = orgCPUProfileFlag
}()
*config.CPUProfile = "somewhere/that/hopefully/doesnt/exists"
ll := log.StandardLogger()
orgExitFunc := ll.ExitFunc
defer func() {
ll.ExitFunc = orgExitFunc
}()
called := false
ll.ExitFunc = func(int) {
called = true
}
orgLevel := ll.Level
defer func() {
ll.SetLevel(orgLevel)
}()
ll.SetLevel(log.DebugLevel)
hook := test.NewGlobal()
orgOsCreate := osCreate
defer func() {
osCreate = orgOsCreate
}()
osCreate = func(string) (*os.File, error) {
return nil, nil
}
tmpfile, ex := ioutil.TempFile("", "coyim-main-1-")
c.Assert(ex, IsNil)
defer func() {
// We ignore the results of this close, we just need to make sure
// it's not open before removing it.
_ = tmpfile.Close()
ee := os.Remove(tmpfile.Name())
c.Assert(ee, IsNil)
}()
pprof.StartCPUProfile(tmpfile)
startProfileIfNecessary()
c.Assert(called, Equals, true)
c.Assert(len(hook.Entries), Equals, 1)
c.Assert(hook.LastEntry().Level, Equals, log.FatalLevel)
c.Assert(hook.LastEntry().Message, Matches, "could not start CPU profile")
c.Assert(hook.LastEntry().Data["error"], ErrorMatches, "cpu profiling already in use")
}
func (s *MainSuite) Test_startProfileIfNecessary_works(c *C) {
defer pprof.StopCPUProfile()
tmpfile, ex := ioutil.TempFile("", "coyim-main-2-")
c.Assert(ex, IsNil)
ex2 := tmpfile.Close()
c.Assert(ex2, IsNil)
defer func() {
exe := os.Remove(tmpfile.Name())
c.Assert(exe, IsNil)
}()
orgCPUProfileFlag := *config.CPUProfile
defer func() {
*config.CPUProfile = orgCPUProfileFlag
}()
*config.CPUProfile = tmpfile.Name()
orgOsCreate := osCreate
defer func() {
osCreate = orgOsCreate
}()
osCreate = func(string) (*os.File, error) {
return tmpfile, nil
}
startProfileIfNecessary()
}