2018-03-28 15:31:09 +02:00
|
|
|
package before
|
|
|
|
|
|
|
|
import (
|
2021-07-23 08:09:29 -03:00
|
|
|
"os"
|
2020-12-13 10:33:08 -03:00
|
|
|
"path/filepath"
|
2018-03-28 15:31:09 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2023-09-16 17:01:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/skips"
|
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/testlib"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
2019-03-03 14:12:22 -03:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-28 15:31:09 +02:00
|
|
|
)
|
|
|
|
|
2021-07-23 08:09:29 -03:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
defer log.SetLevel(log.InfoLevel)
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2018-03-28 15:31:09 +02:00
|
|
|
func TestDescription(t *testing.T) {
|
2019-03-03 14:12:22 -03: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-03 21:37:16 -03:00
|
|
|
{},
|
|
|
|
{"go version"},
|
|
|
|
{"go version", "go list"},
|
2020-04-12 14:12:53 -03:00
|
|
|
{`bash -c "go version; echo \"lala spaces and such\""`},
|
2018-03-28 15:31:09 +02:00
|
|
|
} {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(
|
2018-03-28 15:31:09 +02:00
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: tc,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2019-03-03 14:12:22 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(ctx))
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 14:12:53 -03:00
|
|
|
func TestRunPipeInvalidCommand(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(
|
2020-07-22 09:14:56 -03:00
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{`bash -c "echo \"unterminated command\"`},
|
2020-04-12 14:12:53 -03:00
|
|
|
},
|
2020-07-22 09:14:56 -03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
require.EqualError(t, Pipe{}.Run(ctx), "invalid command line string")
|
2020-04-12 14:12:53 -03:00
|
|
|
}
|
|
|
|
|
2018-03-28 15:31:09 +02:00
|
|
|
func TestRunPipeFail(t *testing.T) {
|
2023-11-29 22:25:14 -03:00
|
|
|
for _, tc := range []string{
|
|
|
|
"go tool foobar",
|
|
|
|
"sh ./testdata/foo.sh",
|
2018-03-28 15:31:09 +02:00
|
|
|
} {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(
|
2018-03-28 15:31:09 +02:00
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
2023-11-29 22:25:14 -03:00
|
|
|
Hooks: []string{tc},
|
2018-03-28 15:31:09 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-11-29 22:25:14 -03:00
|
|
|
err := Pipe{}.Run(ctx)
|
|
|
|
require.Contains(t, err.Error(), "hook failed")
|
2018-03-28 15:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 14:12:22 -03:00
|
|
|
|
|
|
|
func TestRunWithEnv(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
f := filepath.Join(t.TempDir(), "testfile")
|
2023-03-02 00:01:11 -03:00
|
|
|
require.NoError(t, Pipe{}.Run(testctx.NewWithCfg(
|
2019-03-03 14:12:22 -03:00
|
|
|
config.Project{
|
|
|
|
Env: []string{
|
2020-12-13 10:33:08 -03:00
|
|
|
"TEST_FILE=" + f,
|
2019-03-03 14:12:22 -03:00
|
|
|
},
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{"touch {{ .Env.TEST_FILE }}"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)))
|
2020-12-13 10:33:08 -03:00
|
|
|
require.FileExists(t, f)
|
2019-03-03 14:12:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidTemplate(t *testing.T) {
|
2023-08-24 22:06:12 -03:00
|
|
|
testlib.RequireTemplateError(t, Pipe{}.Run(testctx.NewWithCfg(
|
2019-03-03 14:12:22 -03:00
|
|
|
config.Project{
|
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{"touch {{ .fasdsd }"},
|
|
|
|
},
|
|
|
|
},
|
2023-08-24 22:06:12 -03:00
|
|
|
)))
|
2019-03-03 14:12:22 -03:00
|
|
|
}
|
2021-09-18 10:21:29 -03:00
|
|
|
|
|
|
|
func TestSkip(t *testing.T) {
|
|
|
|
t.Run("skip", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
require.True(t, Pipe{}.Skip(testctx.New()))
|
2021-09-18 10:21:29 -03:00
|
|
|
})
|
|
|
|
|
2022-06-22 21:56:53 -03:00
|
|
|
t.Run("skip before", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2022-06-22 21:56:53 -03:00
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{""},
|
|
|
|
},
|
2023-09-16 17:01:20 -03:00
|
|
|
}, testctx.Skip(skips.Before))
|
2022-06-22 21:56:53 -03:00
|
|
|
require.True(t, Pipe{}.Skip(ctx))
|
|
|
|
})
|
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
t.Run("dont skip", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-09-18 10:21:29 -03:00
|
|
|
Before: config.Before{
|
|
|
|
Hooks: []string{""},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.False(t, Pipe{}.Skip(ctx))
|
|
|
|
})
|
|
|
|
}
|