bug_test.go

  1package bugcmd
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"testing"
  7
  8	"github.com/stretchr/testify/require"
  9
 10	"github.com/MichaelMure/git-bug/commands/bug/testenv"
 11)
 12
 13func Test_repairQuery(t *testing.T) {
 14	cases := []struct {
 15		args   []string
 16		output string
 17	}{
 18		{
 19			[]string{""},
 20			"",
 21		},
 22		{
 23			[]string{"foo"},
 24			"foo",
 25		},
 26		{
 27			[]string{"foo", "bar"},
 28			"foo bar",
 29		},
 30		{
 31			[]string{"foo bar", "baz"},
 32			"\"foo bar\" baz",
 33		},
 34		{
 35			[]string{"foo:bar", "baz"},
 36			"foo:bar baz",
 37		},
 38		{
 39			[]string{"foo:bar boo", "baz"},
 40			"foo:\"bar boo\" baz",
 41		},
 42	}
 43
 44	for _, tc := range cases {
 45		require.Equal(t, tc.output, repairQuery(tc.args))
 46	}
 47}
 48
 49func TestBug_Format(t *testing.T) {
 50	const expOrgMode = `^#+TODO: OPEN | CLOSED
 51[*] OPEN   [0-9a-f]{7} \[\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d\] John Doe: this is a bug title ::
 52[*]{2} Last Edited: \[\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d\]
 53[*]{2} Actors:
 54: [0-9a-f]{7} John Doe
 55[*]{2} Participants:
 56: [0-9a-f]{7} John Doe
 57$`
 58
 59	cases := []struct {
 60		format string
 61		exp    string
 62	}{
 63		{"default", "^[0-9a-f]{7}\topen\tthis is a bug title                               \tJohn Doe       \t\n$"},
 64		{"plain", "^[0-9a-f]{7} \\[open\\] this is a bug title\n$"},
 65		{"compact", "^[0-9a-f]{7} open this is a bug title                            John Doe\n$"},
 66		{"id", "^[0-9a-f]{64}\n$"},
 67		{"org-mode", expOrgMode},
 68	}
 69
 70	for _, testcase := range cases {
 71		opts := bugOptions{
 72			sortDirection: "asc",
 73			sortBy:        "creation",
 74			outputFormat:  testcase.format,
 75		}
 76
 77		name := fmt.Sprintf("with %s format", testcase.format)
 78
 79		t.Run(name, func(t *testing.T) {
 80			env, _ := testenv.NewTestEnvAndBug(t)
 81
 82			require.NoError(t, runBug(env, opts, []string{}))
 83			require.Regexp(t, testcase.exp, env.Out.String())
 84		})
 85	}
 86
 87	t.Run("with JSON format", func(t *testing.T) {
 88		opts := bugOptions{
 89			sortDirection: "asc",
 90			sortBy:        "creation",
 91			outputFormat:  "json",
 92		}
 93
 94		env, _ := testenv.NewTestEnvAndBug(t)
 95
 96		require.NoError(t, runBug(env, opts, []string{}))
 97
 98		var bugs []JSONBugExcerpt
 99		require.NoError(t, json.Unmarshal(env.Out.Bytes(), &bugs))
100
101		require.Len(t, bugs, 1)
102	})
103}