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

75 lines
1.2 KiB
Go
Raw Normal View History

// +build !uintonly
package testenv
import (
"os"
2021-06-10 23:48:32 +00:00
"strconv"
"sync"
"testing"
2021-01-01 07:48:29 +00:00
"time"
2021-06-02 02:53:19 +00:00
"github.com/diamondburned/arikawa/v3/discord"
"github.com/pkg/errors"
)
2021-01-01 07:48:29 +00:00
const PerseveranceTime = 50 * time.Minute
type Env struct {
2021-06-10 23:48:32 +00:00
BotToken string
ChannelID discord.ChannelID
VoiceChID discord.ChannelID
ShardCount int // default 3
}
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() {
2021-06-10 23:48:32 +00:00
token := os.Getenv("BOT_TOKEN")
if token == "" {
globalErr = errors.New("missing $BOT_TOKEN")
return
}
2021-06-10 23:48:32 +00:00
id, err := discord.ParseSnowflake(os.Getenv("CHANNEL_ID"))
if err != nil {
globalErr = errors.Wrap(err, "invalid $CHANNEL_ID")
return
}
2021-06-10 23:48:32 +00:00
vid, err := discord.ParseSnowflake(os.Getenv("VOICE_ID"))
if err != nil {
globalErr = errors.Wrap(err, "invalid $VOICE_ID")
return
}
2021-06-10 23:48:32 +00:00
shardCount := 3
if c, err := strconv.Atoi(os.Getenv("SHARD_COUNT")); err == nil {
shardCount = c
}
globalEnv = Env{
2021-06-10 23:48:32 +00:00
BotToken: token,
ChannelID: discord.ChannelID(id),
VoiceChID: discord.ChannelID(vid),
ShardCount: shardCount,
}
}