1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/client/git_test.go
Carlos Alexandro Becker 5bf42b726f
feat: publish taps, krews and scoops to regular git repositories (#3961)
basically redoing #3559 as it got impossible to merge with the many
changes since it was open (which is totally my fault for not merging it
earlier).

Anyhow, still a WIP, going also doing some other related improvements in
the way.

cc/ @graytonio

closes #3559
closes #3525

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Grayton Ward <graytonio.ward@gmail.com>
2023-04-30 10:18:13 -03:00

249 lines
5.6 KiB
Go

package client
import (
"os"
"strings"
"testing"
"github.com/charmbracelet/keygen"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/stretchr/testify/require"
)
func TestGitClient(t *testing.T) {
cli := NewGitUploadClient("master")
author := config.CommitAuthor{
Name: "Foo",
Email: "foo@bar.com",
}
t.Run("full", func(t *testing.T) {
url := testlib.GitMakeBareRepository(t)
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: url,
PrivateKey: testlib.MakeNewSSHKey(t, keygen.Ed25519, ""),
Name: "test1",
}
require.NoError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte("fake content"),
"fake.txt",
"hey test",
))
require.NoError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte("fake content 2"),
"fake.txt",
"hey test 2",
))
require.Equal(t, "fake content 2", string(testlib.CatFileFromBareRepository(t, url, "fake.txt")))
})
t.Run("no repo name", func(t *testing.T) {
url := testlib.GitMakeBareRepository(t)
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: url,
PrivateKey: testlib.MakeNewSSHKey(t, keygen.Ed25519, ""),
}
require.NoError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte("fake content"),
"fake.txt",
"hey test",
))
require.NoError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte("fake content 2"),
"fake.txt",
"hey test 2",
))
require.Equal(t, "fake content 2", string(testlib.CatFileFromBareRepository(t, url, "fake.txt")))
})
t.Run("bad url", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: "{{ .Nope }}",
}
testlib.RequireTemplateError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte{},
"filename",
"msg",
))
})
t.Run("clone fail", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: "git@github.com:nope/nopenopenopenope",
PrivateKey: testlib.MakeNewSSHKey(t, keygen.Ed25519, ""),
}
err := cli.CreateFile(
ctx,
author,
repo,
[]byte{},
"filename",
"msg",
)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to clone")
})
t.Run("bad ssh cmd", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: testlib.GitMakeBareRepository(t),
PrivateKey: testlib.MakeNewSSHKey(t, keygen.Ed25519, ""),
GitSSHCommand: "{{.Foo}}",
}
testlib.RequireTemplateError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte{},
"filename",
"msg",
))
})
t.Run("empty url", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{}
require.EqualError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte{},
"filename",
"msg",
), "url is empty")
})
t.Run("bad ssh cmd", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: testlib.GitMakeBareRepository(t),
PrivateKey: "{{.Foo}}",
}
testlib.RequireTemplateError(t, cli.CreateFile(
ctx,
author,
repo,
[]byte{},
"filename",
"msg",
))
})
t.Run("bad key path", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Dist: t.TempDir(),
})
repo := Repo{
GitURL: testlib.GitMakeBareRepository(t),
PrivateKey: "./nope",
}
require.Error(t, cli.CreateFile(
ctx,
author,
repo,
[]byte{},
"filename",
"msg",
))
})
}
func TestKeyPath(t *testing.T) {
t.Run("with valid path", func(t *testing.T) {
path := testlib.MakeNewSSHKey(t, keygen.Ed25519, "")
result, err := keyPath(path)
require.NoError(t, err)
require.Equal(t, path, result)
})
t.Run("with invalid path", func(t *testing.T) {
result, err := keyPath("testdata/nope")
require.ErrorIs(t, err, os.ErrNotExist)
require.Equal(t, "", result)
})
t.Run("with password protected key path", func(t *testing.T) {
path := testlib.MakeNewSSHKey(t, keygen.Ed25519, "pwd")
bts, err := os.ReadFile(path)
require.NoError(t, err)
result, err := keyPath(string(bts))
require.EqualError(t, err, "git: key is password-protected")
require.Empty(t, result)
})
t.Run("with key", func(t *testing.T) {
for _, algo := range []keygen.KeyType{keygen.Ed25519, keygen.RSA} {
t.Run(string(algo), func(t *testing.T) {
path := testlib.MakeNewSSHKey(t, algo, "")
bts, err := os.ReadFile(path)
require.NoError(t, err)
result, err := keyPath(string(bts))
require.NoError(t, err)
resultbts, err := os.ReadFile(result)
require.NoError(t, err)
require.Equal(t, string(bts), string(resultbts))
})
}
})
t.Run("empty", func(t *testing.T) {
result, err := keyPath("")
require.EqualError(t, err, `private_key is empty`)
require.Equal(t, "", result)
})
t.Run("with invalid EOF", func(t *testing.T) {
path := testlib.MakeNewSSHKey(t, keygen.Ed25519, "")
bts, err := os.ReadFile(path)
require.NoError(t, err)
result, err := keyPath(strings.TrimSpace(string(bts)))
require.NoError(t, err)
resultbts, err := os.ReadFile(result)
require.NoError(t, err)
require.Equal(t, string(bts), string(resultbts))
})
}
func TestRepoFromURL(t *testing.T) {
for k, v := range map[string]string{
"goreleaser": "git@github.com:goreleaser/goreleaser.git",
"nfpm": "https://github.com/goreleaser/nfpm",
"test": "https://myserver.git/foo/test.git",
} {
t.Run(k, func(t *testing.T) {
require.Equal(t, k, nameFromURL(v))
})
}
}