-
Notifications
You must be signed in to change notification settings - Fork 2
/
matcher_test.go
146 lines (133 loc) · 3.99 KB
/
matcher_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"strings"
"testing"
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/dnaeon/go-vcr.v3/cassette"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
)
type matcherTest struct {
name string
input string
expected Match
}
func TestMatchIssues(t *testing.T) {
for _, test := range []matcherTest{
{
name: "simple reference",
input: "owner/repo#123",
expected: Issue{Owner: "owner", Repo: "repo", Num: "123"},
},
{
name: "reference inside markdown link",
input: "[owner/repo#123: title](https://github.com/owner/repo/issues/123)",
expected: Issue{Owner: "owner", Repo: "repo", Num: "123"},
},
{
name: "no reference",
input: "whatever / other thing # 123",
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
issue, matched := match(test.input)
if test.expected == nil {
assert.False(t, matched)
} else {
assert.True(t, matched, "should have matched")
assert.Equal(t, test.expected, issue)
}
})
}
}
func TestMatchURLs(t *testing.T) {
for _, test := range []matcherTest{
{
name: "issue url",
input: "https://github.com/owner/repo/issues/123",
expected: Issue{Owner: "owner", Repo: "repo", Num: "123"},
},
{
name: "issue url inside markdown",
input: "[owner/repo#123: title](https://github.com/owner/repo/issues/123)",
expected: Issue{Owner: "owner", Repo: "repo", Num: "123"},
},
{
name: "pull url",
input: "https://github.com/owner/repo/pull/123",
expected: Pull{Owner: "owner", Repo: "repo", Num: "123"},
},
{
name: "pull url inside markdown",
input: "[owner/repo#123: title](https://github.com/owner/repo/pull/123)",
expected: Pull{Owner: "owner", Repo: "repo", Num: "123"},
},
{
name: "repo discussion url",
input: "https://github.com/owner/repo/discussions/123",
expected: Discussion{Owner: "owner", Repo: "repo", Num: "123"},
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
reference, matched := match(test.input)
if (test.expected == Issue{}) {
assert.False(t, matched, "should not have matched")
} else {
assert.True(t, matched, "should have matched")
assert.Equal(t, test.expected, reference)
}
})
}
}
func TestMatchURLPrecedence(t *testing.T) {
ref, matched := match("[owner/repo#123: title](https://github.com/another/repo/issues/456)")
assert.True(t, matched)
assert.Equal(t, Issue{Owner: "another", Repo: "repo", Num: "456"}, ref)
}
func TestIssueTitle(t *testing.T) {
client := testClient(t, "fixtures/issue_title")
issue := Issue{Owner: "github", Repo: "scientist", Num: "174"}
title, err := issue.Title(client)
require.NoError(t, err)
assert.Equal(t, "Allow setting raise_on_mismatches to base class level for tests", title)
}
func TestPullRequestTitle(t *testing.T) {
client := testClient(t, "fixtures/pull_request_title")
pull := Pull{Owner: "github", Repo: "scientist", Num: "175"}
title, err := pull.Title(client)
require.NoError(t, err)
assert.Equal(t, "Add Ruby 3.1 to CI", title)
}
func TestDiscussionTitle(t *testing.T) {
client := testClient(t, "fixtures/discussion_title")
discussion := Discussion{Owner: "cli", Repo: "cli", Num: "2673"}
title, err := discussion.Title(client)
require.NoError(t, err)
assert.Equal(t, "Upgrade command", title)
}
func testClient(t *testing.T, fixturePath string) api.GQLClient {
r, err := recorder.New(fixturePath)
require.NoError(t, err)
t.Cleanup(func() { _ = r.Stop() })
require.Equal(t, recorder.ModeRecordOnce, r.Mode())
hook := func(i *cassette.Interaction) error {
delete(i.Request.Headers, "Authorization")
for h := range i.Response.Headers {
if strings.HasPrefix(h, "X-") {
delete(i.Response.Headers, h)
}
}
return nil
}
r.AddHook(hook, recorder.AfterCaptureHook)
client, err := gh.GQLClient(&api.ClientOptions{
EnableCache: false,
Transport: r.GetDefaultClient().Transport,
})
require.NoError(t, err)
return client
}