-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
cmd_rm_test.go
168 lines (156 loc) · 3.75 KB
/
cmd_rm_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
package main
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
"testing"
"github.com/x-motemen/ghq/cmdutil"
)
func TestRmCommand(t *testing.T) {
defer func(orig func(cmd *exec.Cmd) error) {
cmdutil.CommandRunner = orig
}(cmdutil.CommandRunner)
commandRunner := func(cmd *exec.Cmd) error {
return nil
}
defer func(orig string) { _home = orig }(_home)
_home = ""
homeOnce = &sync.Once{}
tmpd := newTempDir(t)
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
setEnv(t, envGhqRoot, tmpd)
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
testCases := []struct {
name string
input []string
setup func(t *testing.T)
expectErr bool
cmdRun func(cmd *exec.Cmd) error
skipOnWin bool
}{
{
name: "simple",
input: []string{"rm", "motemen/ghqq"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq"), 0755)
},
expectErr: false,
},
{
name: "empty directory",
input: []string{"rm", "motemen/ghqqq"},
setup: func(t *testing.T) {},
expectErr: true,
},
{
name: "incorrect repository name",
input: []string{"rm", "example.com/goooo/gooo"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq"), 0755)
},
expectErr: true,
},
{
name: "permission denied",
input: []string{"rm", "motemen/ghqq"},
setup: func(t *testing.T) {
f := filepath.Join(tmpd, "github.com", "motemen", "ghqq")
os.MkdirAll(f, 0000)
t.Cleanup(func() {
os.Chmod(f, 0755)
})
},
expectErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skipOnWin && runtime.GOOS == "windows" {
t.SkipNow()
}
if tc.setup != nil {
tc.setup(t)
}
cmdutil.CommandRunner = commandRunner
if tc.cmdRun != nil {
cmdutil.CommandRunner = tc.cmdRun
}
})
}
}
func TestRmDryRunCommand(t *testing.T) {
defer func(orig func(cmd *exec.Cmd) error) {
cmdutil.CommandRunner = orig
}(cmdutil.CommandRunner)
commandRunner := func(cmd *exec.Cmd) error {
return nil
}
defer func(orig string) { _home = orig }(_home)
_home = ""
homeOnce = &sync.Once{}
tmpd := newTempDir(t)
defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
setEnv(t, envGhqRoot, tmpd)
_localRepositoryRoots = nil
localRepoOnce = &sync.Once{}
testCases := []struct {
name string
input []string
setup func(t *testing.T)
expectErr bool
cmdRun func(cmd *exec.Cmd) error
skipOnWin bool
}{
{
name: "simple",
input: []string{"rm", "--dry-run", "motemen/ghqq"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq"), 0755)
},
expectErr: false,
},
{
name: "empty directory",
input: []string{"rm", "--dry-run", "motemen/ghqqq"},
setup: func(t *testing.T) {},
expectErr: true,
},
{
name: "incorrect repository name",
input: []string{"rm", "--dry-run", "example.com/goooo/gooo"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com", "motemen", "ghqq"), 0755)
},
expectErr: true,
},
{
name: "permission denied",
input: []string{"rm", "--dry-run", "motemen/ghqq"},
setup: func(t *testing.T) {
f := filepath.Join(tmpd, "github.com", "motemen", "ghqq")
os.MkdirAll(f, 0000)
t.Cleanup(func() {
os.Chmod(f, 0755)
})
},
expectErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skipOnWin && runtime.GOOS == "windows" {
t.SkipNow()
}
if tc.setup != nil {
tc.setup(t)
}
cmdutil.CommandRunner = commandRunner
if tc.cmdRun != nil {
cmdutil.CommandRunner = tc.cmdRun
}
})
}
}