-
Notifications
You must be signed in to change notification settings - Fork 0
/
configured_oper_getFile_test.go
198 lines (191 loc) · 4.95 KB
/
configured_oper_getFile_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
package main
import (
"errors"
"fmt"
"io"
"os"
"testing"
"github.com/baalimago/go_away_boilerplate/pkg/testboil"
)
func Test_configuredOper_getFile(t *testing.T) {
createFileWithContent := func(t *testing.T, fileName, content string) *os.File {
f, err := os.Create(fileName)
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
_, err = f.WriteString(content)
if err != nil {
t.Fatalf("failed to write data to file in pre test: %v", err)
}
return f
}
testCases := []struct {
desc string
given struct {
fileName string
fileMode string
// fileName intended to be injected, as it is generated via t.TempDir(). Note that
// fileGenFunc runs before the test/the getFile call, but fileName is generated on
// test start
fileGenFunc func(*testing.T, string) *os.File
}
want struct {
err error
// validate file integrity by checking its content
fileContent string
}
}{
{
desc: "given nonexistent file, no fileMode, some file path, it should return new file",
given: struct {
fileName string
fileMode string
fileGenFunc func(*testing.T, string) *os.File
}{
fileName: fmt.Sprintf("%v/someNewFile", t.TempDir()),
fileMode: "anything",
fileGenFunc: func(*testing.T, string) *os.File { return nil },
},
want: struct {
err error
fileContent string
}{
err: nil,
fileContent: "",
},
},
{
desc: "given existent file, truncate fileMode, new file path, it should return new file",
given: struct {
fileName string
fileMode string
fileGenFunc func(*testing.T, string) *os.File
}{
fileName: fmt.Sprintf("%v/someNewFile", t.TempDir()),
fileMode: "t",
fileGenFunc: func(*testing.T, string) *os.File { return nil },
},
want: struct {
err error
fileContent string
}{
err: nil,
fileContent: "",
},
},
{
desc: "given existent file, truncate fileMode, existing path, it should return new file",
given: struct {
fileName string
fileMode string
fileGenFunc func(*testing.T, string) *os.File
}{
fileName: fmt.Sprintf("%v/someNewFile", t.TempDir()),
fileMode: "t",
fileGenFunc: func(t *testing.T, fileName string) *os.File {
return createFileWithContent(t, fileName, "SHOULD_GO_AWAY")
},
},
want: struct {
err error
fileContent string
}{
err: nil,
fileContent: "",
},
},
{
desc: "given existent file, append fileMode, existing file path, it should return existing file",
given: struct {
fileName string
fileMode string
fileGenFunc func(*testing.T, string) *os.File
}{
fileName: fmt.Sprintf("%v/someNewFile", t.TempDir()),
fileMode: "a",
fileGenFunc: func(t *testing.T, fileName string) *os.File {
return createFileWithContent(t, fileName, "SHOULD_STAY")
},
},
want: struct {
err error
fileContent string
}{
err: nil,
fileContent: "SHOULD_STAY",
},
},
{
desc: "given nonexisting file, append fileMode, new file path, it should return new file",
given: struct {
fileName string
fileMode string
fileGenFunc func(*testing.T, string) *os.File
}{
fileName: fmt.Sprintf("%v/someNewFile", t.TempDir()),
fileMode: "a",
fileGenFunc: func(t *testing.T, _ string) *os.File {
f := testboil.CreateTestFile(t, "someNewFile")
_, err := f.WriteString("SHOULD_BE_IGNORED")
if err != nil {
t.Fatalf("failed to write data to file in pre test: %v", err)
}
return f
},
},
want: struct {
err error
fileContent string
}{
err: nil,
fileContent: "",
},
},
{
desc: "given existing file, quit fileMode, existing file path, it should error",
given: struct {
fileName string
fileMode string
fileGenFunc func(*testing.T, string) *os.File
}{
fileName: fmt.Sprintf("%v/someNewFile", t.TempDir()),
fileMode: "q",
fileGenFunc: func(t *testing.T, fileName string) *os.File {
return createFileWithContent(t, fileName, "doesn't matter, should quit")
},
},
want: struct {
err error
fileContent string
}{
err: UserQuitError,
fileContent: "",
},
},
}
assertFileContent := func(t *testing.T, f *os.File, want string) {
t.Helper()
if f == nil {
return
}
b, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
got := string(b)
if string(b) != want {
t.Fatalf("failed to verify contents of file, want: %v, got: %v", want, got)
}
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
tC.given.fileGenFunc(t, tC.given.fileName)
c := configuredOper{}
gotFile, gotErr := c.getFile(tC.given.fileName, tC.given.fileMode)
if tC.want.err != nil && !errors.Is(gotErr, tC.want.err) {
t.Fatalf("expected error: %v, got: %v", tC.want.err, gotErr)
}
assertFileContent(t, gotFile, tC.want.fileContent)
})
}
}