1package execenv
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11
12 "github.com/MichaelMure/git-bug/cache"
13 "github.com/MichaelMure/git-bug/repository"
14)
15
16var _ Out = &TestOut{}
17
18type TestOut struct {
19 *bytes.Buffer
20}
21
22func (te *TestOut) Printf(format string, a ...interface{}) {
23 _, _ = fmt.Fprintf(te.Buffer, format, a...)
24}
25
26func (te *TestOut) Print(a ...interface{}) {
27 _, _ = fmt.Fprint(te.Buffer, a...)
28}
29
30func (te *TestOut) Println(a ...interface{}) {
31 _, _ = fmt.Fprintln(te.Buffer, a...)
32}
33
34func (te *TestOut) PrintJSON(v interface{}) error {
35 raw, err := json.MarshalIndent(v, "", " ")
36 if err != nil {
37 return err
38 }
39 te.Println(string(raw))
40 return nil
41}
42
43func (te *TestOut) Raw() io.Writer {
44 return te.Buffer
45}
46
47func NewTestEnv(t *testing.T) *Env {
48 t.Helper()
49
50 repo := repository.CreateGoGitTestRepo(t, false)
51
52 buf := new(bytes.Buffer)
53
54 backend, err := cache.NewRepoCacheNoEvents(repo)
55 require.NoError(t, err)
56
57 t.Cleanup(func() {
58 backend.Close()
59 })
60
61 return &Env{
62 Repo: repo,
63 Backend: backend,
64 Out: &TestOut{buf},
65 Err: &TestOut{buf},
66 }
67}