-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebhook_test.go
84 lines (70 loc) · 1.62 KB
/
webhook_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
package caddy_webhook
import (
"fmt"
"os"
"testing"
"github.com/alecthomas/assert"
"github.com/go-git/go-git/v5"
)
func TestGetRepoNameFromURL(t *testing.T) {
testCases := []struct {
link string
name string
err bool
}{
{"https://github.com/WingLim/caddy-webhook.git", "caddy-webhook", false},
{"[email protected]:WingLim/caddy-webhook.git", "caddy-webhook", false},
{"ftp://balabala.git", "", true},
}
for _, tc := range testCases {
name, err := getRepoNameFromURL(tc.link)
if tc.err {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tc.name, name)
}
}
}
func TestIsEmptyOrGit(t *testing.T) {
testCases := []struct {
path string
expected bool
}{
{"test/not_exist", true},
{"test/empty", true},
{"test/not_empty", false},
{"test/file", false},
{"test/dir_git", true},
}
err := os.Mkdir("test", 0666)
defer func() {
err := os.RemoveAll("test")
assert.Nil(t, err)
}()
assert.Nil(t, err)
err = os.Mkdir("test/empty", 0666)
assert.Nil(t, err)
err = os.Mkdir("test/not_empty", 0666)
assert.Nil(t, err)
file1, err := os.Create("test/not_empty/test.txt")
defer func(file1 *os.File) {
err := file1.Close()
assert.Nil(t, err)
}(file1)
assert.Nil(t, err)
file2, err := os.Create("test/file")
defer func(file2 *os.File) {
err := file2.Close()
assert.Nil(t, err)
}(file2)
assert.Nil(t, err)
err = os.Mkdir("test/dir", 0666)
assert.Nil(t, err)
_, err = git.PlainInit("test/dir_git", false)
assert.Nil(t, err)
for i, tc := range testCases {
actual := isEmptyOrGit(tc.path, nil)
assert.Equal(t, tc.expected, actual, fmt.Sprintf("case %d", i))
}
}