-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnefilim-suite_test.go
217 lines (175 loc) Β· 4.83 KB
/
nefilim-suite_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
package nef_test
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok
nef "github.com/snivilised/nefilim"
lab "github.com/snivilised/nefilim/internal/laboratory"
"github.com/snivilised/nefilim/test/luna"
)
func TestNefilim(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Nefilim Suite")
}
type CalcType uint
const (
CalcTypeAbsolute CalcType = iota
CalcTypeRelative
)
func (c CalcType) String() string {
if c == CalcTypeAbsolute {
return "ABSOLUTE"
}
return "RELATIVE"
}
type (
ensureTE struct {
given string
should string
relative string
expected string
asFile bool
}
RPEntry struct {
given string
should string
path string
expect string
}
manyResultAction func(calc nef.PathCalc) []string
singleAsserter func(string)
manyAsserter func([]string)
pairAsserter func(string, string)
calcTE struct {
given string
should string
}
genericCalcTE[I, R string | []string] struct {
calcTE
input I
expect map[CalcType]R
}
calcVariadicToOneTE struct {
calcTE
input []string
expect map[CalcType]string
}
pair struct {
dir string
file string
}
calcOneToPairTE struct {
calcTE
input string
expect map[CalcType]pair
}
funcFS[T any] func(entry fsTE[T], fS T)
fsTE[T any] struct {
given string
should string
note string
op string
overwrite bool
directory bool
require string
target string
from string
to string
arrange funcFS[T]
action funcFS[T]
}
)
var (
fakeHome = filepath.Join(string(filepath.Separator), "home", "rabbitweed")
fakeAbsCwd = filepath.Join(string(filepath.Separator), "home", "rabbitweed", "music", "xpander")
fakeAbsParent = filepath.Join(string(filepath.Separator), "home", "rabbitweed", "music")
)
func scratch(root string) { // should we re-create scratch too, so the tests don't have to?
scratchPath := filepath.Join(root, lab.Static.FS.Scratch)
if _, err := os.Stat(scratchPath); err == nil {
Expect(os.RemoveAll(scratchPath)).To(Succeed(),
fmt.Sprintf("failed to delete existing directory %q", scratchPath),
)
}
}
// require ensures that a path exists. If files are also provided,
// it will create these files too. The files are relative to the root
// and should be prefixed by parent; that is to say, when a test needs
// scratch/foo.txt, parent = 'scratch' and file = 'scratch/foo.txt';
// ie te file still needs to be relative to root, not parent.
func require(root, parent string, files ...string) error {
if err := os.MkdirAll(filepath.Join(root, parent), lab.Perms.Dir.Perm()); err != nil {
return fmt.Errorf("failed to create directory: %q (%w)", parent, err)
}
for _, name := range files {
handle, err := os.Create(filepath.Join(root, name))
if err != nil {
return fmt.Errorf("failed to create file: %q (%w)", name, err)
}
handle.Close()
}
return nil
}
func requires(fS nef.WriterFS, root, parent string, files ...string) error {
if err := fS.MakeDirAll(filepath.Join(root, parent), lab.Perms.Dir.Perm()); err != nil {
return fmt.Errorf("failed to create directory: %q (%w)", parent, err)
}
for _, name := range files {
handle, err := os.Create(filepath.Join(root, name))
if err != nil {
return fmt.Errorf("failed to create file: %q (%w)", name, err)
}
handle.Close()
}
return nil
}
func fakeHomeResolver() (string, error) {
return fakeHome, nil
}
func fakeAbsResolver(path string) (string, error) {
if strings.HasPrefix(path, "..") {
return filepath.Join(fakeAbsParent, path[2:]), nil
}
if strings.HasPrefix(path, ".") {
return filepath.Join(fakeAbsCwd, path[1:]), nil
}
return path, nil
}
func errorHomeResolver() (string, error) {
return "", errors.New("failed to resolve home")
}
func errorAbsResolver(_ string) (string, error) {
return "", errors.New("failed to resolve abs")
}
func Normalise(p string) string {
return strings.ReplaceAll(p, "/", string(filepath.Separator))
}
func Because(name, because string) string {
return fmt.Sprintf("β for item named: '%v', because: '%v'", name, because)
}
func Reason(name string) string {
return fmt.Sprintf("β for item named: '%v'", name)
}
func Log() string {
return luna.Repo("Test/test.log")
}
func IsInvalidPathError(err error, reason string) {
Expect(nef.IsInvalidPathError(err)).To(BeTrue(),
fmt.Sprintf("not NewInvalidPathError, %q", reason),
)
}
func IsSameDirectoryMoveRejectionError(err error, reason string) {
Expect(nef.IsRejectSameDirMoveError(err)).To(BeTrue(),
fmt.Sprintf("not SameDirectoryMoveRejectionError, %q", reason),
)
}
func IsDifferentDirectoryChangeRejectionError(err error, reason string) {
Expect(nef.IsRejectDifferentDirChangeError(err)).To(BeTrue(),
fmt.Sprintf("not DifferentDirectoryChangeRejectionError, %q", reason),
)
}