1
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
2
|
|
|
// Author: Nikita Koryabkin |
3
|
|
|
// Email: [email protected] |
4
|
|
|
// Telegram: https://t.me/Apologiz |
5
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
6
|
|
|
|
7
|
|
|
package file |
8
|
|
|
|
9
|
|
|
import ( |
10
|
|
|
"fmt" |
11
|
|
|
"io" |
12
|
|
|
"os" |
13
|
|
|
"reflect" |
14
|
|
|
"testing" |
15
|
|
|
|
16
|
|
|
"github.com/mylockerteam/alog/util" |
17
|
|
|
"github.com/spf13/afero" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
func init() { |
21
|
|
|
fs = afero.NewMemMapFs() |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
type argsCreateDirectoryIfNotExist struct { |
25
|
|
|
dirPath string |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
type testCreateDirectoryIfNotExist struct { |
29
|
|
|
name string |
30
|
|
|
args argsCreateDirectoryIfNotExist |
31
|
|
|
wantErr bool |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func casesCreateDirectoryIfNotExist() []testCreateDirectoryIfNotExist { |
35
|
|
|
return []testCreateDirectoryIfNotExist{ |
36
|
|
|
{ |
37
|
|
|
args: argsCreateDirectoryIfNotExist{ |
38
|
|
|
dirPath: "/", |
39
|
|
|
}, |
40
|
|
|
wantErr: false, |
41
|
|
|
}, |
42
|
|
|
{ |
43
|
|
|
args: argsCreateDirectoryIfNotExist{ |
44
|
|
|
dirPath: "", |
45
|
|
|
}, |
46
|
|
|
wantErr: false, |
47
|
|
|
}, |
48
|
|
|
{ |
49
|
|
|
args: argsCreateDirectoryIfNotExist{ |
50
|
|
|
dirPath: fmt.Sprintf("/tmp/%s/", util.RandString(10)), |
51
|
|
|
}, |
52
|
|
|
wantErr: false, |
53
|
|
|
}, |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func Test_createDirectoryIfNotExist(t *testing.T) { |
58
|
|
|
tests := casesCreateDirectoryIfNotExist() |
59
|
|
|
for _, tt := range tests { |
60
|
|
|
t.Run(tt.name, func(t *testing.T) { |
61
|
|
|
if err := createDirectoryIfNotExist(tt.args.dirPath); (err != nil) != tt.wantErr { |
62
|
|
|
t.Errorf("createDirectoryIfNotExist() error = %v, wantErr %v", err, tt.wantErr) |
63
|
|
|
} |
64
|
|
|
}) |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
type argsOpenFile struct { |
69
|
|
|
filePath string |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
type testsOpenFile struct { |
73
|
|
|
name string |
74
|
|
|
args argsOpenFile |
75
|
|
|
want afero.File |
76
|
|
|
wantErr bool |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
func casesOpenFile() []testsOpenFile { |
80
|
|
|
return []testsOpenFile{ |
81
|
|
|
{ |
82
|
|
|
args: argsOpenFile{ |
83
|
|
|
filePath: fmt.Sprintf("/tmp/%s/", util.RandString(10)), |
84
|
|
|
}, |
85
|
|
|
wantErr: false, |
86
|
|
|
}, |
87
|
|
|
{ |
88
|
|
|
args: argsOpenFile{ |
89
|
|
|
filePath: "/", |
90
|
|
|
}, |
91
|
|
|
wantErr: false, |
92
|
|
|
}, |
93
|
|
|
{ |
94
|
|
|
args: argsOpenFile{ |
95
|
|
|
filePath: "", |
96
|
|
|
}, |
97
|
|
|
wantErr: true, |
98
|
|
|
}, |
99
|
|
|
{ |
100
|
|
|
args: argsOpenFile{ |
101
|
|
|
filePath: "/dev/stdout", |
102
|
|
|
}, |
103
|
|
|
wantErr: false, |
104
|
|
|
}, |
105
|
|
|
{ |
106
|
|
|
args: argsOpenFile{ |
107
|
|
|
filePath: "/dev/stderr", |
108
|
|
|
}, |
109
|
|
|
wantErr: false, |
110
|
|
|
}, |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
func Test_openFile(t *testing.T) { |
115
|
|
|
tests := casesOpenFile() |
116
|
|
|
for _, tt := range tests { |
117
|
|
|
t.Run(tt.name, func(t *testing.T) { |
118
|
|
|
got, err := openFile(tt.args.filePath) |
119
|
|
|
if (err != nil) != tt.wantErr { |
120
|
|
|
t.Errorf("openFile() error = %v, wantErr %v", err, tt.wantErr) |
121
|
|
|
return |
122
|
|
|
} |
123
|
|
|
if (err != nil) != tt.wantErr && !reflect.DeepEqual(got, tt.want) { |
124
|
|
|
t.Errorf("openFile() = %v, want %v", got, tt.want) |
125
|
|
|
} |
126
|
|
|
}) |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
type argsAddDirectory struct { |
131
|
|
|
filePath string |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
type testsAddDirectory struct { |
135
|
|
|
name string |
136
|
|
|
args argsAddDirectory |
137
|
|
|
wantErr bool |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
func casesAddDirectory() []testsAddDirectory { |
141
|
|
|
return []testsAddDirectory{ |
142
|
|
|
{ |
143
|
|
|
args: argsAddDirectory{ |
144
|
|
|
filePath: "/", |
145
|
|
|
}, |
146
|
|
|
wantErr: false, |
147
|
|
|
}, |
148
|
|
|
{ |
149
|
|
|
args: argsAddDirectory{ |
150
|
|
|
filePath: "", |
151
|
|
|
}, |
152
|
|
|
wantErr: true, |
153
|
|
|
}, |
154
|
|
|
{ |
155
|
|
|
args: argsAddDirectory{ |
156
|
|
|
filePath: fmt.Sprintf("/tmp/%s/", util.RandString(10)), |
157
|
|
|
}, |
158
|
|
|
wantErr: false, |
159
|
|
|
}, |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
func Test_addDirectory(t *testing.T) { |
164
|
|
|
tests := casesAddDirectory() |
165
|
|
|
for _, tt := range tests { |
166
|
|
|
t.Run(tt.name, func(t *testing.T) { |
167
|
|
|
if err := addDirectory(tt.args.filePath); (err != nil) != tt.wantErr { |
168
|
|
|
t.Errorf("addDirectory() error = %v, wantErr %v", err, tt.wantErr) |
169
|
|
|
} |
170
|
|
|
}) |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
func TestGet(t *testing.T) { |
175
|
|
|
type args struct { |
176
|
|
|
filePath string |
177
|
|
|
} |
178
|
|
|
tests := []struct { |
179
|
|
|
name string |
180
|
|
|
args args |
181
|
|
|
want io.Writer |
182
|
|
|
}{ |
183
|
|
|
{ |
184
|
|
|
args: args{ |
185
|
|
|
filePath: "", |
186
|
|
|
}, |
187
|
|
|
want: &Strategy{}, |
188
|
|
|
}, |
189
|
|
|
{ |
190
|
|
|
args: args{ |
191
|
|
|
filePath: fmt.Sprintf("/tmp/%s/", util.RandString(10)), |
192
|
|
|
}, |
193
|
|
|
want: &Strategy{}, |
194
|
|
|
}, |
195
|
|
|
} |
196
|
|
|
for _, tt := range tests { |
197
|
|
|
t.Run(tt.name, func(t *testing.T) { |
198
|
|
|
if got := Get(tt.args.filePath); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
199
|
|
|
t.Errorf("Get() = %v, want %v", got, tt.want) |
200
|
|
|
} |
201
|
|
|
}) |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
func TestWrite(t *testing.T) { |
206
|
|
|
type args struct { |
207
|
|
|
p []byte |
208
|
|
|
} |
209
|
|
|
tests := []struct { |
210
|
|
|
name string |
211
|
|
|
args args |
212
|
|
|
strategy Strategy |
213
|
|
|
wantErr bool |
214
|
|
|
}{ |
215
|
|
|
{ |
216
|
|
|
args: args{ |
217
|
|
|
[]byte("Hello, Alog!"), |
218
|
|
|
}, |
219
|
|
|
strategy: Strategy{}, |
220
|
|
|
wantErr: true, |
221
|
|
|
}, |
222
|
|
|
{ |
223
|
|
|
args: args{ |
224
|
|
|
[]byte("Hello, Alog!"), |
225
|
|
|
}, |
226
|
|
|
strategy: Strategy{ |
227
|
|
|
File: os.Stdout, |
228
|
|
|
}, |
229
|
|
|
wantErr: false, |
230
|
|
|
}, |
231
|
|
|
} |
232
|
|
|
for _, tt := range tests { |
233
|
|
|
t.Run(tt.name, func(t *testing.T) { |
234
|
|
|
if _, err := tt.strategy.Write(tt.args.p); (err != nil) != tt.wantErr { |
235
|
|
|
t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr) |
236
|
|
|
} |
237
|
|
|
}) |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|