-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_create_test.go
166 lines (155 loc) · 4.22 KB
/
cmd_create_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
package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strings"
"sync"
"testing"
"github.com/x-motemen/ghq/cmdutil"
)
func TestDoCreate(t *testing.T) {
defer func(orig func(cmd *exec.Cmd) error) {
cmdutil.CommandRunner = orig
}(cmdutil.CommandRunner)
var lastCmd *exec.Cmd
commandRunner := func(cmd *exec.Cmd) error {
lastCmd = cmd
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
want []string
wantDir string
errStr string
setup func(t *testing.T)
cmdRun func(cmd *exec.Cmd) error
skipOnWin bool
}{{
name: "simple",
input: []string{"create", "motemen/ghqq"},
want: []string{"git", "init"},
wantDir: filepath.Join(tmpd, "github.com/motemen/ghqq"),
}, {
name: "empty directory exists",
input: []string{"create", "motemen/ghqqq"},
want: []string{"git", "init"},
setup: func(t *testing.T) {
os.MkdirAll(filepath.Join(tmpd, "github.com/motemen/ghqqq"), 0755)
},
wantDir: filepath.Join(tmpd, "github.com/motemen/ghqqq"),
}, {
name: "invalid VCS",
input: []string{"create", "example.com/goooo/gooo"},
cmdRun: func(cmd *exec.Cmd) error {
lastCmd = cmd
return errors.New("bad repository")
},
errStr: "unsupported VCS",
}, {
name: "Mercurial",
input: []string{"create", "--vcs=hg", "motemen/ghq-hg"},
want: []string{"hg", "init"},
wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-hg"),
}, {
name: "Darcs",
input: []string{"create", "--vcs=darcs", "motemen/ghq-darcs"},
want: []string{"darcs", "init"},
wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-darcs"),
}, {
name: "Bazzar",
input: []string{"create", "--vcs=bzr", "motemen/ghq-bzr"},
want: []string{"bzr", "init"},
wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-bzr"),
}, {
name: "Fossil",
input: []string{"create", "--vcs=fossil", "motemen/ghq-fossil"},
want: []string{"fossil", "open", fossilRepoName},
wantDir: filepath.Join(tmpd, "github.com/motemen/ghq-fossil"),
}, {
name: "unsupported VCS",
input: []string{"create", "--vcs=svn", "motemen/ghq-svn"},
errStr: "unsupported VCS",
}, {
name: "not permitted",
input: []string{"create", "motemen/ghq-notpermitted"},
setup: func(t *testing.T) {
f := filepath.Join(tmpd, "github.com/motemen/ghq-notpermitted")
os.MkdirAll(f, 0)
t.Cleanup(func() { os.Chmod(f, 0755) })
},
errStr: "permission denied",
skipOnWin: true,
}, {
name: "not empty",
input: []string{"create", "motemen/ghq-notempty"},
setup: func(t *testing.T) {
f := filepath.Join(tmpd, "github.com/motemen/ghq-notempty", "dummy")
os.MkdirAll(f, 0755)
},
errStr: "already exists and not empty",
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skipOnWin && runtime.GOOS == "windows" {
t.SkipNow()
}
lastCmd = nil
if tc.setup != nil {
tc.setup(t)
}
cmdutil.CommandRunner = commandRunner
if tc.cmdRun != nil {
cmdutil.CommandRunner = tc.cmdRun
}
var err error
out, _, _ := capture(func() {
err = newApp().Run(append([]string{""}, tc.input...))
})
out = strings.TrimSpace(out)
if tc.errStr == "" {
if err != nil {
t.Errorf("error should be nil, but: %s", err)
return
}
} else {
if err == nil {
t.Errorf("err should not be nil")
return
}
if e, g := tc.errStr, err.Error(); !strings.Contains(g, e) {
t.Errorf("err.Error() should contains %q, but not: %q", e, g)
}
}
if len(tc.want) > 0 {
if !reflect.DeepEqual(lastCmd.Args, tc.want) {
t.Errorf("cmd.Args = %v, want: %v", lastCmd.Args, tc.want)
}
if lastCmd.Dir != tc.wantDir {
t.Errorf("cmd.Dir = %q, want: %q", lastCmd.Dir, tc.wantDir)
}
}
if tc.errStr == "" {
if out != tc.wantDir {
t.Errorf("cmd.Dir = %q, want: %q", out, tc.wantDir)
}
} else {
if out != "" {
t.Errorf("output should be empty but: %s", out)
}
}
})
}
}