Simplify Go Endpoints + Add Tests (#120)

This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
Harrison (Harry) Cramer
2023-12-04 10:15:07 -05:00
committed by GitHub
parent 10b0b596ae
commit 93fe3e8bd6
41 changed files with 1745 additions and 728 deletions

View File

@@ -1,251 +0,0 @@
package main
import (
"errors"
"fmt"
"testing"
)
func TestExtractGitInfo_Success(t *testing.T) {
getCurrentBranchName := func() (string, error) {
return "feature/abc", nil
}
refreshGitInfo := func() error {
return nil
}
testCases := []struct {
getProjectRemoteUrl func() (string, error)
expected GitProjectInfo
desc string
}{
{
desc: "Project configured in SSH under a single folder",
getProjectRemoteUrl: func() (string, error) {
return "git@custom-gitlab.com:namespace-1/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "git@custom-gitlab.com:namespace-1/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1",
},
},
{
desc: "Project configured in SSH under a single folder without .git extension",
getProjectRemoteUrl: func() (string, error) {
return "git@custom-gitlab.com:namespace-1/project-name", nil
},
expected: GitProjectInfo{
RemoteUrl: "git@custom-gitlab.com:namespace-1/project-name",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1",
},
},
{
desc: "Project configured in SSH under one nested folder",
getProjectRemoteUrl: func() (string, error) {
return "git@custom-gitlab.com:namespace-1/namespace-2/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "git@custom-gitlab.com:namespace-1/namespace-2/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1/namespace-2",
},
},
{
desc: "Project configured in SSH under two nested folders",
getProjectRemoteUrl: func() (string, error) {
return "git@custom-gitlab.com:namespace-1/namespace-2/namespace-3/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "git@custom-gitlab.com:namespace-1/namespace-2/namespace-3/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1/namespace-2/namespace-3",
},
},
{
desc: "Project configured in SSH:// under a single folder",
getProjectRemoteUrl: func() (string, error) {
return "ssh://custom-gitlab.com/namespace-1/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "ssh://custom-gitlab.com/namespace-1/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1",
},
},
{
desc: "Project configured in SSH:// under a single folder without .git extension",
getProjectRemoteUrl: func() (string, error) {
return "ssh://custom-gitlab.com/namespace-1/project-name", nil
},
expected: GitProjectInfo{
RemoteUrl: "ssh://custom-gitlab.com/namespace-1/project-name",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1",
},
},
{
desc: "Project configured in SSH:// under two nested folders",
getProjectRemoteUrl: func() (string, error) {
return "ssh://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "ssh://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1/namespace-2/namespace-3",
},
},
{
desc: "Project configured in HTTP and under a single folder without .git extension",
getProjectRemoteUrl: func() (string, error) {
return "http://custom-gitlab.com/namespace-1/project-name", nil
},
expected: GitProjectInfo{
RemoteUrl: "http://custom-gitlab.com/namespace-1/project-name",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1",
},
},
{
desc: "Project configured in HTTPS and under a single folder",
getProjectRemoteUrl: func() (string, error) {
return "https://custom-gitlab.com/namespace-1/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "https://custom-gitlab.com/namespace-1/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1",
},
},
{
desc: "Project configured in HTTPS and under a nested folder",
getProjectRemoteUrl: func() (string, error) {
return "https://custom-gitlab.com/namespace-1/namespace-2/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "https://custom-gitlab.com/namespace-1/namespace-2/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1/namespace-2",
},
},
{
desc: "Project configured in HTTPS and under two nested folders",
getProjectRemoteUrl: func() (string, error) {
return "https://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git", nil
},
expected: GitProjectInfo{
RemoteUrl: "https://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
BranchName: "feature/abc",
ProjectName: "project-name",
Namespace: "namespace-1/namespace-2/namespace-3",
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
actual, err := ExtractGitInfo(refreshGitInfo, tC.getProjectRemoteUrl, getCurrentBranchName)
if err != nil {
t.Errorf("No error was expected, got %s", err)
}
if actual != tC.expected {
t.Errorf("\nExpected: %s\nActual: %s", tC.expected, actual)
}
})
}
}
func TestExtractGitInfo_FailToGetProjectRemoteUrl(t *testing.T) {
getCurrentBranchName := func() (string, error) {
return "feature/abc", nil
}
refreshGitInfo := func() error {
return nil
}
testCases := []struct {
getProjectRemoteUrl func() (string, error)
expectedErrorMessage string
desc string
}{
{
desc: "Error returned by function to get the project remote url",
getProjectRemoteUrl: func() (string, error) {
return "", errors.New("error when getting project remote url")
},
expectedErrorMessage: "Could not get project Url: error when getting project remote url",
},
{
desc: "Invalid project remote url",
getProjectRemoteUrl: func() (string, error) {
return "git@invalid", nil
},
expectedErrorMessage: "Invalid Git URL format: git@invalid",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
_, actualErr := ExtractGitInfo(refreshGitInfo, tC.getProjectRemoteUrl, getCurrentBranchName)
if actualErr == nil {
t.Errorf("Expected an error, got none")
}
if actualErr.Error() != tC.expectedErrorMessage {
t.Errorf("\nExpected: %s\nActual: %s", tC.expectedErrorMessage, actualErr.Error())
}
})
}
}
func TestExtractGitInfo_FailToGetCurrentBranchName(t *testing.T) {
expectedErrNestedMsg := "error when getting current branch name"
refreshGitInfo := func() error {
return nil
}
_, actualErr := ExtractGitInfo(refreshGitInfo,
func() (string, error) {
return "git@custom-gitlab.com:namespace/project.git", nil
},
func() (string, error) {
return "", errors.New(expectedErrNestedMsg)
})
if actualErr == nil {
t.Errorf("Expected an error, got none")
}
expectedErr := fmt.Errorf("Failed to get current branch: %s", expectedErrNestedMsg)
if actualErr.Error() != expectedErr.Error() {
t.Errorf("\nExpected: %s\nActual: %s", expectedErr, actualErr)
}
}
func TestRefreshGitRemote_FailToRefreshRemote(t *testing.T) {
expectedErrNestedMsg := "error when fetching origin commits"
_, actualErr := ExtractGitInfo(
func() error {
return errors.New(expectedErrNestedMsg)
},
func() (string, error) {
return "git@custom-gitlab.com:namespace/project.git", nil
},
func() (string, error) {
return "feature/abc", nil
},
)
if actualErr == nil {
t.Errorf("Expected an error, got none")
}
expectedErr := fmt.Errorf("Could not get latest information from remote: %s", expectedErrNestedMsg)
if actualErr.Error() != expectedErr.Error() {
t.Errorf("\nExpected: %s\nActual: %s", expectedErr, actualErr)
}
}