2018-03-28 15:31:09 +02:00
|
|
|
package before
|
|
|
|
|
|
|
|
import (
|
2019-03-03 19:12:22 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-03-28 15:31:09 +02:00
|
|
|
"testing"
|
|
|
|
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2019-03-03 19:12:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-28 15:31:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDescription(t *testing.T) {
|
2019-03-03 19:12:22 +02:00
|
|
|
require.NotEmpty(t, Pipe{}.String())
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunPipe(t *testing.T) {
|
|
|
|
for _, tc := range [][]string{
|
|
|
|
nil,
|
2018-04-04 02:37:16 +02:00
|
|
|
{},
|
|
|
|
{"go version"},
|
|
|
|
{"go version", "go list"},
|
2020-04-12 19:12:53 +02:00
|
|
|
{`bash -c "go version; echo \"lala spaces and such\""`},
|
2018-03-28 15:31:09 +02:00
|
|
|
} {
|
|
|
|
ctx := context.New(
|
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: tc,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2019-03-03 19:12:22 +02:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:12:53 +02:00
|
|
|
func TestRunPipeInvalidCommand(t *testing.T) {
|
2020-07-22 14:14:56 +02:00
|
|
|
ctx := context.New(
|
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{`bash -c "echo \"unterminated command\"`},
|
2020-04-12 19:12:53 +02:00
|
|
|
},
|
2020-07-22 14:14:56 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), "invalid command line string")
|
2020-04-12 19:12:53 +02:00
|
|
|
}
|
|
|
|
|
2018-03-28 15:31:09 +02:00
|
|
|
func TestRunPipeFail(t *testing.T) {
|
2020-07-22 14:14:56 +02:00
|
|
|
for err, tc := range map[string][]string{
|
|
|
|
"hook failed: go tool foobar: exit status 2; output: go tool: no such tool \"foobar\"\n": {"go tool foobar"},
|
|
|
|
"hook failed: sh ./testdata/foo.sh: exit status 1; output: lalala\n": {"sh ./testdata/foo.sh"},
|
2018-03-28 15:31:09 +02:00
|
|
|
} {
|
|
|
|
ctx := context.New(
|
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: tc,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2020-07-22 14:14:56 +02:00
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), err)
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 19:12:22 +02:00
|
|
|
|
|
|
|
func TestRunWithEnv(t *testing.T) {
|
|
|
|
f, err := ioutil.TempFile("", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, os.Remove(f.Name()))
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
require.NoError(t, Pipe{}.Run(context.New(
|
|
|
|
config.Project{
|
|
|
|
Env: []string{
|
|
|
|
"TEST_FILE=" + f.Name(),
|
|
|
|
},
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{"touch {{ .Env.TEST_FILE }}"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)))
|
|
|
|
require.FileExists(t, f.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidTemplate(t *testing.T) {
|
|
|
|
require.EqualError(t, Pipe{}.Run(context.New(
|
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{"touch {{ .fasdsd }"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)), `template: tmpl:1: unexpected "}" in operand`)
|
|
|
|
}
|