2020-05-10 17:03:49 +01:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-25 13:00:51 -03:00
|
|
|
"os"
|
2020-05-10 17:03:49 +01:00
|
|
|
"path/filepath"
|
2023-02-07 10:39:48 -03:00
|
|
|
"strings"
|
2020-05-10 17:03:49 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2023-08-08 22:43:09 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2023-03-02 00:01:11 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testctx"
|
2023-08-24 22:06:12 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2020-05-10 17:03:49 +01:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecute(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2020-05-10 17:03:49 +01:00
|
|
|
ProjectName: "blah",
|
2023-03-02 00:01:11 -03:00
|
|
|
Env: []string{
|
|
|
|
"TEST_A_SECRET=x",
|
|
|
|
"TEST_A_USERNAME=u2",
|
|
|
|
},
|
|
|
|
}, testctx.WithVersion("2.1.0"))
|
2020-05-10 17:03:49 +01:00
|
|
|
|
|
|
|
// Preload artifacts
|
2021-04-25 14:20:49 -03:00
|
|
|
folder := t.TempDir()
|
2020-05-10 17:03:49 +01:00
|
|
|
for _, a := range []struct {
|
|
|
|
id string
|
|
|
|
ext string
|
|
|
|
typ artifact.Type
|
|
|
|
}{
|
|
|
|
{"debpkg", "deb", artifact.LinuxPackage},
|
|
|
|
{"binary", "bin", artifact.Binary},
|
|
|
|
{"archive", "tar", artifact.UploadableArchive},
|
|
|
|
{"ubinary", "ubi", artifact.UploadableBinary},
|
|
|
|
{"checksum", "sum", artifact.Checksum},
|
|
|
|
{"signature", "sig", artifact.Signature},
|
2021-11-11 22:56:03 -03:00
|
|
|
{"signature", "pem", artifact.Certificate},
|
2020-05-10 17:03:49 +01:00
|
|
|
} {
|
2021-04-25 14:20:49 -03:00
|
|
|
file := filepath.Join(folder, "a."+a.ext)
|
|
|
|
require.NoError(t, os.WriteFile(file, []byte("lorem ipsum"), 0o644))
|
2020-05-10 17:03:49 +01:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Name: "a." + a.ext,
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Path: file,
|
|
|
|
Type: a.typ,
|
|
|
|
Extra: map[string]interface{}{
|
2021-10-16 22:46:11 -03:00
|
|
|
artifact.ExtraID: a.id,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-16 22:11:54 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Name: "foo/bar:amd64",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Path: "foo/bar:amd64",
|
|
|
|
Type: artifact.DockerImage,
|
|
|
|
Extra: map[string]interface{}{
|
2021-10-16 22:46:11 -03:00
|
|
|
artifact.ExtraID: "img",
|
2021-08-16 22:11:54 -03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Name: "foo/bar",
|
|
|
|
Path: "foo/bar",
|
|
|
|
Type: artifact.DockerManifest,
|
|
|
|
Extra: map[string]interface{}{
|
2021-10-16 22:46:11 -03:00
|
|
|
artifact.ExtraID: "mnf",
|
2021-08-16 22:11:54 -03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-06-02 15:05:54 -03:00
|
|
|
osEnv := func(ignores ...string) []string {
|
|
|
|
var result []string
|
|
|
|
outer:
|
|
|
|
for _, key := range passthroughEnvVars {
|
|
|
|
for _, ignore := range ignores {
|
|
|
|
if key == ignore {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if value := os.Getenv(key); value != "" {
|
|
|
|
result = append(result, key+"="+value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:03:49 +01:00
|
|
|
testCases := []struct {
|
2023-08-24 22:06:12 -03:00
|
|
|
name string
|
|
|
|
publishers []config.Publisher
|
|
|
|
expectErr error
|
|
|
|
expectErrAs any
|
2020-05-10 17:03:49 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"filter by IDs",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
IDs: []string{"archive"},
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
2021-06-02 15:05:54 -03:00
|
|
|
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"no filter",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
2023-08-08 22:43:09 -03:00
|
|
|
Name: "test",
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Disable: "false",
|
2020-05-10 17:03:49 +01:00
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
2021-06-02 15:05:54 -03:00
|
|
|
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2021-08-16 22:11:54 -03:00
|
|
|
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
2023-08-08 22:43:09 -03:00
|
|
|
{
|
|
|
|
"disabled",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Disable: "true",
|
|
|
|
Env: []string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
pipe.ErrSkip{},
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2023-08-08 22:43:09 -03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"disabled invalid tmpl",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Disable: "{{ .NOPE }}",
|
|
|
|
Env: []string{},
|
|
|
|
},
|
|
|
|
},
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
|
|
|
&tmpl.Error{},
|
2023-08-08 22:43:09 -03:00
|
|
|
},
|
2020-05-10 17:03:49 +01:00
|
|
|
{
|
|
|
|
"include checksum",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Checksum: true,
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
2021-06-02 15:05:54 -03:00
|
|
|
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.sum"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2021-08-16 22:11:54 -03:00
|
|
|
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"include signatures",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Signature: true,
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
2021-06-02 15:05:54 -03:00
|
|
|
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.sig"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2021-11-11 22:56:03 -03:00
|
|
|
{ExpectedArgs: []string{"a.pem"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2021-08-16 22:11:54 -03:00
|
|
|
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2021-08-16 22:11:54 -03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"docker",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
IDs: []string{"img", "mnf"},
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
|
|
|
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
2022-01-06 20:04:43 -05:00
|
|
|
{
|
|
|
|
"extra files",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
|
|
|
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.txt"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
ExtraFiles: []config.ExtraFile{
|
|
|
|
{Glob: filepath.Join("testdata", "*.txt")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2022-01-06 20:04:43 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"extra files with rename",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
|
|
|
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.ubi"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"a.tar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"b.txt"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
{ExpectedArgs: []string{"foo/bar:amd64"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
ExtraFiles: []config.ExtraFile{
|
|
|
|
{
|
|
|
|
Glob: filepath.Join("testdata", "*.txt"),
|
|
|
|
NameTemplate: "b.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2022-01-06 20:04:43 -05:00
|
|
|
},
|
2020-05-10 17:03:49 +01:00
|
|
|
{
|
|
|
|
"try dir templating",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Signature: true,
|
|
|
|
IDs: []string{"debpkg"},
|
|
|
|
Dir: "{{ dir .ArtifactPath }}",
|
|
|
|
Cmd: MockCmd + " {{ .ArtifactName }}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
2021-06-02 15:05:54 -03:00
|
|
|
{ExpectedArgs: []string{"a.deb"}, ExitCode: 0, ExpectedEnv: osEnv()},
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"check env templating",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
IDs: []string{"debpkg"},
|
|
|
|
Cmd: MockCmd,
|
|
|
|
Env: []string{
|
|
|
|
"PROJECT={{.ProjectName}}",
|
|
|
|
"ARTIFACT={{.ArtifactName}}",
|
|
|
|
"SECRET={{.Env.TEST_A_SECRET}}",
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
|
|
|
{
|
2021-06-02 15:05:54 -03:00
|
|
|
ExpectedEnv: append(
|
|
|
|
[]string{"PROJECT=blah", "ARTIFACT=a.deb", "SECRET=x"},
|
|
|
|
osEnv()...,
|
|
|
|
),
|
|
|
|
ExitCode: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2021-06-02 15:05:54 -03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"override path",
|
|
|
|
[]config.Publisher{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
IDs: []string{"debpkg"},
|
|
|
|
Cmd: MockCmd,
|
|
|
|
Env: []string{
|
|
|
|
"PATH=/something-else",
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
|
|
|
{
|
|
|
|
ExpectedEnv: append(
|
|
|
|
[]string{"PATH=/something-else"},
|
|
|
|
osEnv("PATH")...,
|
|
|
|
),
|
2020-05-10 17:03:49 +01:00
|
|
|
ExitCode: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"command error",
|
|
|
|
[]config.Publisher{
|
2023-08-08 22:43:09 -03:00
|
|
|
{
|
|
|
|
Disable: "true",
|
|
|
|
},
|
2020-05-10 17:03:49 +01:00
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
IDs: []string{"debpkg"},
|
|
|
|
Cmd: MockCmd + " {{.ArtifactName}}",
|
|
|
|
Env: []string{
|
|
|
|
MarshalMockEnv(&MockData{
|
|
|
|
AnyOf: []MockCall{
|
|
|
|
{
|
|
|
|
ExpectedArgs: []string{"a.deb"},
|
2021-07-24 21:15:40 -03:00
|
|
|
ExpectedEnv: osEnv(),
|
2020-05-10 17:03:49 +01:00
|
|
|
Stderr: "test error",
|
|
|
|
ExitCode: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// stderr is sent to output via logger
|
2021-07-24 21:32:04 -03:00
|
|
|
fmt.Errorf(`publishing: %s failed: exit status 1: test error`, MockCmd),
|
2023-08-24 22:06:12 -03:00
|
|
|
nil,
|
2020-05-10 17:03:49 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
|
|
|
|
err := Execute(ctx, tc.publishers)
|
2023-08-24 22:06:12 -03:00
|
|
|
if tc.expectErr != nil {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.HasPrefix(err.Error(), tc.expectErr.Error()), err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tc.expectErrAs != nil {
|
|
|
|
require.ErrorAs(t, err, tc.expectErrAs)
|
2020-05-10 17:03:49 +01:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 22:06:12 -03:00
|
|
|
require.NoError(t, err)
|
2020-05-10 17:03:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|