-
Notifications
You must be signed in to change notification settings - Fork 38
/
publish_test.go
219 lines (203 loc) · 6.21 KB
/
publish_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
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_verifyStatusOfTheCreatedRelease(t *testing.T) {
tests := []struct {
name string
config Configs
expectedStatus string
}{
{
"Given the user fraction is equal to 0 and the status is not set when the release is created then expect the status to be COMPLETED",
Configs{UserFraction: 0}, releaseStatusCompleted,
},
{
"Given the user fraction is greather than 0 and the status is not set when the release is created then expect the status to be IN_PROGRESS",
Configs{UserFraction: 0.5}, releaseStatusInProgress,
},
{
"Given the status when the release is created then expect the status to be the same as in the config",
Configs{Status: releaseStatusDraft}, releaseStatusDraft,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trackRelease, err := createTrackRelease(tt.config, []int64{})
assert.NoError(t, err)
assert.Equal(t, tt.expectedStatus, trackRelease.Status)
})
}
}
func Test_verifyUserFractionOfTheCreatedRelease(t *testing.T) {
tests := []struct {
name string
config Configs
expectedUserFraction float64
}{
{
"Given status is IN_PROGRESS and the user fraction is set when the release is created then expect the user fraction to be applied",
Configs{UserFraction: 1, Status: releaseStatusInProgress}, 1,
},
{
"Given status is HALTED and the user fraction is set when the release is created then expect the user fraction to be applied",
Configs{UserFraction: 1, Status: releaseStatusHalted}, 1,
},
{
"Given status is DRAFT and the user fraction is set when the release is created then expect the user fraction not to be applied",
Configs{UserFraction: 1, Status: releaseStatusDraft}, 0,
},
{
"Given status is COMPLETED and the user fraction is set when the release is created then expect the user fraction not to be applied",
Configs{UserFraction: 1, Status: releaseStatusCompleted}, 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trackRelease, err := createTrackRelease(tt.config, []int64{})
assert.NoError(t, err)
assert.Equal(t, tt.expectedUserFraction, trackRelease.UserFraction)
})
}
}
func Test_releaseStatusFromConfig(t *testing.T) {
tests := []struct {
name string
userFraction float64
want string
}{
{"nonStagedRollout", 0, releaseStatusCompleted},
{"nonStagedRollout", 0.5, releaseStatusInProgress},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := releaseStatusFromConfig(tt.userFraction); got != tt.want {
t.Errorf("releaseStatusFromConfig() = %v, want %v", got, tt.want)
}
})
}
}
func Test_expFileInfo(t *testing.T) {
tests := []struct {
name string
expFileConfigEntry string
want string
want1 string
wantErr bool
}{
{"valid1", "main:/file/path/1.obb", "/file/path/1.obb", "main", false},
{"valid2", "type:/file/path/2.obb", "/file/path/2.obb", "type", false},
{"invalid1", "/file/path/2.obb", "", "", true},
{"invalid2", "", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := expFileInfo(tt.expFileConfigEntry)
if (err != nil) != tt.wantErr {
t.Errorf("expFileInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("expFileInfo() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("expFileInfo() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func Test_validateExpansionFilePath(t *testing.T) {
tests := []struct {
name string
expFilePath string
want bool
}{
{"valid1", "main:", true},
{"valid2", "patch:", true},
{"invalid1", "", false},
{"invalid2", "something", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateExpansionFileConfig(tt.expFilePath); got != tt.want {
t.Errorf("validateExpansionFileConfig() = %v, want %v", got, tt.want)
}
})
}
}
func Test_readLocalisedRecentChanges(t *testing.T) {
createTestFiles := func(localeToNote map[string]string) (string, error) {
tmpDir := t.TempDir()
for locale, notes := range localeToNote {
if err := os.WriteFile(filepath.Join(tmpDir, "whatsnew-"+locale), []byte(notes), 0600); err != nil {
return "", err
}
}
return tmpDir, nil
}
tests := []struct {
name string
testFiles map[string]string
want map[string]string
wantErr bool
}{
{
name: "1 language: en-US",
testFiles: map[string]string{"en-US": "English"},
want: map[string]string{"en-US": "English"},
wantErr: false,
},
{
name: "2 language: en-US",
testFiles: map[string]string{"en-US": "English", "de-DE": "German"},
want: map[string]string{"en-US": "English", "de-DE": "German"},
wantErr: false,
},
{
name: "no second subtag",
testFiles: map[string]string{"ca": "Catalan"},
want: map[string]string{"ca": "Catalan"},
wantErr: false,
},
{
name: "Latin American Spanish",
testFiles: map[string]string{"es-419": "Latin American Spanish"},
want: map[string]string{"es-419": "Latin American Spanish"},
wantErr: false,
},
{
// "sr-Latn-RS" represents Serbian ('sr') written using Latin script
//('Latn') as used in Serbia ('RS').
name: "Latin American Spanish",
testFiles: map[string]string{"sr-Latn-RS": "Serbian"},
want: map[string]string{"sr-Latn-RS": "Serbian"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testDir, err := createTestFiles(tt.testFiles)
if err != nil {
t.Fatalf("setup: failed to create test files, error: %s", err)
}
defer func() {
err := os.RemoveAll(testDir)
if err != nil {
t.Logf("Faield to remove test dir, error: %s", err)
}
}()
got, err := readLocalisedRecentChanges(testDir)
if (err != nil) != tt.wantErr {
t.Errorf("readLocalisedRecentChanges() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("readLocalisedRecentChanges() = %v, want %v", got, tt.want)
}
})
}
}