2021-10-07 15:01:31 +02:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-03-02 00:01:11 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testctx"
|
2023-01-07 17:20:11 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
2021-10-07 15:01:31 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStringer(t *testing.T) {
|
|
|
|
require.Equal(t, Pipe{}.String(), "telegram")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefault(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.New()
|
2021-10-07 15:01:31 +02:00
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
require.Equal(t, ctx.Config.Announce.Telegram.MessageTemplate, defaultMessageTemplate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnnounceInvalidTemplate(t *testing.T) {
|
2023-01-07 17:20:11 -03:00
|
|
|
t.Run("message", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2023-01-07 17:20:11 -03:00
|
|
|
Announce: config.Announce{
|
|
|
|
Telegram: config.Telegram{
|
|
|
|
MessageTemplate: "{{ .Foo }",
|
|
|
|
},
|
2021-10-07 15:01:31 +02:00
|
|
|
},
|
2023-01-07 17:20:11 -03:00
|
|
|
})
|
|
|
|
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
|
|
|
|
})
|
|
|
|
t.Run("chatid", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2023-01-07 17:20:11 -03:00
|
|
|
Announce: config.Announce{
|
|
|
|
Telegram: config.Telegram{
|
|
|
|
MessageTemplate: "test",
|
|
|
|
ChatID: "{{ .Foo }",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
testlib.RequireTemplateError(t, Pipe{}.Announce(ctx))
|
|
|
|
})
|
|
|
|
t.Run("chatid not int", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2023-01-07 17:20:11 -03:00
|
|
|
Env: []string{"CHAT_ID=test"},
|
|
|
|
Announce: config.Announce{
|
|
|
|
Telegram: config.Telegram{
|
|
|
|
MessageTemplate: "test",
|
|
|
|
ChatID: "{{ .Env.CHAT_ID }}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.EqualError(t, Pipe{}.Announce(ctx), "telegram: strconv.ParseInt: parsing \"test\": invalid syntax")
|
2021-10-07 15:01:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnnounceMissingEnv(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2023-01-07 17:20:11 -03:00
|
|
|
Env: []string{"CHAT_ID=10"},
|
2021-10-07 15:01:31 +02:00
|
|
|
Announce: config.Announce{
|
2023-01-07 17:20:11 -03:00
|
|
|
Telegram: config.Telegram{
|
|
|
|
ChatID: "{{ .Env.CHAT_ID }}",
|
|
|
|
},
|
2021-10-07 15:01:31 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, Pipe{}.Default(ctx))
|
2022-12-28 12:24:21 -03:00
|
|
|
require.EqualError(t, Pipe{}.Announce(ctx), `telegram: env: environment variable "TELEGRAM_TOKEN" should not be empty`)
|
2021-10-07 15:01:31 +02: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-10-07 15:01:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("dont skip", func(t *testing.T) {
|
2023-03-02 00:01:11 -03:00
|
|
|
ctx := testctx.NewWithCfg(config.Project{
|
2021-10-07 15:01:31 +02:00
|
|
|
Announce: config.Announce{
|
|
|
|
Telegram: config.Telegram{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.False(t, Pipe{}.Skip(ctx))
|
|
|
|
})
|
|
|
|
}
|