Skip to content

Commit

Permalink
Add config mock for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmelin committed Jan 29, 2023
1 parent 9d0421c commit 3b596b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
35 changes: 29 additions & 6 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/cli/go-gh/pkg/config"
"github.com/cli/go-gh/pkg/repository"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
Expand All @@ -22,7 +23,7 @@ func Test_createHook(t *testing.T) {
repo: MockRepo{
host: "github.com",
name: "test-repo",
owner: "lucasmelin",
owner: "user1",
},
data: Hook{
Id: 12345678,
Expand All @@ -37,7 +38,7 @@ func Test_createHook(t *testing.T) {
},
httpMocks: func() {
gock.New("https://api.github.com").
Post("repos/lucasmelin/test-repo/hooks").
Post("repos/user1/test-repo/hooks").
BodyString(`{
"id":12345678,
"name":"web",
Expand Down Expand Up @@ -69,10 +70,10 @@ func Test_createHook(t *testing.T) {
},
"updated_at": "2019-06-03T00:57:16Z",
"created_at": "2019-06-03T00:57:16Z",
"url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678",
"test_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/test",
"ping_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/pings",
"deliveries_url": "https://api.github.com/repos/octocat/Hello-World/hooks/12345678/deliveries",
"url": "https://api.github.com/repos/user1/test-repo/hooks/12345678",
"test_url": "https://api.github.com/repos/user1/test-repo/hooks/12345678/test",
"ping_url": "https://api.github.com/repos/user1/test-repo/hooks/12345678/pings",
"deliveries_url": "https://api.github.com/repos/user1/test-repo/hooks/12345678/deliveries",
"last_response": {
"code": null,
"status": "unused",
Expand All @@ -84,6 +85,7 @@ func Test_createHook(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
if tt.httpMocks != nil {
tt.httpMocks()
Expand Down Expand Up @@ -152,6 +154,7 @@ func Test_getEvents(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
if tt.httpMocks != nil {
tt.httpMocks()
Expand All @@ -167,3 +170,23 @@ func Test_getEvents(t *testing.T) {
})
}
}

func stubConfig(t *testing.T, cfgStr string) {
t.Helper()
old := config.Read
config.Read = func() (*config.Config, error) {
return config.ReadFromString(cfgStr), nil
}
t.Cleanup(func() {
config.Read = old
})
}

func testConfig() string {
return `
hosts:
github.com:
user: user1
oauth_token: abc123
`
}
1 change: 1 addition & 0 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Test_deleteHooks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
if tt.httpMocks != nil {
tt.httpMocks()
Expand Down
1 change: 1 addition & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func Test_getWebhooks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stubConfig(t, testConfig())
t.Cleanup(gock.Off)
if tt.httpMocks != nil {
tt.httpMocks()
Expand Down

0 comments on commit 3b596b1

Please sign in to comment.