-
Notifications
You must be signed in to change notification settings - Fork 38
/
config_test.go
239 lines (226 loc) · 6.64 KB
/
config_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
package main
import (
"os"
"path/filepath"
"reflect"
"testing"
"github.com/bitrise-io/go-steputils/stepconf"
)
func Test_fraction(t *testing.T) {
type cfgs struct {
UserFraction float64 `env:"user_fraction,range]0.0..1.0["`
Input string
Value float64
WantErr bool
}
for _, cfg := range []cfgs{
{
Input: "",
Value: 0,
WantErr: false,
},
{
Input: "0.3",
Value: 0.3,
WantErr: false,
},
{
Input: "0",
Value: 0,
WantErr: true,
},
} {
if err := os.Setenv("user_fraction", cfg.Input); err != nil {
t.Fatal(err)
}
if err := stepconf.Parse(&cfg); err != nil && !cfg.WantErr {
t.Fatal(err)
}
if cfg.UserFraction != cfg.Value {
t.Fatal("eeeh man")
}
}
}
func Test_parseInputList(t *testing.T) {
tests := []struct {
name string
list string
wantApps []string
}{
{
name: "empty app list",
list: "",
wantApps: nil,
},
{
name: "newline separated list",
list: "app.apk\napp.aab\n \n",
wantApps: []string{"app.apk", "app.aab"},
},
{
name: "pipe separated list",
list: "|app.apk|app.aab|",
wantApps: []string{"app.apk", "app.aab"},
},
{
name: "pipe and newline separated list",
list: "\napp1.apk|app2.apk\napp.aab|",
wantApps: []string{"app1.apk", "app2.apk", "app.aab"},
},
{
name: "pipe and newline separated list",
list: "/bitrise/deploy/app-bitrise-signed.aab\n/bitrise/deploy/app.aab",
wantApps: []string{"/bitrise/deploy/app-bitrise-signed.aab", "/bitrise/deploy/app.aab"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotApps := parseInputList(tt.list); !reflect.DeepEqual(gotApps, tt.wantApps) {
t.Errorf("parseInputList() = %v, want %v", gotApps, tt.wantApps)
}
})
}
}
func TestConfigs_appPaths(t *testing.T) {
tests := []struct {
name string
config Configs
wantApps []string
wantWarnings []string
}{
{
name: "empty test",
config: Configs{
AppPath: "",
},
wantApps: nil,
wantWarnings: nil,
},
{
name: "prefers aab",
config: Configs{
AppPath: "app.apk|app.aab",
},
wantApps: []string{"app.aab"},
wantWarnings: []string{"Both .aab and .apk files provided, using the .aab file(s): app.aab"},
},
{
name: "multiple .aab",
config: Configs{
AppPath: "app.aab\napp1.aab",
},
wantApps: []string{"app.aab", "app1.aab"},
},
{
name: "unknown extension",
config: Configs{
AppPath: "mapping.txt",
},
wantApps: nil,
wantWarnings: []string{"unknown app path extension in path: mapping.txt, supported extensions: .apk, .aab"},
},
{
name: "newline (\n) as a character",
config: Configs{
AppPath: `/bitrise/deploy/app-bitrise-signed.aab\n/bitrise/deploy/app.aab`,
},
wantApps: []string{"/bitrise/deploy/app-bitrise-signed.aab", "/bitrise/deploy/app.aab"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotApps, gotWarnings := tt.config.appPaths()
if !reflect.DeepEqual(gotApps, tt.wantApps) {
t.Errorf("Configs.appPaths() gotApps = %v, want %v", gotApps, tt.wantApps)
}
if !reflect.DeepEqual(gotWarnings, tt.wantWarnings) {
t.Errorf("Configs.appPaths() gotWarnings = %v, want %v", gotWarnings, tt.wantWarnings)
}
})
}
}
func TestConfigs_mappingPaths(t *testing.T) {
tmpDir := t.TempDir()
tests := []struct {
name string
configs Configs
wantErr bool
createFiles []string
}{
{
name: "no mapping file",
configs: Configs{},
wantErr: false,
},
{
name: "single mapping file",
configs: Configs{MappingFile: filepath.Join(tmpDir, "single", "mapping.txt")},
wantErr: false,
createFiles: []string{filepath.Join(tmpDir, "single", "mapping.txt")},
},
{
name: "single non-existent mapping file",
configs: Configs{MappingFile: filepath.Join(tmpDir, "single_nonexistent", "mapping.txt")},
wantErr: true,
},
{
name: "multiple existing mapping files",
configs: Configs{MappingFile: filepath.Join(tmpDir, "multiple", "mapping.txt") + "|" + filepath.Join(tmpDir, "multiple", "mapping2.txt")},
wantErr: false,
createFiles: []string{filepath.Join(tmpDir, "multiple", "mapping.txt"), filepath.Join(tmpDir, "multiple", "mapping2.txt")},
},
{
name: "1 existing 1 invalid mapping file",
configs: Configs{MappingFile: filepath.Join(tmpDir, "multiple_nonexistent", "mapping.txt") + "\n" + filepath.Join(tmpDir, "multiple_nonexistent", "mapping2.txt")},
wantErr: true,
createFiles: []string{filepath.Join(tmpDir, "multiple_nonexistent", "mapping.txt")},
},
}
for _, tt := range tests {
for _, path := range tt.createFiles {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
t.Errorf("failed to create path: %s", err)
}
_, err = os.Create(path)
if err != nil {
t.Errorf("failed to create file: %s", err)
}
}
gotErr := tt.configs.validateMappingFile()
if tt.wantErr && gotErr == nil {
t.Errorf("%s: wanted error but result is nil", tt.name)
} else if !tt.wantErr && gotErr != nil {
t.Errorf("%s: wanted no error, got: %v", tt.name, gotErr)
}
}
}
func Test_expansionFiles(t *testing.T) {
tests := []struct {
name string
appPaths []string
expansionFilePathConfig string
entries []string
wantErr bool
}{
{"mainOnly", []string{"x.apk", "y.apk", "z.apk"}, "main:a.obb|main:b.obb|main:c.obb", []string{"main:a.obb", "main:b.obb", "main:c.obb"}, false},
{"pathOnly", []string{"x.apk", "y.apk", "z.apk"}, "patch:a.obb|patch:b.obb|patch:c.obb", []string{"patch:a.obb", "patch:b.obb", "patch:c.obb"}, false},
{"mixed", []string{"x.apk", "y.apk", "z.apk"}, "main:a.obb|patch:b.obb|patch:c.obb", []string{"main:a.obb", "patch:b.obb", "patch:c.obb"}, false},
{"omit", []string{"x.apk", "y.apk", "z.apk"}, "main:a.obb||patch:c.obb", []string{"main:a.obb", "", "patch:c.obb"}, false},
{"multipleOmit", []string{"w.apk", "x.apk", "y.apk", "z.apk"}, "main:a.obb|||patch:c.obb", []string{"main:a.obb", "", "", "patch:c.obb"}, false},
{"invalid1", []string{"x.apk", "y.apk", "z.apk"}, "main:a.obb", []string{}, true},
{"invalid2", []string{"x.apk", "y.apk", "z.apk"}, "", []string{}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := expansionFiles(tt.appPaths, tt.expansionFilePathConfig)
if (err != nil) != tt.wantErr {
t.Errorf("expansionFiles() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.entries) {
t.Errorf("expansionFiles() got1 = %v, want %v", got, tt.entries)
}
})
}
}