Skip to content
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
42 changes: 42 additions & 0 deletions pkg/util/gitlab/ci_yml_templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gitlab

import (
"os"
"strings"
"testing"
)

var gitlab_token = "FAKE_GITLAB_TOKEN"

func TestClient_GetGitLabCIGolangTemplate(t *testing.T) {
baseURL := ""
os.Setenv("GITLAB_TOKEN", gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
client *Client
wantContent bool
wantErr bool
}{
// TODO: Add test cases.
{"base", client, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.client.GetGitLabCIGolangTemplate()
if (err != nil) != tt.wantErr {
t.Errorf("Client.GetGitLabCIGolangTemplate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !strings.Contains(got, "golang") {
t.Errorf("Client.GetGitLabCIGolangTemplate's content doesn't include 'golang'")
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/util/gitlab/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *Client) UpdateSingleFile(project, branch, commitMessage, filename, cont
}

func (c *Client) CommitMultipleFiles(project, branch, commitMessage string, files map[string][]byte) error {
var commitActionsOptions = make([]*gitlab.CommitActionOptions, 0)
var commitActionsOptions = make([]*gitlab.CommitActionOptions, len(files))

for fileName, content := range files {
commitActionsOptions = append(commitActionsOptions, &gitlab.CommitActionOptions{
Expand Down
133 changes: 133 additions & 0 deletions pkg/util/gitlab/commits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package gitlab

import (
"os"
"testing"
)

func TestClient_CommitSingleFile(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
client *Client
project string
branch string
commitMessage string
filename string
content string
wantErr bool
}{
// TODO: Add test cases.
{"base", client, "", "", "", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.client.CommitSingleFile(tt.project, tt.branch, tt.commitMessage, tt.filename, tt.content); (err != nil) != tt.wantErr {
t.Errorf("Client.CommitSingleFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestClient_DeleteSingleFile(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
client *Client
project string
branch string
commitMessage string
filename string
wantErr bool
}{
// TODO: Add test cases.
{"base", client, "", "", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.client.DeleteSingleFile(tt.project, tt.branch, tt.commitMessage, tt.filename); (err != nil) != tt.wantErr {
t.Errorf("Client.DeleteSingleFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestClient_UpdateSingleFile(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
client *Client
project string
branch string
commitMessage string
filename string
content string
wantErr bool
}{
// TODO: Add test cases.
{"base", client, "", "", "", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.client.UpdateSingleFile(tt.project, tt.branch, tt.commitMessage, tt.filename, tt.content); (err != nil) != tt.wantErr {
t.Errorf("Client.UpdateSingleFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestClient_CommitMultipleFiles(t *testing.T) {
m := map[string][]byte{
"a.txt": []byte("a.txt"),
"b.txt": []byte("b.txt"),
"c.txt": []byte("c.txt"),
}
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
client *Client
project string
branch string
commitMessage string
files map[string][]byte
wantErr bool
}{
// TODO: Add test cases.
{"base", client, "", "", "", m, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.client.CommitMultipleFiles(tt.project, tt.branch, tt.commitMessage, tt.files); (err != nil) != tt.wantErr {
t.Errorf("Client.CommitMultipleFiles() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
10 changes: 0 additions & 10 deletions pkg/util/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import (
"os"

"github.com/xanzy/go-gitlab"

"github.com/devstream-io/devstream/pkg/util/log"
)

var client *Client

const (
DefaultGitlabHost = "https://gitlab.com"
)
Expand All @@ -29,11 +25,6 @@ type Client struct {
}

func NewClient(opts ...OptionFunc) (*Client, error) {
if client != nil {
log.Debug("Use a cached client.")
return client, nil
}

token := os.Getenv("GITLAB_TOKEN")
if token == "" {
return nil, errors.New("failed to read GITLAB_TOKEN from environment variable")
Expand All @@ -51,7 +42,6 @@ func NewClient(opts ...OptionFunc) (*Client, error) {
c.Client, err = gitlab.NewClient(token)
} else {
c.Client, err = gitlab.NewClient(token, gitlab.WithBaseURL(c.baseURL))

}

if err != nil {
Expand Down
69 changes: 69 additions & 0 deletions pkg/util/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package gitlab

import (
"os"
"testing"
)

var fake_gitlab_token = "FAKE_GITLAB_TOKEN"
var baseURL = ""

func TestWithBaseURL(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
got := WithBaseURL(baseURL)
if got == nil {
t.Error("got must not be nil\n")
}
client, err := NewClient(got)
if err != nil {
t.Error(err)
}
if client.baseURL != baseURL {
t.Errorf("client.baseURL = %s\n, want = %s\n", client.baseURL, baseURL)
}
}

func TestNewClient(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()

baseURL := ""
opts := []OptionFunc{
WithBaseURL(baseURL),
}
opts2 := []OptionFunc{
WithBaseURL(DefaultGitlabHost),
}
testDataName := "last case: empty token"
tests := []struct {
name string
opts []OptionFunc
isNilClient bool
wantErr bool
}{
// TODO: Add test cases.
{"base", opts, false, false},
{"base not empty baseUrl", opts2, false, false},
{testDataName, opts, true, true},
}
for _, tt := range tests {
if tt.name == testDataName {
os.Setenv("GITLAB_TOKEN", "")
}
t.Run(tt.name, func(t *testing.T) {
got, err := NewClient(tt.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr)
return
}
if (got != nil) == tt.isNilClient {
t.Errorf("NewClient() = %+v\n, want %+v\n", got, tt.isNilClient)
}
})
}
}
109 changes: 109 additions & 0 deletions pkg/util/gitlab/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package gitlab

import (
"os"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/xanzy/go-gitlab"
)

func TestClient_CreateProject(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
baseURL string
Client *Client
opts *CreateProjectOptions
wantErr bool
}{
// TODO: Add test cases.
{"base", baseURL, client,
&CreateProjectOptions{
Namespace: "",
}, true},
{"base Namespace is not empty", baseURL, client,
&CreateProjectOptions{
Namespace: "test",
}, true},
{"base Visibility is `public`", baseURL, client,
&CreateProjectOptions{
Visibility: "public",
}, true},
{"base Visibility is `internal`", baseURL, client,
&CreateProjectOptions{
Visibility: "internal",
}, true},
{"base Visibility is `private`", baseURL, client,
&CreateProjectOptions{
Visibility: "private",
}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.Client.CreateProject(tt.opts); (err != nil) != tt.wantErr {
t.Errorf("Client.CreateProject() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestClient_DeleteProject(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}

err = client.DeleteProject("")
assert.NotNil(t, err)

err = client.DeleteProject("test")
assert.NotNil(t, err)

}

func TestClient_DescribeProject(t *testing.T) {
os.Setenv("GITLAB_TOKEN", fake_gitlab_token)
defer func() {
os.Unsetenv("GITLAB_TOKEN")
}()
client, err := NewClient(WithBaseURL(baseURL))
if err != nil {
t.Error(err)
}
tests := []struct {
name string
Client *Client
project string
want *gitlab.Project
wantErr bool
}{
// TODO: Add test cases.
{"base", client, "", nil, true},
{"base", client, "test", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.Client.DescribeProject(tt.project)
if (err != nil) != tt.wantErr {
t.Errorf("Client.DescribeProject() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Client.DescribeProject() = %v, want %v", got, tt.want)
}
})
}
}
Loading