-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathutils_test.go
More file actions
157 lines (134 loc) · 4.12 KB
/
utils_test.go
File metadata and controls
157 lines (134 loc) · 4.12 KB
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
package utils
import (
"context"
"os"
"path"
"testing"
"github.com/stretchr/testify/require"
)
func TestExpandPath_NoPath_NoBase(t *testing.T) {
path, err := ExpandPath("", "", "")
require.NoError(t, err)
require.Equal(t, ".", path)
}
func TestExpandPath_NoPath_RelBase(t *testing.T) {
testCases := []string{"repo", "./repo"}
for _, base := range testCases {
path, err := ExpandPath("", base, "")
require.NoError(t, err)
require.Equal(t, "repo", path)
}
}
func TestExpandPath_NoPath_AbsBase(t *testing.T) {
path, err := ExpandPath("", "/repo", "")
require.NoError(t, err)
require.Equal(t, "/repo", path)
}
func TestExpandtPath_RelPath_NoBase(t *testing.T) {
testCases := []string{"repo", "./repo"}
for _, pth := range testCases {
path, err := ExpandPath(pth, "", "")
require.NoError(t, err)
require.Equal(t, "repo", path)
}
}
func TestExpandtPath_RelPath_RelBase(t *testing.T) {
path, err := ExpandPath("repo", "data", "")
require.NoError(t, err)
require.Equal(t, "data/repo", path)
}
func TestExpandtPath_RelPath_AbsBase(t *testing.T) {
path, err := ExpandPath("repo", "/data", "")
require.NoError(t, err)
require.Equal(t, "/data/repo", path)
}
func TestExpandtPath_AbsPath_NoBase(t *testing.T) {
path, err := ExpandPath("/repo", "", "")
require.NoError(t, err)
require.Equal(t, "/repo", path)
}
func TestExpandtPath_AbsPath_RelBase(t *testing.T) {
path, err := ExpandPath("/repo", "data", "")
require.NoError(t, err)
require.Equal(t, "/repo", path)
}
func TestExpandtPath_AbsPath_AbsBase(t *testing.T) {
path, err := ExpandPath("/repo", "/data", "")
require.NoError(t, err)
require.Equal(t, "/repo", path)
}
func TestExpandPath_BareTilde_NoHome(t *testing.T) {
path, err := ExpandPath("~", "", "")
require.NoError(t, err)
require.Equal(t, ".", path)
}
func TestExpandPath_BareTilde_RelHome(t *testing.T) {
path, err := ExpandPath("~", "", "user")
require.NoError(t, err)
require.Equal(t, "user", path)
}
func TestExpandPath_BareTilde_AbsHome(t *testing.T) {
path, err := ExpandPath("~", "", "/home/user")
require.NoError(t, err)
require.Equal(t, "/home/user", path)
}
func TestExpandtPath_TildeWithPath_NoHome(t *testing.T) {
path, err := ExpandPath("~/repo", "", "")
require.NoError(t, err)
require.Equal(t, "repo", path)
}
func TestExpandtPath_TildeWithPath_RelHome(t *testing.T) {
path, err := ExpandPath("~/repo", "", "user")
require.NoError(t, err)
require.Equal(t, "user/repo", path)
}
func TestExpandtPath_TildeWithPath_AbsHome(t *testing.T) {
path, err := ExpandPath("~/repo", "", "/home/user")
require.NoError(t, err)
require.Equal(t, "/home/user/repo", path)
}
func TestExpandtPath_ErrorTildeUsernameNotSupported_BareTildeUsername(t *testing.T) {
path, err := ExpandPath("~username", "", "")
require.ErrorContains(t, err, "~username syntax is not supported")
require.Equal(t, "", path)
}
func TestExpandtPath_ErrorTildeUsernameNotSupported_TildeUsernameWithPath(t *testing.T) {
path, err := ExpandPath("~username/repo", "", "")
require.ErrorContains(t, err, "~username syntax is not supported")
require.Equal(t, "", path)
}
func TestMkdirAll_AbsPath_NotExists(t *testing.T) {
absPath := path.Join(t.TempDir(), "a/b/c")
require.NoDirExists(t, absPath)
err := MkdirAll(context.Background(), absPath, -1, -1, 0o755)
require.NoError(t, err)
require.DirExists(t, absPath)
}
func TestMkdirAll_AbsPath_Exists(t *testing.T) {
absPath, err := os.Getwd()
require.NoError(t, err)
err = MkdirAll(context.Background(), absPath, -1, -1, 0o755)
require.NoError(t, err)
require.DirExists(t, absPath)
}
func TestMkdirAll_RelPath_NotExists(t *testing.T) {
cwd := t.TempDir()
os.Chdir(cwd)
relPath := "a/b/c"
absPath := path.Join(cwd, relPath)
require.NoDirExists(t, absPath)
err := MkdirAll(context.Background(), relPath, -1, -1, 0o755)
require.NoError(t, err)
require.DirExists(t, absPath)
}
func TestMkdirAll_RelPath_Exists(t *testing.T) {
cwd := t.TempDir()
os.Chdir(cwd)
relPath := "a/b/c"
absPath := path.Join(cwd, relPath)
err := os.MkdirAll(absPath, 0o755)
require.NoError(t, err)
err = MkdirAll(context.Background(), relPath, -1, -1, 0o755)
require.NoError(t, err)
require.DirExists(t, absPath)
}