1package commands_test
2
3import (
4 "bytes"
5 "context"
6 "flag"
7 "io/ioutil"
8 "os"
9 "testing"
10
11 "github.com/MichaelMure/git-bug/commands"
12 "github.com/MichaelMure/git-bug/repository"
13 "github.com/spf13/cobra"
14 "github.com/stretchr/testify/require"
15)
16
17var update = flag.Bool("update", false, "pass -update to the test runner to update golden files")
18
19type testEnv struct {
20 cwd string
21 repo *repository.GoGitRepo
22 cmd *cobra.Command
23 out *bytes.Buffer
24}
25
26func newTestEnv(t *testing.T) *testEnv {
27 t.Helper()
28
29 cwd, err := ioutil.TempDir("", "")
30 require.NoError(t, err)
31 t.Cleanup(func() {
32 require.NoError(t, os.RemoveAll(cwd))
33 })
34
35 repo, err := repository.InitGoGitRepo(cwd, commands.GitBugNamespace)
36 require.NoError(t, err)
37 t.Cleanup(func() {
38 require.NoError(t, repo.Close())
39 })
40
41 out := new(bytes.Buffer)
42 cmd := commands.NewRootCommand()
43 cmd.SetArgs([]string{})
44 cmd.SetErr(out)
45 cmd.SetOut(out)
46
47 return &testEnv{
48 cwd: cwd,
49 repo: repo,
50 cmd: cmd,
51 out: out,
52 }
53}
54
55func (e *testEnv) Execute(t *testing.T) {
56 t.Helper()
57
58 ctx := context.WithValue(context.Background(), "cwd", e.cwd)
59 require.NoError(t, e.cmd.ExecuteContext(ctx))
60}