-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathfile_test.go
71 lines (69 loc) · 1.41 KB
/
file_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
package formatter
import (
"os"
"path"
"testing"
)
func TestInputFileConfig_ExistsOpen(t *testing.T) {
type fields struct {
Path string
IsStdin bool
}
beforeFunc := func(path string, t *testing.T) {
f, err := os.Create(path)
if err != nil {
t.Errorf("error creating temporary file: %s", err)
}
defer f.Close()
}
afterFunc := func(name string) {
os.Remove(name)
}
tests := []struct {
name string
fields fields
wantErr bool
file string
runBefore bool
runAfter bool
before func(path string, t *testing.T)
after func(path string)
}{
{
name: "File does not exist",
fields: fields{
Path: "",
},
wantErr: true,
},
{
name: "File exists",
fields: fields{
Path: path.Join(os.TempDir(), "inputfile_config_test_exists_2.txt"),
},
wantErr: false,
file: path.Join(os.TempDir(), "inputfile_config_test_exists_2.txt"),
before: beforeFunc,
after: afterFunc,
runBefore: true,
runAfter: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.runBefore {
tt.before(tt.file, t)
}
if tt.runAfter {
defer tt.after(tt.file)
}
i := &InputFileConfig{
Path: tt.fields.Path,
IsStdin: tt.fields.IsStdin,
}
if err := i.ExistsOpen(); (err != nil) != tt.wantErr {
t.Errorf("InputFileConfig.ExistsOpen() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}