-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve-path_test.go
129 lines (115 loc) · 3.32 KB
/
resolve-path_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
package nef_test
import (
"fmt"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok
nef "github.com/snivilised/nefilim"
)
var _ = Describe("ResolvePath", Ordered, func() {
DescribeTable("Overrides provided",
func(entry *RPEntry) {
mocks := nef.ResolveMocks{
HomeFunc: fakeHomeResolver,
AbsFunc: fakeAbsResolver,
}
if filepath.Separator == '/' {
actual := nef.ResolvePath(entry.path, mocks)
Expect(actual).To(Equal(entry.expect))
} else {
normalisedPath := strings.ReplaceAll(entry.path, "/", string(filepath.Separator))
normalisedExpect := strings.ReplaceAll(entry.expect, "/", string(filepath.Separator))
actual := nef.ResolvePath(normalisedPath, mocks)
Expect(actual).To(Equal(normalisedExpect))
}
},
func(entry *RPEntry) string {
return fmt.Sprintf("🧪 ===> given: '%v', should: '%v'", entry.given, entry.should)
},
Entry(nil, &RPEntry{
given: "path is a valid absolute path",
should: "return path unmodified",
path: "/home/rabbitweed/foo",
expect: "/home/rabbitweed/foo",
}),
Entry(nil, &RPEntry{
given: "path contains leading ~",
should: "replace ~ with home path",
path: "~/foo",
expect: "/home/rabbitweed/foo",
}),
Entry(nil, &RPEntry{
given: "path is relative to cwd",
should: "replace ~ with home path",
path: "./foo",
expect: "/home/rabbitweed/music/xpander/foo",
}),
Entry(nil, &RPEntry{
given: "path is relative to parent",
should: "replace ~ with home path",
path: "../foo",
expect: "/home/rabbitweed/music/foo",
}),
Entry(nil, &RPEntry{
given: "path is relative to grand parent",
should: "replace ~ with home path",
path: "../../foo",
expect: "/home/rabbitweed/foo",
}),
)
DescribeTable("Overrides provided (errors)",
func(entry *RPEntry) {
mocks := nef.ResolveMocks{
HomeFunc: errorHomeResolver,
AbsFunc: errorAbsResolver,
}
if filepath.Separator == '/' {
actual := nef.ResolvePath(entry.path, mocks)
Expect(actual).To(Equal(entry.expect))
} else {
normalisedPath := strings.ReplaceAll(entry.path, "/", string(filepath.Separator))
normalisedExpect := strings.ReplaceAll(entry.expect, "/", string(filepath.Separator))
actual := nef.ResolvePath(normalisedPath, mocks)
Expect(actual).To(Equal(normalisedExpect))
}
},
func(entry *RPEntry) string {
return fmt.Sprintf("🧪 ===> given: '%v', should: '%v'", entry.given, entry.should)
},
Entry(nil, &RPEntry{
given: "path contains leading ~",
should: "return path unmodified",
path: "~/foo",
expect: "~/foo",
}),
Entry(nil, &RPEntry{
given: "path is a valid absolute path",
should: "return path unmodified",
path: "/home/rabbitweed/foo",
expect: "/home/rabbitweed/foo",
}),
)
When("No overrides provided", func() {
Context("and: home", func() {
It("🧪 should: not fail", func() {
nef.ResolvePath("~/")
})
})
Context("and: abs cwd", func() {
It("🧪 should: not fail", func() {
nef.ResolvePath("./")
})
})
Context("and: abs parent", func() {
It("🧪 should: not fail", func() {
nef.ResolvePath("../")
})
})
Context("and: abs grand parent", func() {
It("🧪 should: not fail", func() {
nef.ResolvePath("../..")
})
})
})
})