2020-07-09 22:40:37 +02:00
|
|
|
package git_test
|
2017-01-14 20:12:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-07-09 22:40:37 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2017-10-15 21:46:21 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2020-10-06 14:48:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-01-14 20:12:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepoName(t *testing.T) {
|
2020-12-12 18:27:35 +02:00
|
|
|
testlib.Mktmp(t)
|
2017-10-15 21:46:21 +02:00
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
|
2020-07-09 22:40:37 +02:00
|
|
|
repo, err := git.ExtractRepoFromConfig()
|
2021-09-15 21:12:45 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "goreleaser/goreleaser", repo.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoNameWithDifferentRemote(t *testing.T) {
|
|
|
|
testlib.Mktmp(t)
|
|
|
|
testlib.GitInit(t)
|
|
|
|
testlib.GitRemoteAddWithName(t, "upstream", "https://github.com/goreleaser/goreleaser.git")
|
2021-11-13 04:11:44 +02:00
|
|
|
_, err := git.Run("pull", "upstream", "main")
|
2021-09-15 21:12:45 +02:00
|
|
|
require.NoError(t, err)
|
2021-11-13 04:11:44 +02:00
|
|
|
_, err = git.Run("branch", "--set-upstream-to", "upstream/main")
|
2021-09-15 21:12:45 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
repo, err := git.ExtractRepoFromConfig()
|
2020-10-06 14:48:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "goreleaser/goreleaser", repo.String())
|
2017-01-14 20:12:20 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 23:21:20 +02:00
|
|
|
func TestExtractRepoFromURL(t *testing.T) {
|
2021-08-21 15:57:19 +02:00
|
|
|
// valid urls
|
2018-02-20 23:21:20 +02:00
|
|
|
for _, url := range []string{
|
|
|
|
"git@github.com:goreleaser/goreleaser.git",
|
|
|
|
"git@custom:goreleaser/goreleaser.git",
|
2021-09-28 15:43:54 +02:00
|
|
|
"https://foo@github.com/goreleaser/goreleaser",
|
2018-02-20 23:21:20 +02:00
|
|
|
"https://github.com/goreleaser/goreleaser.git",
|
|
|
|
"https://github.enterprise.com/goreleaser/goreleaser.git",
|
2021-09-28 02:08:22 +02:00
|
|
|
"https://gitlab-ci-token:SOME_TOKEN@gitlab.yourcompany.com/goreleaser/goreleaser.git",
|
2018-02-20 23:21:20 +02:00
|
|
|
} {
|
|
|
|
t.Run(url, func(t *testing.T) {
|
2021-08-21 15:57:19 +02:00
|
|
|
repo, err := git.ExtractRepoFromURL(url)
|
|
|
|
require.NoError(t, err)
|
2020-10-06 14:48:04 +02:00
|
|
|
require.Equal(t, "goreleaser/goreleaser", repo.String())
|
2018-02-20 23:21:20 +02:00
|
|
|
})
|
|
|
|
}
|
2021-08-21 15:57:19 +02: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 15:43:54 +02: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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-21 15:57:19 +02:00
|
|
|
// invalid urls
|
|
|
|
for _, url := range []string{
|
|
|
|
"git@gist.github.com:someid.git",
|
|
|
|
"https://gist.github.com/someid.git",
|
|
|
|
} {
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|
2017-01-14 20:12:20 +02:00
|
|
|
}
|