Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Github Mock Handler #118

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/prstatus/prstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package prstatus
import (
"bytes"
"errors"
"io"
"os"
"testing"

Expand Down Expand Up @@ -160,7 +159,7 @@ func prepareFakeResponses() {
},
},
}
fakeGitHub := github.NewFakeGitHub(nil, func(output io.Writer, workingDir string) (interface{}, error) {
fakeGitHub := github.NewFakeGitHub(nil, func(workingDir string) (interface{}, error) {
if workingDir == "work/org/repoWithError" {
return nil, errors.New("Synthetic error")
} else {
Expand Down
68 changes: 36 additions & 32 deletions internal/github/fake_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,59 @@ import (
)

type FakeGitHub struct {
handler func(output io.Writer, command Command, workingDir string, fullRepoName string) (bool, error)
returningHandler func(output io.Writer, workingDir string) (interface{}, error)
handler func(command Command, args []string) (bool, error)
returningHandler func(workingDir string) (interface{}, error)
calls [][]string
}

func (f *FakeGitHub) CreatePullRequest(output io.Writer, workingDir string, metadata PullRequest) (didCreate bool, err error) {
f.calls = append(f.calls, []string{workingDir, metadata.Title})
return f.handler(output, CreatePullRequest, workingDir, "")
func (f *FakeGitHub) CreatePullRequest(_ io.Writer, workingDir string, metadata PullRequest) (didCreate bool, err error) {
args := []string{workingDir, metadata.Title}
f.calls = append(f.calls, args)
return f.handler(CreatePullRequest, args)
}

func (f *FakeGitHub) ForkAndClone(output io.Writer, workingDir string, fullRepoName string) error {
f.calls = append(f.calls, []string{workingDir, fullRepoName})
_, err := f.handler(output, ForkAndClone, workingDir, fullRepoName)
func (f *FakeGitHub) ForkAndClone(_ io.Writer, workingDir string, fullRepoName string) error {
args := []string{workingDir, fullRepoName}
f.calls = append(f.calls, args)
_, err := f.handler(ForkAndClone, args)
return err
}

func (f *FakeGitHub) Clone(output io.Writer, workingDir string, fullRepoName string) error {
f.calls = append(f.calls, []string{workingDir, fullRepoName})
_, err := f.handler(output, Clone, workingDir, fullRepoName)
func (f *FakeGitHub) Clone(_ io.Writer, workingDir string, fullRepoName string) error {
args := []string{workingDir, fullRepoName}
f.calls = append(f.calls, args)
_, err := f.handler(Clone, args)
return err
}

func (f *FakeGitHub) ClosePullRequest(output io.Writer, workingDir string, branchName string) error {
// TODO: handle this differently; branchName here is replacing fullRepoName
// This is OK for now because fullRepoName is used nowhere in the github mocks
f.calls = append(f.calls, []string{workingDir, branchName})
_, err := f.handler(output, ClosePullRequest, workingDir, branchName)
func (f *FakeGitHub) ClosePullRequest(_ io.Writer, workingDir string, branchName string) error {
args := []string{workingDir, branchName}
f.calls = append(f.calls, args)
_, err := f.handler(ClosePullRequest, args)
return err
}

func (f *FakeGitHub) GetPR(output io.Writer, workingDir string, _ string) (*PrStatus, error) {
func (f *FakeGitHub) GetPR(_ io.Writer, workingDir string, _ string) (*PrStatus, error) {
f.calls = append(f.calls, []string{workingDir})
result, err := f.returningHandler(output, workingDir)
result, err := f.returningHandler(workingDir)
if result == nil {
return nil, err
}
return result.(*PrStatus), err
}

func (f *FakeGitHub) GetDefaultBranchName(output io.Writer, workingDir string, fullRepoName string) (string, error) {
f.calls = append(f.calls, []string{workingDir, fullRepoName})
_, err := f.handler(output, GetDefaultBranchName, workingDir, fullRepoName)
func (f *FakeGitHub) GetDefaultBranchName(_ io.Writer, workingDir string, fullRepoName string) (string, error) {
args := []string{workingDir, fullRepoName}
f.calls = append(f.calls, args)
_, err := f.handler(GetDefaultBranchName, args)
return "main", err
}

func (f *FakeGitHub) AssertCalledWith(t *testing.T, expected [][]string) {
assert.Equal(t, expected, f.calls)
}

func NewFakeGitHub(h func(output io.Writer, command Command, workingDir string, fullRepoName string) (bool, error), r func(output io.Writer, workingDir string) (interface{}, error)) *FakeGitHub {
func NewFakeGitHub(h func(command Command, args []string) (bool, error), r func(workingDir string) (interface{}, error)) *FakeGitHub {
return &FakeGitHub{
handler: h,
returningHandler: r,
Expand All @@ -82,44 +85,45 @@ func NewFakeGitHub(h func(output io.Writer, command Command, workingDir string,
}

func NewAlwaysSucceedsFakeGitHub() *FakeGitHub {
return NewFakeGitHub(func(output io.Writer, command Command, workingDir string, fullRepoName string) (bool, error) {
return NewFakeGitHub(func(command Command, args []string) (bool, error) {
return true, nil
}, func(output io.Writer, workingDir string) (interface{}, error) {
}, func(workingDir string) (interface{}, error) {
return PrStatus{}, nil
})
}

func NewAlwaysFailsFakeGitHub() *FakeGitHub {
return NewFakeGitHub(func(output io.Writer, command Command, workingDir string, fullRepoName string) (bool, error) {
return NewFakeGitHub(func(command Command, args []string) (bool, error) {
return false, errors.New("synthetic error")
}, func(output io.Writer, workingDir string) (interface{}, error) {
}, func(workingDir string) (interface{}, error) {
return nil, errors.New("synthetic error")
})
}

func NewAlwaysThrowNoPRFound() *FakeGitHub {
return NewFakeGitHub(func(output io.Writer, command Command, workingDir string, branchName string) (bool, error) {
return NewFakeGitHub(func(command Command, args []string) (bool, error) {
workingDir, branchName := args[0], args[1]
Dan7-7-7 marked this conversation as resolved.
Show resolved Hide resolved
return false, &NoPRFoundError{Path: workingDir, BranchName: branchName}
}, func(output io.Writer, workingDir string) (interface{}, error) {
}, func(workingDir string) (interface{}, error) {
panic("should not be invoked")
})
}

func NewAlwaysReturnsFalseFakeGitHub() *FakeGitHub {
return NewFakeGitHub(func(output io.Writer, command Command, workingDir string, fullRepoName string) (bool, error) {
return NewFakeGitHub(func(command Command, args []string) (bool, error) {
return false, nil
}, func(output io.Writer, workingDir string) (interface{}, error) {
}, func(workingDir string) (interface{}, error) {
return PrStatus{}, nil
})
}

func NewAlwaysFailsOnGetDefaultBranchFakeGitHub() *FakeGitHub {
return NewFakeGitHub(func(output io.Writer, command Command, workingDir string, fullRepoName string) (bool, error) {
return NewFakeGitHub(func(command Command, args []string) (bool, error) {
if command == GetDefaultBranchName {
return false, errors.New("synthetic error")
}
return true, nil
}, func(output io.Writer, workingDir string) (interface{}, error) {
}, func(workingDir string) (interface{}, error) {
return PrStatus{}, nil
})
}
Expand Down
Loading