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

Use fakes instead of mocks for use case tests #86

Merged
merged 42 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
95dd81e
chore: initial commit
hielfx May 9, 2024
92eb6a4
refactor: add some fake providers to use in test cases
hielfx May 9, 2024
9e4cd17
test: use new fake git provider in github tests
hielfx May 9, 2024
41a9606
test: use new fake repository provider in github tests
hielfx May 10, 2024
f30c462
test: use new fake pull request provider in github tests
hielfx May 10, 2024
54d3404
test: add new case for thje fake pull request provider
hielfx May 10, 2024
7a73496
test: add new fake repository provider in jira tests
hielfx May 10, 2024
d692696
test: add new fake git and pull request providers in jira tests
hielfx May 10, 2024
d30bb62
refactor: remove old mock provider initializers
hielfx May 10, 2024
e5267aa
test: remove unnecessary remote branches set on tests
hielfx May 10, 2024
8dff3dd
test: remove unnecessary test initialization
hielfx May 10, 2024
f810e61
test: refactor 99-sample-issue in test cases and use specific branch …
hielfx May 10, 2024
a103d3a
refactor: add fake issue tracker
hielfx May 10, 2024
db97a52
chore: remove fakes from sonar scanner
hielfx May 10, 2024
bdb8f6d
teat: add fake github issue tracker on test cases
hielfx May 13, 2024
90b3f2b
test: add fake jira issue tracker on test cases
hielfx May 13, 2024
021d9c4
test: remove unnecessary initialization method
hielfx May 13, 2024
07f4e23
test: add fake issue tracker provider in test cases
hielfx May 13, 2024
b493417
test: remove unnecessary initialization method
hielfx May 13, 2024
fd9c2c4
test: mock GetBranchName method in test with actual expected branch
hielfx May 13, 2024
b95513a
test: remove unnecessary default branch property
hielfx May 13, 2024
c4c2beb
Revert "test: remove unnecessary default branch property"
hielfx May 13, 2024
e96124a
test: mock GetBranchName removing the default value and forcing its u…
hielfx May 13, 2024
7015ca8
chore: remove duplicated .go extension in file
hielfx May 13, 2024
daf7d96
test: replace assertPullRequestNotCalled with actual assert
hielfx May 14, 2024
01c80d3
test: replace assertPullRequestCalled with actual assert
hielfx May 14, 2024
cfb73b3
test: fake git provider initialization outside package
hielfx May 14, 2024
887fa4f
test: fake pull request provider initialization outside package
hielfx May 14, 2024
6660788
test: fake issue tracker initialization outside package
hielfx May 14, 2024
26a7457
test: mock GetBranchName method in create branch test cases
hielfx May 14, 2024
30cda96
test: fake issue tracker provider initialization outside package
hielfx May 14, 2024
e6dfed7
test: unexport helper initializer functions
hielfx May 14, 2024
f98fd77
test: fake issue tracker and issue tracker provider for create branch…
hielfx May 14, 2024
fcd0cc0
test: fake repository provider for crete branch test cases
hielfx May 14, 2024
9c6ad60
test: fake git provider usage for github in create branch test cases
hielfx May 14, 2024
f4e0d2a
test: remove commented loc in test cases
hielfx May 14, 2024
c0e4a4d
test: refactor test with git initialization
hielfx May 14, 2024
d917ec3
test: refactor test with pull request initialization
hielfx May 14, 2024
d677500
test: refactor test with issue tracker initialization
hielfx May 14, 2024
2ed1314
test: refactor test with repository provider initialization
hielfx May 14, 2024
342ffb4
test: refactor test with git provider initalization in branch test cases
hielfx May 15, 2024
50a93eb
test: remove s.Assert().XXXX and use s.XXXX directly as it's the same
hielfx May 15, 2024
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
2 changes: 1 addition & 1 deletion internal/domain/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type RepositoryProvider interface {
}

type PullRequestProvider interface {
GetPullRequestForBranch(string) (pullRequest *PullRequest, err error)
GetPullRequestForBranch(branch string) (pullRequest *PullRequest, err error)
CreatePullRequest(title string, body string, baseBranch string, headBranch string, draft bool, labels []string) (prUrl string, err error)
}

Expand Down
29 changes: 29 additions & 0 deletions internal/fakes/domain/fake_branch_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package domain

import (
"errors"
"fmt"

"github.com/InditexTech/gh-sherpa/internal/domain"
)

type FakeBranchProvider struct{}

var _ domain.BranchProvider = (*FakeBranchProvider)(nil)

func NewFakeBranchProvider() FakeBranchProvider {
return FakeBranchProvider{}
}

