Skip to content

Commit b53bc7a

Browse files
committed
mock gh api client for tests
1 parent b99f725 commit b53bc7a

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

cmd/push_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/github/gh-stack/internal/config"
99
"github.com/github/gh-stack/internal/git"
10+
"github.com/github/gh-stack/internal/github"
1011
"github.com/github/gh-stack/internal/stack"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
@@ -82,6 +83,7 @@ func TestPush_SkipPRs(t *testing.T) {
8283
defer restore()
8384

8485
cfg, _, errR := config.NewTestConfig()
86+
cfg.GitHubClientOverride = &github.MockClient{}
8587
cmd := PushCmd(cfg)
8688
cmd.SetArgs([]string{"--skip-prs"})
8789
cmd.SetOut(io.Discard)
@@ -124,6 +126,7 @@ func TestPush_SkipsMergedBranches(t *testing.T) {
124126
defer restore()
125127

126128
cfg, _, errR := config.NewTestConfig()
129+
cfg.GitHubClientOverride = &github.MockClient{}
127130
cmd := PushCmd(cfg)
128131
cmd.SetArgs([]string{"--skip-prs"})
129132
cmd.SetOut(io.Discard)
@@ -158,6 +161,7 @@ func TestPush_PushFailure(t *testing.T) {
158161
defer restore()
159162

160163
cfg, _, errR := config.NewTestConfig()
164+
cfg.GitHubClientOverride = &github.MockClient{}
161165
cmd := PushCmd(cfg)
162166
cmd.SetArgs([]string{"--skip-prs"})
163167
cmd.SetOut(io.Discard)

cmd/unstack_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/github/gh-stack/internal/config"
1010
"github.com/github/gh-stack/internal/git"
11+
"github.com/github/gh-stack/internal/github"
1112
"github.com/github/gh-stack/internal/stack"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -43,6 +44,7 @@ func TestUnstack_RemovesStack(t *testing.T) {
4344
writeTwoStacks(t, gitDir, s1, s2)
4445

4546
cfg, outR, errR := config.NewTestConfig()
47+
cfg.GitHubClientOverride = &github.MockClient{}
4648
err := runUnstack(cfg, &unstackOptions{})
4749
output := collectOutput(cfg, outR, errR)
4850

internal/config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type Config struct {
2626
ColorMagenta func(string) string
2727
ColorCyan func(string) string
2828
ColorGray func(string) string
29+
30+
// GitHubClientOverride, when non-nil, is returned by GitHubClient()
31+
// instead of creating a real client. Used in tests to inject a MockClient.
32+
GitHubClientOverride ghapi.ClientOps
2933
}
3034

3135
// New creates a new Config with terminal-aware output and color support.
@@ -109,7 +113,10 @@ func (c *Config) Repo() (repository.Repository, error) {
109113
return repository.Current()
110114
}
111115

112-
func (c *Config) GitHubClient() (*ghapi.Client, error) {
116+
func (c *Config) GitHubClient() (ghapi.ClientOps, error) {
117+
if c.GitHubClientOverride != nil {
118+
return c.GitHubClientOverride, nil
119+
}
113120
repo, err := c.Repo()
114121
if err != nil {
115122
return nil, fmt.Errorf("determining repository: %w", err)

0 commit comments

Comments
 (0)