Skip to content

Commit 876ca32

Browse files
author
Nate Smith
authored
Merge pull request cli#852 from vilmibm/spinnertest
totally inelegant approach to hopefully stopping flakey tests
2 parents 28b35aa + 15a6225 commit 876ca32

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

command/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,11 @@ func repoFork(cmd *cobra.Command, args []string) error {
379379
loading := utils.Gray("Forking ") + utils.Bold(utils.Gray(ghrepo.FullName(repoToFork))) + utils.Gray("...")
380380
s.Suffix = " " + loading
381381
s.FinalMSG = utils.Gray(fmt.Sprintf("- %s\n", loading))
382-
s.Start()
382+
utils.StartSpinner(s)
383383

384384
forkedRepo, err := api.ForkRepo(apiClient, repoToFork)
385385
if err != nil {
386-
s.Stop()
386+
utils.StopSpinner(s)
387387
return fmt.Errorf("failed to fork: %w", err)
388388
}
389389

command/repo_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/briandowns/spinner"
15+
1416
"github.com/cli/cli/context"
1517
"github.com/cli/cli/internal/run"
1618
"github.com/cli/cli/test"
19+
"github.com/cli/cli/utils"
1720
)
1821

22+
func stubSpinner() {
23+
// not bothering with teardown since we never want spinners when doing tests
24+
utils.StartSpinner = func(_ *spinner.Spinner) {
25+
}
26+
utils.StopSpinner = func(_ *spinner.Spinner) {
27+
}
28+
}
29+
1930
func TestRepoFork_already_forked(t *testing.T) {
31+
stubSpinner()
2032
initContext = func() context.Context {
2133
ctx := context.NewBlank()
2234
ctx.SetBaseRepo("OWNER/REPO")
@@ -42,6 +54,7 @@ func TestRepoFork_already_forked(t *testing.T) {
4254
}
4355

4456
func TestRepoFork_reuseRemote(t *testing.T) {
57+
stubSpinner()
4558
initContext = func() context.Context {
4659
ctx := context.NewBlank()
4760
ctx.SetBaseRepo("OWNER/REPO")
@@ -77,6 +90,7 @@ func stubSince(d time.Duration) func() {
7790
}
7891

7992
func TestRepoFork_in_parent(t *testing.T) {
93+
stubSpinner()
8094
initBlankContext("", "OWNER/REPO", "master")
8195
defer stubSince(2 * time.Second)()
8296
http := initFakeHTTP()
@@ -98,6 +112,7 @@ func TestRepoFork_in_parent(t *testing.T) {
98112
}
99113

100114
func TestRepoFork_outside(t *testing.T) {
115+
stubSpinner()
101116
tests := []struct {
102117
name string
103118
args string
@@ -134,6 +149,7 @@ func TestRepoFork_outside(t *testing.T) {
134149
}
135150

136151
func TestRepoFork_in_parent_yes(t *testing.T) {
152+
stubSpinner()
137153
initBlankContext("", "OWNER/REPO", "master")
138154
defer stubSince(2 * time.Second)()
139155
http := initFakeHTTP()
@@ -168,6 +184,7 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
168184
}
169185

170186
func TestRepoFork_outside_yes(t *testing.T) {
187+
stubSpinner()
171188
defer stubSince(2 * time.Second)()
172189
http := initFakeHTTP()
173190
defer http.StubWithFixture(200, "forkResult.json")()
@@ -194,6 +211,7 @@ func TestRepoFork_outside_yes(t *testing.T) {
194211
}
195212

196213
func TestRepoFork_outside_survey_yes(t *testing.T) {
214+
stubSpinner()
197215
defer stubSince(2 * time.Second)()
198216
http := initFakeHTTP()
199217
defer http.StubWithFixture(200, "forkResult.json")()
@@ -227,6 +245,7 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
227245
}
228246

229247
func TestRepoFork_outside_survey_no(t *testing.T) {
248+
stubSpinner()
230249
defer stubSince(2 * time.Second)()
231250
http := initFakeHTTP()
232251
defer http.StubWithFixture(200, "forkResult.json")()
@@ -261,6 +280,7 @@ func TestRepoFork_outside_survey_no(t *testing.T) {
261280
}
262281

263282
func TestRepoFork_in_parent_survey_yes(t *testing.T) {
283+
stubSpinner()
264284
initBlankContext("", "OWNER/REPO", "master")
265285
defer stubSince(2 * time.Second)()
266286
http := initFakeHTTP()
@@ -303,6 +323,7 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
303323
}
304324

305325
func TestRepoFork_in_parent_survey_no(t *testing.T) {
326+
stubSpinner()
306327
initBlankContext("", "OWNER/REPO", "master")
307328
defer stubSince(2 * time.Second)()
308329
http := initFakeHTTP()

utils/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ func Humanize(s string) string {
7474
return strings.Map(h, s)
7575
}
7676

77+
// We do this so we can stub out the spinner in tests -- it made things really flakey. this is not
78+
// an elegant solution.
79+
var StartSpinner = func(s *spinner.Spinner) {
80+
s.Start()
81+
}
82+
83+
var StopSpinner = func(s *spinner.Spinner) {
84+
s.Stop()
85+
}
86+
7787
func Spinner(w io.Writer) *spinner.Spinner {
7888
return spinner.New(spinner.CharSets[11], 400*time.Millisecond, spinner.WithWriter(w))
7989
}

0 commit comments

Comments
 (0)