-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile.go
100 lines (79 loc) · 2.66 KB
/
file.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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package assert
import (
"errors"
"io/fs"
"os"
)
func (a *Assertion) FileExists(path string, msg ...interface{}) *Assertion {
a.TB().Helper()
if _, err := os.Stat(path); err != nil && !errors.Is(err, fs.ErrExist) {
return a.Assert(false, NewFailure("FileExists", msg, map[string]interface{}{"err": err}))
}
return a
}
func (a *Assertion) FileNotExists(path string, msg ...interface{}) *Assertion {
a.TB().Helper()
_, err := os.Stat(path)
if err == nil {
return a.Assert(false, NewFailure("FileNotExists", msg, nil))
}
if errors.Is(err, fs.ErrExist) {
return a.Assert(false, NewFailure("FileNotExists", msg, map[string]interface{}{"err": err}))
}
return a
}
func (a *Assertion) FileExistsFS(fsys fs.FS, path string, msg ...interface{}) *Assertion {
a.TB().Helper()
if _, err := fs.Stat(fsys, path); err != nil && !errors.Is(err, fs.ErrExist) {
return a.Assert(false, NewFailure("FileExistsFS", msg, map[string]interface{}{"err": err}))
}
return a
}
func (a *Assertion) FileNotExistsFS(fsys fs.FS, path string, msg ...interface{}) *Assertion {
a.TB().Helper()
_, err := fs.Stat(fsys, path)
if err == nil {
return a.Assert(false, NewFailure("FileNotExistsFS", msg, nil))
}
if errors.Is(err, fs.ErrExist) {
return a.Assert(false, NewFailure("FileNotExistsFS", msg, map[string]interface{}{"err": err}))
}
return a
}
// IsDir 断言 path 是个目录
func (a *Assertion) IsDir(path string, msg ...interface{}) *Assertion {
a.TB().Helper()
s, err := os.Stat(path)
if err != nil {
return a.Assert(false, NewFailure("IsDir", msg, map[string]interface{}{"err": err}))
}
return a.Assert(s.IsDir(), NewFailure("IsDir", msg, nil))
}
func (a *Assertion) IsDirFS(fsys fs.FS, path string, msg ...interface{}) *Assertion {
a.TB().Helper()
s, err := fs.Stat(fsys, path)
if err != nil {
return a.Assert(false, NewFailure("IsDirFS", msg, map[string]interface{}{"err": err}))
}
return a.Assert(s.IsDir(), NewFailure("IsDirFS", msg, nil))
}
// IsNotDir 断言 path 不存在或是非目录
func (a *Assertion) IsNotDir(path string, msg ...interface{}) *Assertion {
a.TB().Helper()
s, err := os.Stat(path)
if err != nil {
return a.Assert(false, NewFailure("IsNotDir", msg, map[string]interface{}{"err": err}))
}
return a.Assert(!s.IsDir(), NewFailure("IsNotDir", msg, nil))
}
func (a *Assertion) IsNotDirFS(fsys fs.FS, path string, msg ...interface{}) *Assertion {
a.TB().Helper()
s, err := os.Stat(path)
if err != nil {
return a.Assert(false, NewFailure("IsNotDirFS", msg, map[string]interface{}{"err": err}))
}
return a.Assert(!s.IsDir(), NewFailure("IsNotDirFS", msg, nil))
}