test(778): verify root command returns main help text

Steve Moyer created

Change summary

commands/root_test.go                 | 82 +++++++++++++++++++++++++++++
commands/testdata/root_out_golden.txt | 38 +++++++++++++
2 files changed, 120 insertions(+)

Detailed changes

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())
+}

commands/testdata/root_out_golden.txt 🔗

@@ -0,0 +1,38 @@
+git-bug is a bug tracker embedded in git.
+
+git-bug use git objects to store the bug tracking separated from the files
+history. As bugs are regular git objects, they can be pushed and pulled from/to
+the same git remote you are already using to collaborate with other people.
+
+Usage:
+  git-bug [flags]
+  git-bug [command]
+
+Available Commands:
+  add         Create a new bug.
+  bridge      Configure and use bridges to other bug trackers.
+  commands    Display available commands.
+  comment     Display or add comments to a bug.
+  completion  Generate the autocompletion script for the specified shell
+  deselect    Clear the implicitly selected bug.
+  help        Help about any command
+  label       Display, add or remove labels to/from a bug.
+  ls          List bugs.
+  ls-id       List bug identifiers.
+  ls-label    List valid labels.
+  pull        Pull bugs update from a git remote.
+  push        Push bugs update to a git remote.
+  rm          Remove an existing bug.
+  select      Select a bug for implicit use in future commands.
+  show        Display the details of a bug.
+  status      Display or change a bug status.
+  termui      Launch the terminal UI.
+  title       Display or change a title of a bug.
+  user        Display or change the user identity.
+  version     Show git-bug version information.
+  webui       Launch the web UI.
+
+Flags:
+  -h, --help   help for git-bug
+
+Use "git-bug [command] --help" for more information about a command.