Skip to content

Commit 3b596b1

Browse files
committed
Add config mock for tests
1 parent 9d0421c commit 3b596b1

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

cmd/create_test.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"reflect"
55
"testing"
66

7+
"github.com/cli/go-gh/pkg/config"
78
"github.com/cli/go-gh/pkg/repository"
89
"github.com/stretchr/testify/assert"
910
"gopkg.in/h2non/gock.v1"
@@ -22,7 +23,7 @@ func Test_createHook(t *testing.T) {
2223
repo: MockRepo{
2324
host: "github.com",
2425
name: "test-repo",
25-
owner: "lucasmelin",
26+
owner: "user1",
2627
},
2728
data: Hook{
2829
Id: 12345678,
@@ -37,7 +38,7 @@ func Test_createHook(t *testing.T) {
3738
},
3839
httpMocks: func() {
3940
gock.New("https://api.github.com").
40-
Post("repos/lucasmelin/test-repo/hooks").
41+
Post("repos/user1/test-repo/hooks").
4142
BodyString(`{
4243
"id":12345678,
4344
"name":"web",
@@ -69,10 +70,10 @@ func Test_createHook(t *testing.T) {
6970
},
7071
"updated_at": "2019-06-03T00:57:16Z",
7172
"created_at": "2019-06-03T00:57:16Z",
72-
"url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678",
73-
"test_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/test",
74-
"ping_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/pings",
75-
"deliveries_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/deliveries",
73+
"url": "https://api.github.com/repos/user1/test-repo/hooks/12345678",
74+
"test_url": "https://api.github.com/repos/user1/test-repo/hooks/12345678/test",
75+
"ping_url": "https://api.github.com/repos/user1/test-repo/hooks/12345678/pings",
76+
"deliveries_url": "https://api.github.com/repos/user1/test-repo/hooks/12345678/deliveries",
7677
"last_response": {
7778
"code": null,
7879
"status": "unused",
@@ -84,6 +85,7 @@ func Test_createHook(t *testing.T) {
8485
}
8586
for _, tt := range tests {
8687
t.Run(tt.name, func(t *testing.T) {
88+
stubConfig(t, testConfig())
8789
t.Cleanup(gock.Off)
8890
if tt.httpMocks != nil {
8991
tt.httpMocks()
@@ -152,6 +154,7 @@ func Test_getEvents(t *testing.T) {
152154
}
153155
for _, tt := range tests {
154156
t.Run(tt.name, func(t *testing.T) {
157+
stubConfig(t, testConfig())
155158
t.Cleanup(gock.Off)
156159
if tt.httpMocks != nil {
157160
tt.httpMocks()
@@ -167,3 +170,23 @@ func Test_getEvents(t *testing.T) {
167170
})
168171
}
169172
}
173+
174+
func stubConfig(t *testing.T, cfgStr string) {
175+
t.Helper()
176+
old := config.Read
177+
config.Read = func() (*config.Config, error) {
178+
return config.ReadFromString(cfgStr), nil
179+
}
180+
t.Cleanup(func() {
181+
config.Read = old
182+
})
183+
}
184+
185+
func testConfig() string {
186+
return `
187+
hosts:
188+
github.com:
189+
user: user1
190+
oauth_token: abc123
191+
`
192+
}

cmd/delete_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func Test_deleteHooks(t *testing.T) {
3333
}
3434
for _, tt := range tests {
3535
t.Run(tt.name, func(t *testing.T) {
36+
stubConfig(t, testConfig())
3637
t.Cleanup(gock.Off)
3738
if tt.httpMocks != nil {
3839
tt.httpMocks()

cmd/list_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func Test_getWebhooks(t *testing.T) {
126126
}
127127
for _, tt := range tests {
128128
t.Run(tt.name, func(t *testing.T) {
129+
stubConfig(t, testConfig())
129130
t.Cleanup(gock.Off)
130131
if tt.httpMocks != nil {
131132
tt.httpMocks()

0 commit comments

Comments
 (0)