-
Notifications
You must be signed in to change notification settings - Fork 3
/
install_test.go
97 lines (86 loc) · 2.05 KB
/
install_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
package main
import (
"fmt"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/dirk/quickhook/internal/test"
"github.com/dirk/quickhook/repo"
)
func TestInstallPreCommitYes(t *testing.T) {
tempDir := test.NewTempDir(t, 0)
tempDir.RequireExec("git", "init", "--quiet", ".")
tempDir.MkdirAll(".quickhook", "pre-commit")
output, err := tempDir.ExecQuickhook("install", "--yes")
assert.NoError(t, err)
shimPath := path.Join(".git", "hooks", "pre-commit")
assert.Equal(t,
fmt.Sprintf("Installed shim %v", shimPath),
strings.TrimSpace(output))
assert.FileExists(t,
path.Join(tempDir.Root, shimPath))
}
func TestInstallPreCommitMutatingYes(t *testing.T) {
tempDir := test.NewTempDir(t, 0)
tempDir.RequireExec("git", "init", "--quiet", ".")
tempDir.MkdirAll(".quickhook", "pre-commit-mutating")
output, err := tempDir.ExecQuickhook("install", "--yes")
assert.NoError(t, err)
shimPath := path.Join(".git", "hooks", "pre-commit")
assert.Equal(t,
fmt.Sprintf("Installed shim %v", shimPath),
strings.TrimSpace(output))
assert.FileExists(t,
path.Join(tempDir.Root, shimPath))
}
func TestInstallNoQuickhookDirectory(t *testing.T) {
tempDir := test.NewTempDir(t, 0)
tempDir.RequireExec("git", "init", "--quiet", ".")
output, err := tempDir.ExecQuickhook("install", "--yes")
assert.Error(t, err)
assert.Contains(t, output, "Missing hooks directory")
}
func TestPromptForInstall(t *testing.T) {
ptyTests := []struct {
name string
stdin string
expected bool
}{
{
"yes",
"yes\n",
true,
},
{
"short yes",
"y\n",
true,
},
{
"no",
"no\n",
false,
},
{
"short no",
"n\n",
false,
},
{
"no input",
"",
false,
},
}
for _, tt := range ptyTests {
t.Run(tt.name, func(t *testing.T) {
tempDir := test.NewTempDir(t, 0)
repo := &repo.Repo{Root: tempDir.Root}
stdin := strings.NewReader(tt.stdin)
shouldInstall, err := promptForInstallShim(stdin, repo, ".git/hooks/pre-commit")
assert.NoError(t, err)
assert.Equal(t, tt.expected, shouldInstall)
})
}
}