aboutsummaryrefslogtreecommitdiffstats
path: root/commands/ls_test.go
blob: 46728847ee9f7c4affaf54365917836ff648fb8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package commands

import (
	"encoding/json"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
)

func Test_repairQuery(t *testing.T) {
	cases := []struct {
		args   []string
		output string
	}{
		{
			[]string{""},
			"",
		},
		{
			[]string{"foo"},
			"foo",
		},
		{
			[]string{"foo", "bar"},
			"foo bar",
		},
		{
			[]string{"foo bar", "baz"},
			"\"foo bar\" baz",
		},
		{
			[]string{"foo:bar", "baz"},
			"foo:bar baz",
		},
		{
			[]string{"foo:bar boo", "baz"},
			"foo:\"bar boo\" baz",
		},
	}

	for _, tc := range cases {
		require.Equal(t, tc.output, repairQuery(tc.args))
	}
}

func TestLs_Format(t *testing.T) {
	const expOrgMode = `^#+TODO: OPEN | CLOSED
[*] 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 ::
[*]{2} Last Edited: \[\d\d\d\d-\d\d-\d\d [[:alpha:]]{3} \d\d:\d\d\]
[*]{2} Actors:
: [0-9a-f]{7} John Doe
[*]{2} Participants:
: [0-9a-f]{7} John Doe
$`

	cases := []struct {
		format string
		exp    string
	}{
		{"default", "^[0-9a-f]{7}\topen\tthis is a bug title                               \tJohn Doe       \t\n$"},
		{"plain", "^[0-9a-f]{7} \\[open\\] this is a bug title\n$"},
		{"compact", "^[0-9a-f]{7} open this is a bug title                            John Doe\n$"},
		{"org-mode", expOrgMode},
	}

	for _, testcase := range cases {
		opts := lsOptions{
			sortDirection: "asc",
			sortBy:        "creation",
			outputFormat:  testcase.format,
		}

		name := fmt.Sprintf("with %s format", testcase.format)

		t.Run(name, func(t *testing.T) {
			env, _, _ := newTestEnvAndBug(t)

			require.NoError(t, runLs(env.env, opts, []string{}))
			t.Log(env.out.String())
			require.Regexp(t, testcase.exp, env.out.String())
			require.Equal(t, "", env.err.String())
		})
	}

	t.Run("with JSON format", func(t *testing.T) {
		opts := lsOptions{
			sortDirection: "asc",
			sortBy:        "creation",
			outputFormat:  "json",
		}

		env, _, _ := newTestEnvAndBug(t)

		require.NoError(t, runLs(env.env, opts, []string{}))

		bugs := []JSONBugExcerpt{}
		require.NoError(t, json.Unmarshal(env.out.Bytes(), &bugs))
		require.Empty(t, env.err.Bytes())

		require.Len(t, bugs, 1)
	})
}