1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-16 03:44:26 +00:00
arikawa/internal/testenv/testenv.go

79 lines
1.2 KiB
Go
Raw Normal View History

// +build !uintonly
package testenv
import (
"os"
"sync"
"testing"
2021-01-01 07:48:29 +00:00
"time"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/pkg/errors"
)
2021-01-01 07:48:29 +00:00
const PerseveranceTime = 50 * time.Minute
type Env struct {
BotToken string
ChannelID discord.ChannelID
VoiceChID discord.ChannelID
}
var (
globalEnv Env
globalErr error
once sync.Once
)
func Must(t *testing.T) Env {
e, err := GetEnv()
if err != nil {
2021-01-01 07:48:29 +00:00
t.Skip("integration test variables missing")
}
return e
}
func GetEnv() (Env, error) {
once.Do(getEnv)
return globalEnv, globalErr
}
func getEnv() {
var token = os.Getenv("BOT_TOKEN")
if token == "" {
globalErr = errors.New("missing $BOT_TOKEN")
return
}
var cid = os.Getenv("CHANNEL_ID")
if cid == "" {
globalErr = errors.New("missing $CHANNEL_ID")
return
}
id, err := discord.ParseSnowflake(cid)
if err != nil {
globalErr = errors.Wrap(err, "invalid $CHANNEL_ID")
return
}
var sid = os.Getenv("VOICE_ID")
if sid == "" {
globalErr = errors.New("missing $VOICE_ID")
return
}
vid, err := discord.ParseSnowflake(sid)
if err != nil {
globalErr = errors.Wrap(err, "invalid $VOICE_ID")
return
}
globalEnv = Env{
BotToken: token,
ChannelID: discord.ChannelID(id),
VoiceChID: discord.ChannelID(vid),
}
}