aboutsummaryrefslogtreecommitdiffstats
path: root/commands/root_test.go
diff options
context:
space:
mode:
authorSteve Moyer <smoyer1@selesy.com>2022-05-27 13:34:59 -0400
committerSteve Moyer <smoyer1@selesy.com>2022-05-27 13:37:16 -0400
commit5962ed8453c07ff824a6935883b648cd642c4d2a (patch)
treeb029ebcee41c83062da8d4898cec108c4ec7a077 /commands/root_test.go
parent523a1481857e4e4e13c03a1d4d7630db73568694 (diff)
downloadgit-bug-5962ed8453c07ff824a6935883b648cd642c4d2a.tar.gz
test(778): verify root command returns main help text
Diffstat (limited to 'commands/root_test.go')
-rw-r--r--commands/root_test.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/commands/root_test.go b/commands/root_test.go
new file mode 100644
index 00000000..5e780ec7
--- /dev/null
+++ b/commands/root_test.go
@@ -0,0 +1,82 @@
+package commands_test
+
+import (
+ "bytes"
+ "context"
+ "flag"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/MichaelMure/git-bug/commands"
+ "github.com/MichaelMure/git-bug/repository"
+ "github.com/spf13/cobra"
+ "github.com/stretchr/testify/require"
+)
+
+var update = flag.Bool("update", false, "pass -update to the test runner to update golden files")
+
+type testEnv struct {
+ cwd string
+ repo *repository.GoGitRepo
+ cmd *cobra.Command
+ out *bytes.Buffer
+}
+
+func newTestEnv(t *testing.T) *testEnv {
+ t.Helper()
+
+ cwd, err := ioutil.TempDir("", "")
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ require.NoError(t, os.RemoveAll(cwd))
+ })
+
+ repo, err := repository.InitGoGitRepo(cwd)
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ require.NoError(t, repo.Close())
+ })
+
+ out := new(bytes.Buffer)
+ cmd := commands.NewRootCommand()
+ cmd.SetArgs([]string{})
+ cmd.SetErr(out)
+ cmd.SetOut(out)
+
+ return &testEnv{
+ cwd: cwd,
+ repo: repo,
+ cmd: cmd,
+ out: out,
+ }
+}
+
+func (e *testEnv) Execute(t *testing.T) {
+ t.Helper()
+
+ ctx := context.WithValue(context.Background(), "cwd", e.cwd)
+ require.NoError(t, e.cmd.ExecuteContext(ctx))
+}
+
+func requireGoldenFileEqual(t *testing.T, path string, act []byte) {
+ t.Helper()
+
+ path = filepath.Join("testdata", path)
+
+ if *update {
+ require.NoError(t, ioutil.WriteFile(path, act, 0644))
+ }
+
+ exp, err := ioutil.ReadFile(path)
+ require.NoError(t, err)
+ require.Equal(t, exp, act)
+}
+
+func TestNewRootCommand(t *testing.T) {
+ testEnv := newTestEnv(t)
+ testEnv.Execute(t)
+
+ requireGoldenFileEqual(t, "root_out_golden.txt", testEnv.out.Bytes())
+}