-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
82 lines (66 loc) · 2.43 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
package main
import (
"fmt"
"os"
"reflect"
"testing"
"github.com/bouk/monkey"
"github.com/evandroflores/claimr/cmd"
"github.com/evandroflores/claimr/model"
"github.com/shomali11/slacker"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestEnvironmentToken(t *testing.T) {
currentEnv := os.Getenv("CLAIMR_TOKEN")
os.Unsetenv("CLAIMR_TOKEN")
defer func() { os.Setenv("CLAIMR_TOKEN", currentEnv) }()
expectedMsg := "Claimr slack bot token unset. Set CLAIMR_TOKEN to continue."
mockLogFatal := func(msg ...interface{}) {
assert.Equal(t, expectedMsg, msg[0])
panic("log.Fatal called")
}
patchLog := monkey.Patch(log.Fatal, mockLogFatal)
defer patchLog.Unpatch()
assert.PanicsWithValue(t, "log.Fatal called", main, "log.Fatal was not called")
}
func TestSlackerCommands(t *testing.T) {
var mockSlacker *slacker.Slacker
var commands []model.Command
patchCommand := monkey.PatchInstanceMethod(reflect.TypeOf(mockSlacker), "Command",
func(slacker *slacker.Slacker, usage string, description string, handler func(request *slacker.Request, response slacker.ResponseWriter)) {
if usage == "help" {
return //ignoring help slacker command
}
commands = append(commands, model.Command{Usage: usage, Description: description, Handler: handler})
})
patchListen := monkey.PatchInstanceMethod(reflect.TypeOf(mockSlacker), "Listen", func(*slacker.Slacker) error {
assert.CallerInfo()
return nil
})
main()
for i, command := range cmd.CommandList() {
assert.Equal(t, command.Description, commands[i].Description)
}
patchCommand.Unpatch()
patchListen.Unpatch()
}
func TestSlackerListenError(t *testing.T) {
var mockSlacker *slacker.Slacker
patchCommand := monkey.PatchInstanceMethod(reflect.TypeOf(mockSlacker), "Command",
func(slacker *slacker.Slacker, usage string, description string, handler func(request *slacker.Request, response slacker.ResponseWriter)) {
})
patchListen := monkey.PatchInstanceMethod(reflect.TypeOf(mockSlacker), "Listen", func(*slacker.Slacker) error {
return fmt.Errorf("Simulated Error")
})
expectedMsg := fmt.Errorf("Simulated Error")
mockLogFatal := func(msg ...interface{}) {
assert.Equal(t, expectedMsg, msg[0])
panic("log.Fatal called")
}
patchLog := monkey.Patch(log.Fatal, mockLogFatal)
defer patchCommand.Unpatch()
defer patchListen.Unpatch()
defer patchLog.Unpatch()
assert.PanicsWithValue(t, "log.Fatal called", main, "log.Fatal called was not called")
}