-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_helper.go
More file actions
248 lines (219 loc) · 5.16 KB
/
test_helper.go
File metadata and controls
248 lines (219 loc) · 5.16 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package grs
import (
"bytes"
"fmt"
"jcheng/grs/shexec"
"os"
"os/exec"
"path"
"strings"
)
// GitTestHelper provides convenience methods for setting up a Git repo. To simplify error-handling, if any convenience
// method fails, the error is stored and future calls are ignored. The current error state can be retrieved using
// Err().
//
// It applies the Scanner.Err() technique as mentioned in https://blog.golang.org/errors-are-values
type GitTestHelper struct {
err error
errCause string
git string
runner shexec.CommandRunner
debugExec bool
wd string
}
type option func(*GitTestHelper)
func NewGitTestHelper(options ...option) *GitTestHelper {
retval := &GitTestHelper{
err: nil,
git: ResolveGit(),
runner: &shexec.ExecRunner{},
debugExec: false,
}
for _, o := range options {
o(retval)
}
if retval.wd == "" {
retval.wd, _ = os.Getwd()
}
if retval.wd == "" {
retval.wd = os.TempDir()
}
return retval
}
func WithDebug(debugExec bool) option {
return func(g *GitTestHelper) {
g.debugExec = debugExec
}
}
func WithWd(wd string) option {
return func(g *GitTestHelper) {
g.wd = wd
}
}
func (s *GitTestHelper) Git() string {
return s.git
}
func (s *GitTestHelper) Err() error {
return s.err
}
func (s *GitTestHelper) ErrCause() string {
return s.errCause
}
func (s *GitTestHelper) ErrString() string {
return fmt.Sprintf("%v\n\n%v", s.errCause, s.err)
}
func (s *GitTestHelper) CommandRunner() shexec.CommandRunner {
return s.runner
}
func (s *GitTestHelper) Mkdir(dir string) bool {
if s.err != nil {
return false
}
target := s.toAbsPath(dir)
if err := os.Mkdir(target, 0755); err != nil {
s.err = err
s.errCause = "mkdir " + target
return false
}
return true
}
func (s *GitTestHelper) Chdir(dir string) bool {
if s.err != nil {
return false
}
target := s.toAbsPath(dir)
s.wd = target
return true
}
func (s *GitTestHelper) Getwd() string {
return s.wd
}
func (s *GitTestHelper) Touch(file string) bool {
if s.err != nil {
return false
}
target := s.toAbsPath(file)
f, err := os.Create(target)
if err != nil {
s.err = err
s.errCause = "touch " + target
return false
}
if f != nil {
if err := f.Close(); err != nil {
return false
}
}
return true
}
func (s *GitTestHelper) SetContents(file, contents string) (ok bool) {
if s.err != nil {
return false
}
target := s.toAbsPath(file)
f, err := os.Create(target)
if err != nil {
s.err = err
s.errCause = fmt.Sprintf("opening %v for write", target)
return false
}
defer func() {
err2 := f.Close()
if err2 != nil {
s.err = err2
s.errCause = fmt.Sprintf("closing %v after write", target)
ok = false
}
}()
_, err = f.WriteString(contents)
if err != nil {
s.err = err
s.errCause = "writing to " + target
return false
}
ok = true
return ok
}
// Exec executes the given command using GetWd() as the working directory.
// For example, `NewGitHelper(WithWd("/tmp")); Exec("ls")` will list the contents of `/tmp`.
func (s *GitTestHelper) Exec(first string, arg ...string) bool {
if s.err != nil {
return false
}
cmd := s.runner.Command(first, arg...).WithDir(s.Getwd())
bytearr, err := cmd.CombinedOutput()
if s.debugExec {
words := append([]string{">>>", first}, arg...)
fmt.Println(strings.Join(words, " "))
fmt.Println(string(bytearr))
}
if err != nil {
s.err = fmt.Errorf("%v %v", err, string(bytearr))
s.errCause = first + " " + strings.Join(arg, " ")
return false
}
return true
}
// RunGit is a convenience method for calling "git ..."
func (s *GitTestHelper) RunGit(args ...string) bool {
return s.Exec(s.Git(), args...)
}
// NewRepoPair creates two repos under the specified directory named source and dest. The source repo is initialized
// as a bare repository. The dest repo is used to add an initial commit and push said commit to source. This method
// changes the helper's working directory to the "dest" directory if possible.
func (s *GitTestHelper) NewRepoPair(basedir string) {
git := s.Git()
target := s.toAbsPath(basedir)
s.Chdir(target)
s.Mkdir("source")
s.Chdir("source")
s.Exec(git, "init", "--bare")
s.Chdir("..")
s.Exec(git, "clone", "source", "dest")
s.Chdir("dest")
s.TouchAndCommit("init.txt", "init")
s.Exec(git, "push", "origin")
}
func (s *GitTestHelper) Add(path string) bool {
return s.Exec(s.git, "add", path)
}
func (s *GitTestHelper) Commit(msg string) bool {
if s.err != nil {
return false
}
git := s.git
return s.Exec(git, "commit", "-m", msg)
}
func (s *GitTestHelper) TouchAndCommit(file, msg string) bool {
return s.Touch(file) &&
s.Add(file) &&
s.Commit(msg)
}
func ResolveGit() string {
git := "git"
if tmp, ok := os.LookupEnv("GRS_TEST_GIT"); ok {
git = tmp
}
return git
}
type Result struct {
delegate *exec.Cmd
Stdout string
}
func (cmd *Result) String() string {
return cmd.delegate.Stdout.(*bytes.Buffer).String()
}
// Given the current working directory and a target path, turn the target path to an absolute path
func (s *GitTestHelper) toAbsPath(name string) string {
if path.IsAbs(name) {
return name
}
wd := s.Getwd()
if wd == "" {
wd, _ = os.Getwd()
if wd == "" {
wd = os.TempDir()
}
}
return path.Join(wd, name)
}