-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathfile_util_test.go
More file actions
100 lines (91 loc) · 2.29 KB
/
Copy pathfile_util_test.go
File metadata and controls
100 lines (91 loc) · 2.29 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
package gosnowflake
import (
"os/user"
"path/filepath"
"runtime"
"testing"
)
func TestGetDigestAndSizeForInvalidDir(t *testing.T) {
fileUtil := new(snowflakeFileUtil)
digest, size, err := fileUtil.getDigestAndSizeForFile("/home/file.txt")
if digest != "" {
t.Fatal("should be empty")
}
if size != 0 {
t.Fatal("should be 0")
}
if err == nil {
t.Fatal("should have failed")
}
}
type tcBaseName struct {
in string
out string
}
func TestBaseName(t *testing.T) {
testcases := []tcBaseName{
{"/tmp", "tmp"},
{"/home/desktop/.", ""},
{"/home/desktop/..", ""},
{".", ""},
{"..", ""},
{"/", ""},
{"/home/desktop/", ""},
{"archive.tar.gz", "archive.tar.gz"},
{"/path/to/archive.tar.gz", "archive.tar.gz"},
{"trailing-dot.tar.gz.", "trailing-dot.tar.gz."},
{"/path/to/trailing-dot.tar.gz.", "trailing-dot.tar.gz."},
{"/path/to/Untitled 1.", "Untitled 1."},
}
for _, test := range testcases {
t.Run(test.in, func(t *testing.T) {
actual := baseName(test.in)
assertEqualE(t, actual, test.out, "baseName:", test.in)
})
}
}
func TestBaseNameWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Windows-specific path tests")
}
testcases := []tcBaseName{
{`C:\Users\file.txt`, "file.txt"},
{`C:\Users\`, ""},
// filepath.Base: "If the path consists entirely of separators, Base returns a single separator"
// "C:\" -> "\" which is not a file name, but a root path, so should be rejected
{`C:\`, ""},
{`C:\Users\trailing-dot.txt.`, "trailing-dot.txt."},
{`C:\path\to\Untitled 1.`, "Untitled 1."},
{`C:\path\to\.`, ""},
{`C:\path\to\..`, ""},
}
for _, test := range testcases {
t.Run(test.in, func(t *testing.T) {
actual := baseName(test.in)
assertEqualE(t, actual, test.out, "baseName:", test.in)
})
}
}
func TestExpandUser(t *testing.T) {
skipOnMissingHome(t)
usr, err := user.Current()
if err != nil {
t.Fatal(err)
}
homeDir := usr.HomeDir
user, err := expandUser("~")
if err != nil {
t.Fatal(err)
}
if homeDir != user {
t.Fatalf("failed to expand user, expected: %v, got: %v", homeDir, user)
}
user, err = expandUser("~/storage")
if err != nil {
t.Fatal(err)
}
expectedPath := filepath.Join(homeDir, "storage")
if expectedPath != user {
t.Fatalf("failed to expand user, expected: %v, got: %v", expectedPath, user)
}
}