Skip to content

Commit

Permalink
*: Improve WSDebug logging in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Nov 4, 2023
1 parent 2c2daec commit b0a773c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
27 changes: 14 additions & 13 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strconv"
"strings"
"sync"
"testing"
"time"

Expand All @@ -14,16 +13,18 @@ import (
"github.com/diamondburned/arikawa/v3/utils/ws"
)

var doLogOnce sync.Once
func doLog(t *testing.T) {
if !testing.Verbose() {
return
}

func doLog() {
doLogOnce.Do(func() {
if testing.Verbose() {
ws.WSDebug = func(v ...interface{}) {
log.Println(append([]interface{}{"Debug:"}, v...)...)
}
}
})
prev := ws.WSDebug
t.Cleanup(func() { ws.WSDebug = prev })

ws.WSDebug = func(v ...interface{}) {
t.Helper()
t.Log(v...)
}
}

func TestURL(t *testing.T) {
Expand All @@ -45,7 +46,7 @@ func TestURL(t *testing.T) {
}

func TestInvalidToken(t *testing.T) {
doLog()
doLog(t)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestInvalidToken(t *testing.T) {
}

func TestIntegration(t *testing.T) {
doLog()
doLog(t)

config := testenv.Must(t)

Expand All @@ -108,7 +109,7 @@ func TestIntegration(t *testing.T) {
}

func TestReuseGateway(t *testing.T) {
doLog()
doLog(t)

config := testenv.Must(t)

Expand Down
23 changes: 13 additions & 10 deletions voice/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"fmt"
"log"
"math/rand"
"os"
"runtime"
"strconv"
"sync"
"testing"
"time"
Expand All @@ -24,15 +21,18 @@ import (
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
)

func TestMain(m *testing.M) {
ws.WSDebug = func(v ...interface{}) {
_, file, line, _ := runtime.Caller(1)
caller := file + ":" + strconv.Itoa(line)
log.Println(append([]interface{}{caller}, v...)...)
func doLog(t *testing.T) {
if !testing.Verbose() {
return
}

code := m.Run()
os.Exit(code)
prev := ws.WSDebug
t.Cleanup(func() { ws.WSDebug = prev })

ws.WSDebug = func(v ...interface{}) {
t.Helper()
t.Log(v...)
}
}

type testState struct {
Expand All @@ -41,6 +41,9 @@ type testState struct {
}

func testOpen(t *testing.T) *testState {
t.Helper()
doLog(t)

config := testenv.Must(t)

s := state.New("Bot " + config.BotToken)
Expand Down

0 comments on commit b0a773c

Please sign in to comment.