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