1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/git/config_test.go
Aaron U'Ren 917cae54f0
fix(config): handle relative git repos (#4575)
## If applied, this commit will...

If applied this change will allow goreleaser to handle relative remotes
when attempting to parse a repo URL from git.

## Why is this change being made? 

To fix the error that I recently came across while trying to test my
goreleaser configuration:

```
% goreleaser check
  • checking                                 path=
  ⨯ configuration is invalid                 error=invalid scm url: .
  ⨯ .goreleaser.yml                                  error=configuration is invalid: invalid scm url: .
  ⨯ command failed                                   error=1 out of 1 configuration file(s) have issues
```

This change happened while on a branch doing some development. As part
of that development I needed to test a change to my goreleaser config.

My git config at the time looked like (repo obfuscated):

```
% cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@gitlab.com:some/repo
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main
[branch "release_fixes"]
        remote = .
        merge = refs/heads/main
```

It is fairly common for git to add remotes with a `.` when branch
tracking is enabled.

While, in general, there aren't many use cases that require a user to
need to release from a non-primary branch, there are cases where the
user may want to test their configuration with `goreleaser check` and
the error of `invalid scm url: .` isn't very helpful.

---------

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2024-01-29 10:31:19 -03:00

130 lines
4.1 KiB
Go

package git_test
import (
"context"
"strings"
"testing"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/require"
)
func TestNotARepo(t *testing.T) {
testlib.Mktmp(t)
_, err := git.ExtractRepoFromConfig(context.Background())
require.EqualError(t, err, `current folder is not a git repository`)
}
func TestNoRemote(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
_, err := git.ExtractRepoFromConfig(context.Background())
require.EqualError(t, err, `no remote configured to list refs from`)
}
func TestRelativeRemote(t *testing.T) {
ctx := context.Background()
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAddWithName(t, "upstream", "https://github.com/goreleaser/goreleaser.git")
_, err := git.Run(ctx, "pull", "upstream", "main")
require.NoError(t, err)
_, err = git.Run(ctx, "branch", "--set-upstream-to", "upstream/main")
require.NoError(t, err)
_, err = git.Run(ctx, "checkout", "--track", "-b", "relative_branch")
require.NoError(t, err)
gitCfg, err := git.Run(ctx, "config", "--local", "--list")
require.NoError(t, err)
require.True(t, strings.Contains(gitCfg, "branch.relative_branch.remote=."))
repo, err := git.ExtractRepoFromConfig(ctx)
require.NoError(t, err)
require.Equal(t, "goreleaser/goreleaser", repo.String())
}
func TestRepoName(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
repo, err := git.ExtractRepoFromConfig(context.Background())
require.NoError(t, err)
require.Equal(t, "goreleaser/goreleaser", repo.String())
}
func TestRepoNameWithDifferentRemote(t *testing.T) {
ctx := context.Background()
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAddWithName(t, "upstream", "https://github.com/goreleaser/goreleaser.git")
_, err := git.Run(ctx, "pull", "upstream", "main")
require.NoError(t, err)
_, err = git.Run(ctx, "branch", "--set-upstream-to", "upstream/main")
require.NoError(t, err)
repo, err := git.ExtractRepoFromConfig(ctx)
require.NoError(t, err)
require.Equal(t, "goreleaser/goreleaser", repo.String())
}
func TestExtractRepoFromURL(t *testing.T) {
// valid urls
for _, url := range []string{
"git@github.com:goreleaser/goreleaser.git",
"git@custom:goreleaser/goreleaser.git",
"https://foo@github.com/goreleaser/goreleaser",
"https://github.com/goreleaser/goreleaser.git",
"https://something.with.port:8080/goreleaser/goreleaser.git",
"https://github.enterprise.com/goreleaser/goreleaser.git",
"https://gitlab-ci-token:SOME_TOKEN@gitlab.yourcompany.com/goreleaser/goreleaser.git",
} {
t.Run(url, func(t *testing.T) {
repo, err := git.ExtractRepoFromURL(url)
require.NoError(t, err)
require.Equal(t, "goreleaser/goreleaser", repo.String())
require.NoError(t, repo.CheckSCM())
require.Equal(t, url, repo.RawURL)
})
}
// nested urls
for _, url := range []string{
"git@custom:group/nested/goreleaser/goreleaser.git",
"https://gitlab.mycompany.com/group/nested/goreleaser/goreleaser.git",
"https://gitlab-ci-token:SOME_TOKEN@gitlab.yourcompany.com/group/nested/goreleaser/goreleaser.git",
} {
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())
require.NoError(t, repo.CheckSCM())
require.Equal(t, url, repo.RawURL)
})
}
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.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/",
} {
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())
require.Error(t, repo.CheckSCM())
require.Equal(t, url, repo.RawURL)
})
}
}