forked from quic-go/quic-go
qlog: simplify QLOGDIR test by using t.Setenv and t.TempDir (#4745)
* qlog: simplify QLOGDIR test by using t.Setenv * use testing.T.TempDir
This commit is contained in:
@@ -3,47 +3,25 @@ package qlog
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/quic-go/quic-go/internal/protocol"
|
||||
"github.com/quic-go/quic-go/logging"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
originalQlogDirValue string
|
||||
tempTestDirPath string
|
||||
ctx = context.Background()
|
||||
perspective = logging.PerspectiveClient
|
||||
)
|
||||
|
||||
func setup(t *testing.T) {
|
||||
originalQlogDirValue = os.Getenv("QLOGDIR")
|
||||
var err error
|
||||
tempTestDirPath, err = os.MkdirTemp("", "temp_test_dir")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func cleanup(t *testing.T) {
|
||||
require.NoError(t, os.Setenv("QLOGDIR", originalQlogDirValue))
|
||||
require.NoError(t, os.RemoveAll(tempTestDirPath))
|
||||
}
|
||||
|
||||
func TestEnvironmentVariableIsSet(t *testing.T) {
|
||||
setup(t)
|
||||
defer cleanup(t)
|
||||
func TestQLOGDIRSet(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
connID, _ := protocol.GenerateConnectionIDForInitial()
|
||||
qlogDir := path.Join(tempTestDirPath, "qlogs")
|
||||
err := os.Setenv("QLOGDIR", qlogDir)
|
||||
require.NoError(t, err)
|
||||
qlogDir := filepath.Join(tmpDir, "qlogs")
|
||||
t.Setenv("QLOGDIR", qlogDir)
|
||||
|
||||
tracer := DefaultConnectionTracer(ctx, perspective, connID)
|
||||
tracer := DefaultConnectionTracer(context.Background(), protocol.PerspectiveClient, connID)
|
||||
require.NotNil(t, tracer)
|
||||
tracer.Close()
|
||||
|
||||
_, err = os.Stat(qlogDir)
|
||||
_, err := os.Stat(qlogDir)
|
||||
qlogDirCreated := !os.IsNotExist(err)
|
||||
require.True(t, qlogDirCreated)
|
||||
|
||||
@@ -52,14 +30,10 @@ func TestEnvironmentVariableIsSet(t *testing.T) {
|
||||
require.Len(t, childs, 1)
|
||||
}
|
||||
|
||||
func TestEnvironmentVariableIsNotSet(t *testing.T) {
|
||||
setup(t)
|
||||
defer cleanup(t)
|
||||
|
||||
func TestQLOGDIRNotSet(t *testing.T) {
|
||||
connID, _ := protocol.GenerateConnectionIDForInitial()
|
||||
err := os.Setenv("QLOGDIR", "")
|
||||
require.NoError(t, err)
|
||||
t.Setenv("QLOGDIR", "")
|
||||
|
||||
tracer := DefaultConnectionTracer(ctx, perspective, connID)
|
||||
tracer := DefaultConnectionTracer(context.Background(), protocol.PerspectiveClient, connID)
|
||||
require.Nil(t, tracer)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user