-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnsys_test.go
175 lines (160 loc) · 4.06 KB
/
gnsys_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
package gnsys_test
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/gnames/gnsys"
"github.com/matryer/is"
"github.com/stretchr/testify/assert"
)
func TestPing(t *testing.T) {
assert := assert.New(t)
ping := gnsys.Ping("google.com:80", 3)
assert.True(ping)
ping = gnsys.Ping("notAserver:80", 3)
assert.False(ping)
}
func TestConvertTilda(t *testing.T) {
is := is.New(t)
tests := []struct {
name, input string
outputSameSize, errNil bool
}{
{"no tilda", "/somedir", true, true},
{"tilda", "~/somedir", false, true},
}
for _, v := range tests {
t.Run(v.name, func(_ *testing.T) {
path, err := gnsys.ConvertTilda(v.input)
is.Equal(len(v.input) == len(path), v.outputSameSize)
is.Equal(v.errNil, err == nil)
})
}
}
func TestFileExists(t *testing.T) {
is := is.New(t)
tests := []struct {
name, path string
fileExists, errNil bool
}{
{"file exists", "testdata/text.txt", true, true},
{"file does not exist", "testdata/text2.txt", false, true},
{"is dir", "testdata", false, false},
}
for _, v := range tests {
t.Run(v.name, func(_ *testing.T) {
exists, err := gnsys.FileExists(v.path)
is.Equal(v.fileExists, exists)
is.Equal(v.errNil, err == nil)
})
}
}
func TestDirExists(t *testing.T) {
is := is.New(t)
tests := []struct {
name, path string
dirExists, dirEmpty, errNil bool
}{
{"dir exists notempty", "testdata", true, false, true},
{"dir exists not", "testdata/nodir", false, false, false},
}
for _, v := range tests {
t.Run(v.name, func(_ *testing.T) {
exists, empty, err := gnsys.DirExists(v.path)
is.Equal(v.dirExists, exists)
is.Equal(v.dirEmpty, empty)
is.Equal(v.errNil, err == nil)
})
}
}
func TestIsFile(t *testing.T) {
is := is.New(t)
tests := []struct {
name, path string
isFile bool
}{
{"is dir", "testdata", false},
{"is file", "testdata/text.txt", true},
{"is not file", "testdata/nofile", false},
}
for _, v := range tests {
t.Run(v.name, func(_ *testing.T) {
isfile := gnsys.IsFile(v.path)
is.Equal(v.isFile, isfile)
})
}
}
func TestIsDir(t *testing.T) {
is := is.New(t)
tests := []struct {
name, path string
isDir bool
}{
{"is dir", "testdata", true},
{"is not dir", "testdata/text.txt", false},
{"is not dir", "testdata/nodir", false},
}
for _, v := range tests {
t.Run(v.name, func(_ *testing.T) {
isdir := gnsys.IsDir(v.path)
is.Equal(v.isDir, isdir)
})
}
}
func TestGetDirState(t *testing.T) {
is := is.New(t)
makeEmptyDir(t)
tests := []struct {
name, path string
state gnsys.DirState
}{
{"is dir", "testdata", gnsys.DirNotEmpty},
{"is empty dir", "testdata/empty_dir", gnsys.DirEmpty},
{"is not dir", "testdata/text.txt", gnsys.NotDir},
{"absent", "testdata/absent_from_tests", gnsys.DirAbsent},
}
for _, v := range tests {
t.Run(v.name, func(_ *testing.T) {
state := gnsys.GetDirState(v.path)
is.Equal(v.state, state)
})
}
}
func makeEmptyDir(t *testing.T) {
dir := filepath.Join("testdata/empty_dir")
os.Mkdir(dir, 0775)
}
func TestDownload(t *testing.T) {
is := is.New(t)
url := "http://opendata.globalnames.org/dwca/183-sherborn.tar.gz"
path := os.TempDir()
filePath, err := gnsys.Download(url, path, false)
is.NoErr(err)
exists, _ := gnsys.FileExists(filePath)
is.True(exists)
err = os.Remove(filePath)
is.NoErr(err)
}
func TestSplitPath(t *testing.T) {
assert := assert.New(t)
tests := []struct {
msg, path, dir, file, ext string
}{
{"empty", "", "", "", ""},
{"root1", "/", "/", "", ""},
{"any", "c", "c", "", ""},
{"dir1", "/one/two/three/", "/one/two/three", "", ""},
{"dir3", "~/one/two/three/", "~/one/two/three", "", ""},
{"file1", "~/one/two/three", "~/one/two", "three", ""},
{"file2", "./one/two/three.txt", "one/two", "three", ".txt"},
{"file3", "../one/two/three.txt.gz", "../one/two", "three.txt", ".gz"},
}
for _, v := range tests {
d, f, e := gnsys.SplitPath(v.path)
fmt.Printf("%s, %s, %s", d, f, e)
assert.Equal(v.dir, d, v.msg+":dir")
assert.Equal(v.file, f, v.msg+":file")
assert.Equal(v.ext, e, v.msg+":ext")
}
}