-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify_test.go
146 lines (138 loc) · 3.46 KB
/
notify_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
package main
import (
"fmt"
"testing"
)
func TestParseNotifyMode(t *testing.T) {
tests := []struct {
input string
expected notifyMode
wantErr bool
}{
{"always", notifyAlways, false},
{"never", notifyNever, false},
{"on-failure", notifyOnFailure, false},
{"", notifyOnFailure, false},
{"invalid", "", true},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("parseNotifyMode(%q)", tt.input), func(t *testing.T) {
got, err := parseNotifyMode(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("parseNotifyMode(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
return
}
if got != tt.expected {
t.Errorf("parseNotifyMode(%q) = %v, want %v", tt.input, got, tt.expected)
}
})
}
}
func TestNotifyIfNeeded(t *testing.T) {
var notified bool
mockNotify := func(jobName string, completed CompletedJob) error {
notified = true
return nil
}
tests := []struct {
name string
mode notifyMode
job CompletedJob
shouldNotify bool
}{
{
name: "always mode success",
mode: notifyAlways,
job: CompletedJob{ExitStatus: 0},
shouldNotify: true,
},
{
name: "always mode failure",
mode: notifyAlways,
job: CompletedJob{ExitStatus: 1},
shouldNotify: true,
},
{
name: "never mode success",
mode: notifyNever,
job: CompletedJob{ExitStatus: 0},
shouldNotify: false,
},
{
name: "never mode failure",
mode: notifyNever,
job: CompletedJob{ExitStatus: 1},
shouldNotify: false,
},
{
name: "on-failure mode success",
mode: notifyOnFailure,
job: CompletedJob{ExitStatus: 0},
shouldNotify: false,
},
{
name: "on-failure mode failure",
mode: notifyOnFailure,
job: CompletedJob{ExitStatus: 1},
shouldNotify: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
notified = false
err := notifyIfNeeded(mockNotify, tt.mode, "test-job", tt.job)
if err != nil {
t.Errorf("notifyIfNeeded() error = %v", err)
}
if notified != tt.shouldNotify {
t.Errorf("notifyIfNeeded() notified = %v, want %v", notified, tt.shouldNotify)
}
})
}
}
func TestFormatMessage(t *testing.T) {
tests := []struct {
name string
job CompletedJob
wantSubject string
wantBody string
wantError bool
}{
{
name: "success case",
job: CompletedJob{ExitStatus: 0},
wantSubject: `Job "test-job" succeeded`,
wantBody: "",
wantError: false,
},
{
name: "failure with exit status",
job: CompletedJob{ExitStatus: 1},
wantSubject: `Job "test-job" failed`,
wantBody: "Exit status: 1\n\n",
wantError: false,
},
{
name: "failure with error message",
job: CompletedJob{Error: "test error"},
wantSubject: `Job "test-job" failed`,
wantBody: "Error: test error\n\n",
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
subject, body, err := formatMessage(nil, "test-job", tt.job)
if (err != nil) != tt.wantError {
t.Errorf("formatMessage() error = %v, wantError %v", err, tt.wantError)
return
}
if subject != tt.wantSubject {
t.Errorf("formatMessage() subject = %v, want %v", subject, tt.wantSubject)
}
if body != tt.wantBody {
t.Errorf("formatMessage() body = %q, want %q", body, tt.wantBody)
}
})
}
}