forked from git-hooks/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
117 lines (97 loc) · 2.54 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
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
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestGetGitRoot(t *testing.T) {
root, err := getGitRepoRoot()
assert.Nil(t, err)
assert.Equal(t, "git-hooks", filepath.Base(root))
}
func TestGetDirPath(t *testing.T) {
path, err := getGitDirPath()
assert.Nil(t, err)
assert.Equal(t, ".git", filepath.Base(path))
}
func TestGitExec(t *testing.T) {
identity, err := gitExec(GIT["FirstCommit"])
assert.Nil(t, err)
assert.Equal(t, "553ec650fd4f90003774e2ff00b10bc9aa9ec802", identity)
}
func TestGitExecWithDir(t *testing.T) {
wd, err := os.Getwd()
assert.Nil(t, err)
identity, err := gitExecWithDir(wd, GIT["FirstCommit"])
assert.Nil(t, err)
assert.Equal(t, "553ec650fd4f90003774e2ff00b10bc9aa9ec802", identity)
}
func TestBind(t *testing.T) {
sum := 0
f := bind(func(a, b int) {
sum = a + b
}, 1, 2)
f(&cli.Context{})
assert.Equal(t, 3, sum)
}
func TestExists(t *testing.T) {
isExist, err := exists("notExistFileName")
assert.Nil(t, err)
assert.False(t, isExist)
}
func TestDownloadFromUrl(t *testing.T) {
content := "Hello, client"
// start test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, content)
}))
defer ts.Close()
fileName, err := downloadFromUrl(ts.URL)
assert.Nil(t, err)
file, err := os.Open(fileName)
assert.Nil(t, err)
fileinfo, err := file.Stat()
assert.Nil(t, err)
b := make([]byte, fileinfo.Size())
_, err = file.Read(b)
assert.Nil(t, err)
assert.Equal(t, string(b), content)
}
func TestExtract(t *testing.T) {
fileName, err := extract("./fixtures/test.tar.gz")
assert.Nil(t, err)
file, err := os.Open(fileName)
assert.Nil(t, err)
fileinfo, err := file.Stat()
assert.Nil(t, err)
bytes := make([]byte, fileinfo.Size())
_, err = file.Read(bytes)
assert.Nil(t, err)
// vim store the file with extra newline at the EOF
assert.Equal(t, "test\n", string(bytes))
}
func TestAbsExePath(t *testing.T) {
path, err := absExePath("ls")
assert.Nil(t, err)
assert.Equal(t, "/bin/ls", path)
// should follow symlic
temp, err := ioutil.TempDir(os.TempDir(), "git-hooks-test")
assert.Nil(t, err)
os.Setenv("PATH", temp+":$PATH")
err = os.Symlink("/bin/ls", filepath.Join(temp, "ls"))
assert.Nil(t, err)
path, err = absExePath("ls")
assert.Nil(t, err)
assert.Equal(t, "/bin/ls", path)
}
func TestIsExecutable(t *testing.T) {
fileinfo, err := os.Stat("/bin/ls")
assert.Nil(t, err)
assert.True(t, isExecutable(fileinfo))
}