Skip to content

Commit

Permalink
Add test for gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
matsest committed Dec 25, 2019
1 parent bd78738 commit b550a8e
Showing 1 changed file with 194 additions and 0 deletions.
194 changes: 194 additions & 0 deletions release/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package release

import (
"context"
"errors"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/xanzy/go-gitlab"
)

// GitLabClient is a mock GitLabAPI implementation.
type mockGitLabClient struct {
projectRelease *gitlab.Release
response *gitlab.Response
err error
}

func (c *mockGitLabClient) ProjectGetLatestRelease(ctx context.Context, owner, project string) (*gitlab.Release, *gitlab.Response, error) {
return c.projectRelease, c.response, c.err
}

func TestNewGitLabClient(t *testing.T) {
cases := []struct {
baseURL string
want string
ok bool
}{
{
baseURL: "",
want: "https://gitlab.com/api/v4/",
ok: true,
},
{
baseURL: "https://gitlab.com/api/v4/",
want: "https://gitlab.com/api/v4/",
ok: true,
},
{
baseURL: "http://localhost/api/v4/",
want: "http://localhost/api/v4/",
ok: true,
},
{
baseURL: `https://gitlab\.com/api/v4/`,
want: "",
ok: false,
},
}

for _, tc := range cases {
config := GitLabConfig{
BaseURL: tc.baseURL,
Token: "dummy_token",
}
got, err := NewGitLabClient(config)

if tc.ok && err != nil {
t.Errorf("NewGitLabClient() with baseURL = %s returns unexpected err: %s", tc.baseURL, err)
}

if !tc.ok && err == nil {
t.Errorf("NewGitLabClient() with baseURL = %s expects to return an error, but no error", tc.baseURL)
}

if tc.ok {
if got.client.BaseURL().String() != tc.want {
t.Errorf("NewGitLabClient() with baseURL = %s returns %s, but want %s", tc.baseURL, got.client.BaseURL().String(), tc.want)
}
}
}
}

func TestNewGitLabRelease(t *testing.T) {
cases := []struct {
source string
api GitLabAPI
owner string
project string
ok bool
}{
{
source: "gitlab-org/gitlab",
api: &mockGitLabClient{},
owner: "gitlab-org",
project: "gitlab",
ok: true,
},
{
source: "gitlab",
api: &mockGitLabClient{},
owner: "",
project: "",
ok: false,
},
}

for _, tc := range cases {
config := GitLabConfig{
api: tc.api,
}
got, err := NewGitLabRelease(tc.source, config)

if tc.ok && err != nil {
t.Errorf("NewGitLabRelease() with source = %s, api = %#v returns unexpected err: %s", tc.source, tc.api, err)
}

if !tc.ok && err == nil {
t.Errorf("NewGitLabRelease() with source = %s, api = %#v expects to return an error, but no error", tc.source, tc.api)
}

if tc.ok {
r := got

if r.api != tc.api {
t.Errorf("NewGitLabRelease() with source = %s, api = %#v sets api = %#v, but want %s", tc.source, tc.api, r.api, tc.api)
}

if !(r.owner == tc.owner && r.project == tc.project) {
t.Errorf("NewGitLabRelease() with source = %s, api = %#v returns (%s, %s), but want (%s, %s)", tc.source, tc.api, r.owner, r.project, tc.owner, tc.project)
}
}
}
}

func TestGitLabReleaseLatest(t *testing.T) {
tagv010 := "v0.1.0"
tag010 := "0.1.0"
cases := []struct {
client *mockGitLabClient
want string
ok bool
}{
{
client: &mockGitLabClient{
projectRelease: &gitlab.Release{
TagName: tagv010,
},
response: &gitlab.Response{},
err: nil,
},
want: "0.1.0",
ok: true,
},
{
client: &mockGitLabClient{
projectRelease: &gitlab.Release{
TagName: tag010,
},
response: &gitlab.Response{},
err: nil,
},
want: "0.1.0",
ok: true,
},
{
client: &mockGitLabClient{
projectRelease: nil,
response: &gitlab.Response{},
// Actual error response type is *gitlab.ErrorResponse,
// but we are not interested in the internal structure.
err: errors.New(`GET https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/releases: 404 Not Found []`),
},
want: "",
ok: false,
},
}

source := "gitlab-org/gitlab"
for _, tc := range cases {
// Set a mock client
config := GitLabConfig{
api: tc.client,
}
r, err := NewGitLabRelease(source, config)
if err != nil {
t.Fatalf("failed to NewGitLabRelease(%s, %#v): %s", source, config, err)
}

got, err := r.Latest(context.Background())

if tc.ok && err != nil {
t.Errorf("(*GitLabRelease).Latest() with r = %s returns unexpected err: %+v", spew.Sdump(r), err)
}

if !tc.ok && err == nil {
t.Errorf("(*GitLabRelease).Latest() with r = %s expects to return an error, but no error", spew.Sdump(r))
}

if got != tc.want {
t.Errorf("(*GitLabRelease).Latest() with r = %s returns %s, but want = %s", spew.Sdump(r), got, tc.want)
}
}
}

0 comments on commit b550a8e

Please sign in to comment.