From 33183297ddf531b27be65b259e48a0d19691e3d4 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 5 Dec 2024 10:38:23 +0800 Subject: [PATCH] qlog: simplify QLOGDIR test by using t.Setenv and t.TempDir (#4745) * qlog: simplify QLOGDIR test by using t.Setenv * use testing.T.TempDir --- qlog/qlog_dir_test.go | 46 ++++++++++--------------------------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/qlog/qlog_dir_test.go b/qlog/qlog_dir_test.go index ad8ff5579..6c0818fe9 100644 --- a/qlog/qlog_dir_test.go +++ b/qlog/qlog_dir_test.go @@ -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) }