Skip to content

Commit

Permalink
Add a test for checking branch name with Jira
Browse files Browse the repository at this point in the history
Co-authored-by: Sami Alajrami <[email protected]>
Co-authored-by: Steve Tooke <[email protected]>
  • Loading branch information
3 people committed Nov 13, 2023
1 parent f7e4ae7 commit 0f321de
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
40 changes: 29 additions & 11 deletions cmd/kosli/reportEvidenceCommitJira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCm
commitMessage: "Lets test EX-1",
},
},
{
name: "report Jira commit evidence with reference as branch name works",
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation
--jira-base-url https://kosli-test.atlassian.net/ --jira-username [email protected]
--repo-root %s
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments),
goldenRegex: "Jira evidence is reported to commit: [0-9a-f]{40}\n.*Issues references reported:.*\n.*EX-1: issue found",
additionalConfig: jiraTestsAdditionalConfig{
branchName: "EX-1",
commitMessage: "test commit has no reference",
},
},
{
name: "report Jira commit evidence with a slash at the end of --jira-base-url works",
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation
Expand All @@ -137,7 +149,7 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCm
{
wantError: true,
name: "report Jira commit evidence with --jira-pat and --jira-api-token fails",
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation
--jira-base-url https://kosli-test.atlassian.net --jira-api-token xxx
--jira-pat xxxx --repo-root %s --commit 61ab3ea22bd4264996b35bfb82869c482d9f4a06
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments),
Expand All @@ -163,20 +175,26 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCm
},
}
for _, test := range tests {
if test.additionalConfig != nil {
branchName := test.additionalConfig.(jiraTestsAdditionalConfig).branchName
if branchName != "" {
testHelpers.CheckoutNewBranch(suite.workTree, branchName)
}
msg := test.additionalConfig.(jiraTestsAdditionalConfig).commitMessage
commitSha, err := testHelpers.CommitToRepo(suite.workTree, suite.fs, msg)
require.NoError(suite.T(), err)
funcName(test, suite)
}
}

test.cmd = test.cmd + " --commit " + commitSha
func funcName(test cmdTestCase, suite *CommitEvidenceJiraCommandTestSuite) {
if test.additionalConfig != nil {
branchName := test.additionalConfig.(jiraTestsAdditionalConfig).branchName
if branchName != "" {
err := testHelpers.CheckoutNewBranch(suite.workTree, branchName)
require.NoError(suite.T(), err)
defer testHelpers.CheckoutMaster(suite.workTree, suite.T())
}
msg := test.additionalConfig.(jiraTestsAdditionalConfig).commitMessage
commitSha, err := testHelpers.CommitToRepo(suite.workTree, suite.fs, msg)
require.NoError(suite.T(), err)

runTestCmd(suite.T(), []cmdTestCase{test})
test.cmd = test.cmd + " --commit " + commitSha
}

runTestCmd(suite.T(), []cmdTestCase{test})
}

// In order for 'go test' to run this suite, we need to create
Expand Down
15 changes: 14 additions & 1 deletion internal/testHelpers/testHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testHelpers
import (
"fmt"
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -50,6 +51,11 @@ func InitializeGitRepo(repoPath string) (*git.Repository, *git.Worktree, billy.F
return repo, nil, fs, err
}

_, err = CommitToRepo(w, fs, "Initial Commit")
if err != nil {
return repo, w, fs, err
}

return repo, w, fs, nil
}

Expand Down Expand Up @@ -82,6 +88,13 @@ func CommitToRepo(w *git.Worktree, fs billy.Filesystem, commitMessage string) (s
func CheckoutNewBranch(w *git.Worktree, branchName string) error {
return w.Checkout(&git.CheckoutOptions{
Create: true,
Branch: plumbing.ReferenceName(branchName),
Branch: plumbing.NewBranchReferenceName(branchName),
})
}

func CheckoutMaster(workTree *git.Worktree, t *testing.T) {
err := workTree.Checkout(&git.CheckoutOptions{
Branch: plumbing.Master,
})
require.NoError(t, err)
}

0 comments on commit 0f321de

Please sign in to comment.