Skip to content

Commit f055343

Browse files
committed
internal/telemetry/cmd/stacks: divert GitHub changes during testing
When testing, the GitHub client saves changes instead of applying them. This makes it possible to test functions that would otherwise alter GitHub. Add a test for one such function, updateIssues. Change-Id: I77a716dac346ab591ff1f94cd38e414d0082d513 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643937 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 3e68f53 commit f055343

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

gopls/internal/telemetry/cmd/stacks/stacks.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,24 @@ func frameURL(pclntab map[string]FileLine, info Info, frame string) string {
811811
// -- GitHub client --
812812

813813
// A githubClient interacts with GitHub.
814+
// During testing, updates to GitHub are saved in changes instead of being applied.
815+
// Reads from GitHub occur normally.
814816
type githubClient struct {
815-
authToken string // mandatory GitHub authentication token (for R/W issues access)
817+
authToken string // mandatory GitHub authentication token (for R/W issues access)
818+
divertChanges bool // divert attempted GitHub changes to the changes field instead of executing them
819+
changes []any // slice of (addIssueComment | updateIssueBody)
820+
}
821+
822+
// addIssueComment is a change for creating a comment on an issue.
823+
type addIssueComment struct {
824+
number int
825+
comment string
826+
}
827+
828+
// updateIssueBody is a change for modifying an existing issue's body.
829+
type updateIssueBody struct {
830+
number int
831+
body string
816832
}
817833

818834
// -- GitHub search --
@@ -874,6 +890,11 @@ func (cli *githubClient) searchIssues(label string) ([]*Issue, error) {
874890

875891
// updateIssueBody updates the body of the numbered issue.
876892
func (cli *githubClient) updateIssueBody(number int, body string) error {
893+
if cli.divertChanges {
894+
cli.changes = append(cli.changes, updateIssueBody{number, body})
895+
return nil
896+
}
897+
877898
// https://docs.github.com/en/rest/issues/comments#update-an-issue
878899
var payload struct {
879900
Body string `json:"body"`
@@ -893,6 +914,11 @@ func (cli *githubClient) updateIssueBody(number int, body string) error {
893914

894915
// addIssueComment adds a markdown comment to the numbered issue.
895916
func (cli *githubClient) addIssueComment(number int, comment string) error {
917+
if cli.divertChanges {
918+
cli.changes = append(cli.changes, addIssueComment{number, comment})
919+
return nil
920+
}
921+
896922
// https://docs.github.com/en/rest/issues/comments#create-an-issue-comment
897923
var payload struct {
898924
Body string `json:"body"`

gopls/internal/telemetry/cmd/stacks/stacks_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package main
88

99
import (
10+
"strings"
1011
"testing"
1112
)
1213

@@ -128,8 +129,54 @@ func TestParsePredicateError(t *testing.T) {
128129
} {
129130
if _, err := parsePredicate(expr); err == nil {
130131
t.Errorf("%s: got nil, want error", expr)
131-
} else {
132-
t.Logf("%s: %v", expr, err)
133132
}
134133
}
135134
}
135+
136+
// which takes the bulk of the time.
137+
func TestUpdateIssues(t *testing.T) {
138+
if testing.Short() {
139+
t.Skip("downloads source from the internet, skipping in -short")
140+
}
141+
c := &githubClient{divertChanges: true}
142+
issues := []*Issue{{Number: 1, newStacks: []string{"stack1"}}}
143+
info := Info{
144+
Program: "golang.org/x/tools/gopls",
145+
ProgramVersion: "v0.16.1",
146+
}
147+
const stack1 = "stack1"
148+
id1 := stackID(stack1)
149+
stacks := map[string]map[Info]int64{stack1: map[Info]int64{info: 3}}
150+
stacksToURL := map[string]string{stack1: "URL1"}
151+
updateIssues(c, issues, stacks, stacksToURL)
152+
153+
if g, w := len(c.changes), 2; g != w {
154+
t.Fatalf("got %d changes, want %d", g, w)
155+
}
156+
// The first change creates an issue comment.
157+
cic, ok := c.changes[0].(addIssueComment)
158+
if !ok {
159+
t.Fatalf("got %T, want addIssueComment", c.changes[0])
160+
}
161+
if cic.number != 1 {
162+
t.Errorf("issue number: got %d, want 1", cic.number)
163+
}
164+
for _, want := range []string{"URL1", stack1, id1, "golang.org/x/tools/[email protected]"} {
165+
if !strings.Contains(cic.comment, want) {
166+
t.Errorf("missing %q in comment:\n%s", want, cic.comment)
167+
}
168+
}
169+
170+
// The second change updates the issue body.
171+
ui, ok := c.changes[1].(updateIssueBody)
172+
if !ok {
173+
t.Fatalf("got %T, want updateIssueBody", c.changes[1])
174+
}
175+
if ui.number != 1 {
176+
t.Errorf("issue number: got %d, want 1", cic.number)
177+
}
178+
want := "Dups: " + id1
179+
if !strings.Contains(ui.body, want) {
180+
t.Errorf("missing %q in body %q", want, ui.body)
181+
}
182+
}

0 commit comments

Comments
 (0)