-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
88 lines (83 loc) · 1.91 KB
/
util_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
package clade_test
import (
"os"
"path/filepath"
"testing"
"github.com/lesomnus/clade"
"github.com/stretchr/testify/require"
)
func TestResolvePath(t *testing.T) {
wd := must(os.Getwd())
tcs := []struct {
desc string
base string
path string
fallback string
expected string
}{
{
desc: "joined path if `base` is absolute",
base: "/foo/bar",
path: "baz",
fallback: "",
expected: "/foo/bar/baz",
},
{
desc: "joined path with cwd if `base` is relative",
base: "foo/bar",
path: "baz",
fallback: "",
expected: filepath.Join(wd, "foo/bar/baz"),
},
{
desc: "joined path with cwd if `base` is empty",
base: "",
path: "baz",
fallback: "",
expected: filepath.Join(wd, "baz"),
},
{
desc: "`fallback` is joined with `base` if `path` is empty and `base` is absolute",
base: "/foo/bar",
path: "",
fallback: "baz",
expected: "/foo/bar/baz",
},
{
desc: "`fallback` is joined with `base` if `path` is empty and is joined with cwd if `base` is relative",
base: "foo/bar",
path: "",
fallback: "baz",
expected: filepath.Join(wd, "foo/bar/baz"),
},
{
desc: "`fallback` is joined with cwd if `base` and `path` are empty",
base: "",
path: "",
fallback: "baz",
expected: filepath.Join(wd, "baz"),
},
{
desc: "`fallback` is returned as is if it is absolute and `base` and `path` are empty",
base: "",
path: "",
fallback: "/baz",
expected: "/baz",
},
{
desc: "`path` is returned as is if it is absolute",
base: "foo",
path: "/bar",
fallback: "baz",
expected: "/bar",
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
path, err := clade.ResolvePath(tc.base, tc.path, tc.fallback)
require.NoError(err)
require.Equal(tc.expected, path)
})
}
}