func (f *FakeBranchProvider) GetBranchName(issueTracker domain.IssueTracker, issueIdentifier string, repo domain.Repository) (branchName string, err error) {
switch issueTracker.GetIssueTrackerType() {
case domain.IssueTrackerTypeGithub:
return fmt.Sprintf("feature/GH-%s-generated-branch-name", issueIdentifier), nil
case domain.IssueTrackerTypeJira:
return fmt.Sprintf("feature/%s-generated-branch-name", issueIdentifier), nil
default:

}

return "", errors.New("unknown issue tracker type")
}
15 changes: 15 additions & 0 deletions internal/fakes/domain/fake_gh_cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain

import "github.com/InditexTech/gh-sherpa/internal/domain"

type FakeGhCli struct{}

var _ domain.GhCli = (*FakeGhCli)(nil)

func NewFakeGhCli() FakeGhCli {
return FakeGhCli{}
}

func (f FakeGhCli) Execute(result any, args []string) (err error) {
return nil
}
142 changes: 142 additions & 0 deletions internal/fakes/domain/fake_git_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package domain

import (
"errors"
"fmt"
"slices"
"strings"

"github.com/InditexTech/gh-sherpa/internal/domain"
)

type FakeGitProvider struct {
RemoteBranches []string
LocalBranches []string
CurrentBranch string
CommitsToPush map[string][]string
BranchWithCommitError []string
}

var _ domain.GitProvider = (*FakeGitProvider)(nil)

func NewFakeGitProvider() *FakeGitProvider {
return &FakeGitProvider{
CurrentBranch: "main",
RemoteBranches: []string{
"main",
"develop",
},
LocalBranches: []string{
"main",
"develop",
},
CommitsToPush: map[string][]string{},
BranchWithCommitError: []string{},
}
}

func (f *FakeGitProvider) AddLocalBranches(branches ...string) {
f.LocalBranches = append(f.LocalBranches, branches...)
}

func (f *FakeGitProvider) AddRemoteBranches(branches ...string) {
f.RemoteBranches = append(f.RemoteBranches, branches...)
}

func (f *FakeGitProvider) BranchExists(branch string) bool {
return slices.Contains(f.LocalBranches, branch)
}

func (f *FakeGitProvider) FetchBranchFromOrigin(branch string) (err error) {
idx := slices.Index(f.RemoteBranches, branch)
if idx == -1 {
return fmt.Errorf("remote branch %s not found", branch)
}
return nil
}

func (f *FakeGitProvider) CheckoutNewBranchFromOrigin(branch string, base string) (err error) {
idx := slices.Index(f.RemoteBranches, base)
if idx == -1 {
return fmt.Errorf("remote branch %s not found", base)
}
f.LocalBranches = append(f.LocalBranches, branch)
f.CurrentBranch = branch
return nil
}

var ErrGetCurrentBranch = errors.New("no current branch")

func (f *FakeGitProvider) GetCurrentBranch() (branch string, err error) {
if f.CurrentBranch != "" {
return f.CurrentBranch, nil
}

return "", ErrGetCurrentBranch
}

func (f *FakeGitProvider) BranchExistsContains(branch string) (name string, exists bool) {
for _, b := range f.LocalBranches {
if strings.Contains(b, branch) {
return b, true
}
}
for _, b := range f.RemoteBranches {
if strings.Contains(b, branch) {
return b, true
}
}
return "", false
}

func (f *FakeGitProvider) CheckoutBranch(branch string) (err error) {
if !slices.Contains(f.LocalBranches, branch) {
return fmt.Errorf("local branch %s not found", branch)
}
f.CurrentBranch = branch
return nil
}

var ErrGetCommitsToPush = errors.New("error getting commits to push")

func (f *FakeGitProvider) GetCommitsToPush(branch string) (commits []string, err error) {
if slices.Contains(f.BranchWithCommitError, branch) {
return commits, ErrGetCommitsToPush
}

commits, ok := f.CommitsToPush[branch]
if !ok {
commits = []string{}
}

return commits, nil
}

func (f *FakeGitProvider) RemoteBranchExists(branch string) (exists bool) {
return slices.Contains(f.RemoteBranches, branch)
}

func (f *FakeGitProvider) CommitEmpty(message string) (err error) {
currentCommits, ok := f.CommitsToPush[f.CurrentBranch]
if !ok {
currentCommits = []string{}
}
currentCommits = append(currentCommits, message)
f.CommitsToPush[f.CurrentBranch] = currentCommits
return nil
}

func (f *FakeGitProvider) PushBranch(branch string) (err error) {
if !slices.Contains(f.LocalBranches, branch) {
return fmt.Errorf("local branch %s not found", branch)
}
if slices.Contains(f.RemoteBranches, branch) {
return fmt.Errorf("remote branch %s already exists", branch)
}
f.RemoteBranches = append(f.RemoteBranches, branch)
return nil
}

