-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations_test.go
More file actions
174 lines (158 loc) · 4.06 KB
/
annotations_test.go
File metadata and controls
174 lines (158 loc) · 4.06 KB
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
package github
import (
"os"
"testing"
)
func TestFormatTitle(t *testing.T) {
tests := []struct {
name string
statusCode int
errorCode string
ruleName string
want string
}{
{
name: "409 conflict",
statusCode: 409,
errorCode: "DEPLOYMENT_IN_PROGRESS",
ruleName: "",
want: "Deployment Conflict",
},
{
name: "423 with rule name",
statusCode: 423,
errorCode: "NO_DEPLOY_WINDOW",
ruleName: "No Deploy Fridays",
want: "Deployment Blocked: No Deploy Fridays",
},
{
name: "423 without rule name",
statusCode: 423,
errorCode: "NO_DEPLOY_WINDOW",
ruleName: "",
want: "Deployment Blocked by Schedule",
},
{
name: "428 with rule name",
statusCode: 428,
errorCode: "FLOW_VIOLATION",
ruleName: "Staging First",
want: "FLOW_VIOLATION: Staging First",
},
{
name: "428 without rule name",
statusCode: 428,
errorCode: "INSUFFICIENT_SOAK_TIME",
ruleName: "",
want: "INSUFFICIENT_SOAK_TIME",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := formatTitle(tt.statusCode, tt.errorCode, tt.ruleName)
if got != tt.want {
t.Errorf("formatTitle() = %v, want %v", got, tt.want)
}
})
}
}
func TestEscapeWorkflowCommand(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "no special chars",
input: "simple message",
want: "simple message",
},
{
name: "with percent",
input: "100% complete",
want: "100%25 complete",
},
{
name: "with newline",
input: "line1\nline2",
want: "line1%0Aline2",
},
{
name: "with carriage return",
input: "line1\rline2",
want: "line1%0Dline2",
},
{
name: "multiple special chars",
input: "50%\ndone\r",
want: "50%25%0Adone%0D",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := escapeWorkflowCommand(tt.input)
if got != tt.want {
t.Errorf("escapeWorkflowCommand() = %v, want %v", got, tt.want)
}
})
}
}
func TestWriteErrorAnnotation_NotInGitHub(t *testing.T) {
// Ensure GITHUB_ACTIONS is not set
os.Unsetenv("GITHUB_ACTIONS")
// Should not panic or error when not in GitHub Actions
WriteErrorAnnotation(423, "NO_DEPLOY_WINDOW", "Test message", "Test Rule", "", nil)
}
func TestWriteErrorAnnotation_InGitHub(t *testing.T) {
// Set GitHub Actions environment
os.Setenv("GITHUB_ACTIONS", "true")
defer os.Unsetenv("GITHUB_ACTIONS")
// Create a temporary file for GITHUB_STEP_SUMMARY
tmpFile, err := os.CreateTemp("", "github-summary-*.md")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpFile.Name())
tmpFile.Close()
os.Setenv("GITHUB_STEP_SUMMARY", tmpFile.Name())
defer os.Unsetenv("GITHUB_STEP_SUMMARY")
// Call the function
details := map[string]interface{}{
"rule_name": "Test Rule",
"window_start": "2025-11-27T00:00:00Z",
}
WriteErrorAnnotation(423, "NO_DEPLOY_WINDOW", "No deployments allowed", "Test Rule", "2025-11-27T23:59:59Z", details)
// Read the summary file
content, err := os.ReadFile(tmpFile.Name())
if err != nil {
t.Fatalf("Failed to read summary file: %v", err)
}
summary := string(content)
// Verify key content is present
if !contains(summary, "Versioner Deployment Rejected") {
t.Error("Summary should contain 'Versioner Deployment Rejected'")
}
if !contains(summary, "NO_DEPLOY_WINDOW") {
t.Error("Summary should contain error code")
}
if !contains(summary, "Test Rule") {
t.Error("Summary should contain rule name")
}
if !contains(summary, "No deployments allowed") {
t.Error("Summary should contain message")
}
if !contains(summary, "2025-11-27T23:59:59Z") {
t.Error("Summary should contain retry_after timestamp")
}
}
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && findSubstring(s, substr))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}