2020-07-09 16:40:37 -04:00
|
|
|
package git_test
|
2017-01-14 19:12:20 +01:00
|
|
|
|
|
|
|
import (
|
2022-04-12 08:35:19 -03:00
|
|
|
"context"
|
2017-01-14 19:12:20 +01:00
|
|
|
"testing"
|
|
|
|
|
2020-07-09 16:40:37 -04:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2017-10-15 17:46:21 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2020-10-06 09:48:04 -03:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-01-14 19:12:20 +01:00
|
|
|
)
|
|
|
|
|
2021-11-28 10:35:01 -03:00
|
|
|
func TestNotARepo(t *testing.T) {
|
|
|
|
testlib.Mktmp(t)
|
2022-04-12 08:35:19 -03:00
|
|
|
_, err := git.ExtractRepoFromConfig(context.Background())
|
2021-11-28 10:35:01 -03:00
|
|
|
require.EqualError(t, err, `current folder is not a git repository`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoRemote(t *testing.T) {
|
|
|
|
testlib.Mktmp(t)
|
|
|
|
testlib.GitInit(t)
|
2022-04-12 08:35:19 -03:00
|
|
|
_, err := git.ExtractRepoFromConfig(context.Background())
|
2021-11-28 10:35:01 -03:00
|
|
|
require.EqualError(t, err, `no remote configured to list refs from`)
|
|
|
|
}
|
|
|
|
|
2017-01-14 19:12:20 +01:00
|
|
|
func TestRepoName(t *testing.T) {
|
2020-12-12 13:27:35 -03:00
|
|
|
testlib.Mktmp(t)
|
2017-10-15 17:46:21 -02:00
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
|
2022-04-12 08:35:19 -03:00
|
|
|
repo, err := git.ExtractRepoFromConfig(context.Background())
|
2021-09-15 22:12:45 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "goreleaser/goreleaser", repo.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoNameWithDifferentRemote(t *testing.T) {
|
2022-04-12 08:35:19 -03:00
|
|
|
ctx := context.Background()
|
2021-09-15 22:12:45 +03:00
|
|
|
testlib.Mktmp(t)
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAddWithName(t, "upstream", "https://github.com/goreleaser/goreleaser.git")
|
2022-04-12 08:35:19 -03:00
|
|
|
_, err := git.Run(ctx, "pull", "upstream", "main")
|
2021-09-15 22:12:45 +03:00
|
|
|
require.NoError(t, err)
|
2022-04-12 08:35:19 -03:00
|
|
|
_, err = git.Run(ctx, "branch", "--set-upstream-to", "upstream/main")
|
2021-09-15 22:12:45 +03:00
|
|
|
require.NoError(t, err)
|
2022-04-12 08:35:19 -03:00
|
|
|
repo, err := git.ExtractRepoFromConfig(ctx)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "goreleaser/goreleaser", repo.String())
|
2017-01-14 19:12:20 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 18:21:20 -03:00
|
|
|
func TestExtractRepoFromURL(t *testing.T) {
|
2021-08-21 10:57:19 -03:00
|
|
|
// valid urls
|
2018-02-20 18:21:20 -03:00
|
|
|
for _, url := range []string{
|
|
|
|
"git@github.com:goreleaser/goreleaser.git",
|
|
|
|
"git@custom:goreleaser/goreleaser.git",
|
2021-09-28 10:43:54 -03:00
|
|
|
"https://foo@github.com/goreleaser/goreleaser",
|
2018-02-20 18:21:20 -03:00
|
|
|
"https://github.com/goreleaser/goreleaser.git",
|
2021-11-28 10:35:01 -03:00
|
|
|
"https://something.with.port:8080/goreleaser/goreleaser.git",
|
2018-02-20 18:21:20 -03:00
|
|
|
"https://github.enterprise.com/goreleaser/goreleaser.git",
|
2021-09-27 20:08:22 -04:00
|
|
|
"https://gitlab-ci-token:SOME_TOKEN@gitlab.yourcompany.com/goreleaser/goreleaser.git",
|
2018-02-20 18:21:20 -03:00
|
|
|
} {
|
|
|
|
t.Run(url, func(t *testing.T) {
|
2021-08-21 10:57:19 -03:00
|
|
|
repo, err := git.ExtractRepoFromURL(url)
|
|
|
|
require.NoError(t, err)
|
2020-10-06 09:48:04 -03:00
|
|
|
require.Equal(t, "goreleaser/goreleaser", repo.String())
|
2022-05-09 09:32:43 -03:00
|
|
|
require.NoError(t, repo.CheckSCM())
|
|
|
|
require.Equal(t, url, repo.RawURL)
|
2018-02-20 18:21:20 -03:00
|
|
|
})
|
|
|
|
}
|
2021-08-21 10:57:19 -03:00
|
|
|
|
2021-09-23 05:34:14 +02:00
|
|
|
// nested urls
|
|
|
|
for _, url := range []string{
|
|
|
|
"git@custom:group/nested/goreleaser/goreleaser.git",
|
|
|
|
"https://gitlab.mycompany.com/group/nested/goreleaser/goreleaser.git",
|
2021-09-28 10:43:54 -03:00
|
|
|
"https://gitlab-ci-token:SOME_TOKEN@gitlab.yourcompany.com/group/nested/goreleaser/goreleaser.git",
|
2021-09-23 05:34:14 +02:00
|
|
|
} {
|
|
|
|
t.Run(url, func(t *testing.T) {
|
|
|
|
repo, err := git.ExtractRepoFromURL(url)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "group/nested/goreleaser/goreleaser", repo.String())
|
2022-05-09 09:32:43 -03:00
|
|
|
require.NoError(t, repo.CheckSCM())
|
|
|
|
require.Equal(t, url, repo.RawURL)
|
2021-09-23 05:34:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-21 10:57:19 -03:00
|
|
|
for _, url := range []string{
|
|
|
|
"git@gist.github.com:someid.git",
|
|
|
|
"https://gist.github.com/someid.git",
|
2022-05-09 09:32:43 -03:00
|
|
|
} {
|
|
|
|
t.Run(url, func(t *testing.T) {
|
|
|
|
repo, err := git.ExtractRepoFromURL(url)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "someid", repo.String())
|
|
|
|
require.Error(t, repo.CheckSCM())
|
|
|
|
require.Equal(t, url, repo.RawURL)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalid urls
|
|
|
|
for _, url := range []string{
|
|
|
|
"git@gist.github.com:",
|
|
|
|
"https://gist.github.com/",
|
2021-08-21 10:57:19 -03:00
|
|
|
} {
|
|
|
|
t.Run(url, func(t *testing.T) {
|
|
|
|
repo, err := git.ExtractRepoFromURL(url)
|
|
|
|
require.EqualError(t, err, "unsupported repository URL: "+url)
|
|
|
|
require.Equal(t, "", repo.String())
|
2022-05-09 09:32:43 -03:00
|
|
|
require.Error(t, repo.CheckSCM())
|
|
|
|
require.Equal(t, url, repo.RawURL)
|
2021-08-21 10:57:19 -03:00
|
|
|
})
|
|
|
|
}
|
2017-01-14 19:12:20 +01:00
|
|
|
}
|