func (f *FakeGitProvider) IsCurrentBranch(branch string) bool {
return f.CurrentBranch == branch
}
105 changes: 105 additions & 0 deletions internal/fakes/domain/fake_issue_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package domain

import (
"fmt"
"strings"

"github.com/InditexTech/gh-sherpa/internal/domain"
"github.com/InditexTech/gh-sherpa/internal/domain/issue_types"
)

type FakeIssueTracker struct {
IssueTrackerType domain.IssueTrackerType
Configurations map[string]FakeIssueTrackerConfiguration
}

type FakeIssueTrackerConfiguration struct {
IssueTrackerIdentifier string
IssueType issue_types.IssueType
IssueTypeLabel string
Issue domain.Issue
}

var _ domain.IssueTracker = (*FakeIssueTracker)(nil)

func NewFakeIssueTracker() *FakeIssueTracker {
return &FakeIssueTracker{
Configurations: map[string]FakeIssueTrackerConfiguration{},
}
}

func ErrIssueNotInitializedInMap(identifier string) error {
return fmt.Errorf("issue %s not initialized in map", identifier)
}

func (f *FakeIssueTracker) AddIssue(issueId string, issueType issue_types.IssueType) {
issueTypeLabel := "kind/" + issueType.String()
f.Configurations[issueId] = FakeIssueTrackerConfiguration{
IssueTrackerIdentifier: f.IssueTrackerType.String(),
IssueType: issueType,
IssueTypeLabel: issueTypeLabel,
Issue: domain.Issue{
ID: issueId,
IssueTracker: f.IssueTrackerType,
Title: "Sample issue",
Body: "Sample issue body",
Labels: []domain.Label{
{
Id: issueTypeLabel,
Name: issueTypeLabel,
},
},
Url: "https://github.com/InditexTech/gh-sherpa-repo-test/issues/" + issueId,
},
}
}

func (f *FakeIssueTracker) GetIssue(issueId string) (issue domain.Issue, err error) {
config, ok := f.Configurations[issueId]
if !ok {
return domain.Issue{}, ErrIssueNotInitializedInMap(issueId)
}
return config.Issue, nil
}

func (f *FakeIssueTracker) GetIssueType(issue domain.Issue) (issueType issue_types.IssueType) {
return f.Configurations[issue.ID].IssueType
}

func (f *FakeIssueTracker) GetIssueTypeLabel(issue domain.Issue) string {
return f.Configurations[issue.ID].IssueTypeLabel
}

func (f *FakeIssueTracker) IdentifyIssue(issueId string) bool {
return f.Configurations[issueId].IssueTrackerIdentifier == string(f.IssueTrackerType)
}

func (f *FakeIssueTracker) FormatIssueId(issueId string) (formattedIssueId string) {
issueTrackerType := f.GetIssueTrackerType()
if issueTrackerType == domain.IssueTrackerTypeGithub {
return fmt.Sprintf("GH-%s", issueId)
}

return issueId
}

func (f *FakeIssueTracker) ParseRawIssueId(identifier string) (issueId string) {
prefix := ""

switch f.IssueTrackerType {
case domain.IssueTrackerTypeGithub:
prefix = "GH-"
case domain.IssueTrackerTypeJira:
prefix = "PROJECTKEY-"
}

if strings.HasPrefix(identifier, prefix) {
return strings.ReplaceAll(identifier, prefix, "")
}

return ""
}

func (f *FakeIssueTracker) GetIssueTrackerType() domain.IssueTrackerType {
return f.IssueTrackerType
}
34 changes: 34 additions & 0 deletions internal/fakes/domain/fake_issue_tracker_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package domain

import (
"errors"
"strconv"
"strings"

"github.com/InditexTech/gh-sherpa/internal/domain"
)

type FakeIssueTrackerProvider struct {
IssueTracker domain.IssueTracker
}

var _ domain.IssueTrackerProvider = (*FakeIssueTrackerProvider)(nil)

func NewFakeIssueTrackerProvider(issueTracker domain.IssueTracker) *FakeIssueTrackerProvider {
return &FakeIssueTrackerProvider{
IssueTracker: issueTracker,
}
}

func (f *FakeIssueTrackerProvider) GetIssueTracker(identifier string) (issueTracker domain.IssueTracker, err error) {
_, err = strconv.Atoi(identifier)
if strings.HasPrefix(identifier, "GH-") || strings.HasPrefix(identifier, "PROJECTKEY-") || err == nil {
return f.IssueTracker, nil
}

return nil, errors.New("issue tracker not found")
}

func (f *FakeIssueTrackerProvider) ParseIssueId(identifier string) (issueId string) {
return strings.TrimPrefix(identifier, "GH-")
}
Loading