1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-13 11:50:34 +02:00

test: additional tests for gitlab and others (#2599)

* More checks

* test: Adding more tests around gitlab

* chore: removing log
This commit is contained in:
Drew Stinnett 2021-10-25 09:29:19 -04:00 committed by GitHub
parent 69f8927970
commit d4e575dd47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 202 additions and 0 deletions

View File

@ -157,3 +157,8 @@ func TestNewWithToken(t *testing.T) {
require.Nil(t, cli)
})
}
func TestClientBlanks(t *testing.T) {
repo := Repo{}
require.Equal(t, "", repo.String())
}

View File

@ -0,0 +1,27 @@
package client
import (
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
func TestRepoFromRef(t *testing.T) {
owner := "someowner"
name := "somename"
branch := "somebranch"
token := "sometoken"
ref := config.RepoRef{
Owner: owner,
Name: name,
Branch: branch,
Token: token,
}
repo := RepoFromRef(ref)
require.Equal(t, owner, repo.Owner)
require.Equal(t, name, repo.Name)
require.Equal(t, branch, repo.Branch)
}

View File

@ -426,3 +426,112 @@ func TestGitlabChangelog(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "6dcb09b5: Fix all the bugs (Joey User <joey@user.edu>)", log)
}
func TestGitlabCreateFile(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Handle the test where we know the branch
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/newfile.txt") {
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "newfile.txt", "branch": "somebranch" }`))
require.NoError(t, err)
return
}
// Handle the test where we detect the branch
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/newfile-in-default.txt") {
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "newfile.txt", "branch": "main" }`))
require.NoError(t, err)
return
}
// File of doooom...gets created, but 404s when getting fetched
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/files/doomed-file-404.txt") {
if r.Method == "PUT" {
_, err := io.Copy(w, strings.NewReader(`{ "file_path": "doomed-file-404.txt", "branch": "main" }`))
require.NoError(t, err)
} else {
w.WriteHeader(http.StatusNotFound)
}
return
}
defer r.Body.Close()
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
// Test using an arbitrary branch
repo := Repo{
Owner: "someone",
Name: "something",
Branch: "somebranch",
}
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "newfile.txt", "test: test commit")
require.NoError(t, err)
// Test detecting the default branch
repo = Repo{
Owner: "someone",
Name: "something",
// Note there is no branch here, gonna try and guess it!
}
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "newfile-in-default.txt", "test: test commit")
require.NoError(t, err)
// Test a doomed file. This is a file that is 'successfully' created, but returns a 404 when trying to fetch
repo = Repo{
Owner: "someone",
Name: "something",
Branch: "doomed",
}
err = client.CreateFile(ctx, config.CommitAuthor{Name: repo.Owner}, repo, []byte("Hello there"), "doomed-file-404.txt", "test: test commit")
require.Error(t, err)
}
func TestCloseMileston(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "projects/someone/something/milestones") {
r, err := os.Open("testdata/gitlab/milestones.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
require.NoError(t, err)
return
} else if strings.HasSuffix(r.URL.Path, "projects/someone/something/milestones/12") {
r, err := os.Open("testdata/gitlab/milestone.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
require.NoError(t, err)
return
}
defer r.Body.Close()
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
repo := Repo{
Owner: "someone",
Name: "something",
}
err = client.CloseMilestone(ctx, repo, "10.0")
require.NoError(t, err)
// Be sure to error on missing milestones
err = client.CloseMilestone(ctx, repo, "never-will-exist")
require.Error(t, err)
}

View File

@ -0,0 +1,13 @@
{
"id": 12,
"iid": 3,
"project_id": 16,
"title": "10.0",
"description": "Version",
"due_date": "2013-11-29",
"start_date": "2013-11-10",
"state": "active",
"updated_at": "2013-10-02T09:24:18Z",
"created_at": "2013-10-02T09:24:18Z",
"expired": false
}

View File

@ -0,0 +1,15 @@
[
{
"id": 12,
"iid": 3,
"project_id": 16,
"title": "10.0",
"description": "Version",
"due_date": "2013-11-29",
"start_date": "2013-11-10",
"state": "active",
"updated_at": "2013-10-02T09:24:18Z",
"created_at": "2013-10-02T09:24:18Z",
"expired": false
}
]

View File

@ -60,6 +60,17 @@ func TestEqualFilesDontExist(t *testing.T) {
require.Error(t, Copy(b, c))
}
func TestCopyErrors(t *testing.T) {
a := "testdata/nope.txt"
b := "testdata/also-nope.txt"
err := copySymlink(a, b)
require.Error(t, err)
err = copyFile(a, b, 0o755)
require.Error(t, err)
}
func TestCopyFile(t *testing.T) {
dir := t.TempDir()
src, err := ioutil.TempFile(dir, "src")

22
internal/gio/hash_test.go Normal file
View File

@ -0,0 +1,22 @@
package gio
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEqualFilesError(t *testing.T) {
tests := []struct {
a string
b string
}{
{"./testdata/nope.txt", "./testdata/somefile.txt"},
{"./testdata/somefile.txt", "./testdata/nope.txt"},
}
for _, test := range tests {
equal, err := EqualFiles(test.a, test.b)
require.Error(t, err)
require.False(t, equal)
}
}