Skip to content

Commit 77cb69f

Browse files
committed
test: fix tests
1 parent e09d745 commit 77cb69f

1 file changed

Lines changed: 17 additions & 35 deletions

File tree

internal/ctxlog/ctxlog_test.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"context"
99
"log/slog"
1010
"os"
11-
"path/filepath"
1211
"strings"
1312
"testing"
13+
14+
"github.com/stretchr/testify/assert"
1415
)
1516

1617
func TestNew(t *testing.T) {
@@ -177,19 +178,6 @@ func TestLoggingFunctions(t *testing.T) {
177178
}
178179

179180
func TestLogLevelFromEnv(t *testing.T) {
180-
// Get the actual executable name to build the proper environment variable
181-
exec, err := os.Executable()
182-
if err != nil {
183-
t.Fatalf("Failed to get executable: %v", err)
184-
}
185-
186-
exec = filepath.Base(exec)
187-
ext := filepath.Ext(exec)
188-
189-
if ext == ".exe" {
190-
exec = exec[:len(exec)-len(ext)]
191-
}
192-
193181
// Save original environment value
194182
originalValue := os.Getenv(porchLogLevelEnvVar)
195183
defer func() {
@@ -248,9 +236,7 @@ func TestLogLevelFromEnv(t *testing.T) {
248236

249237
// Test the function
250238
level := logLevelFromEnv()
251-
if level != tt.expectedLevel {
252-
t.Errorf("logLevelFromEnv() = %v, want %v (env var %s=%s)", level, tt.expectedLevel, porchLogLevelEnvVar, tt.envValue)
253-
}
239+
assert.Equal(t, tt.expectedLevel, level, "logLevelFromEnv() should return the expected log level")
254240
})
255241
}
256242
}
@@ -268,15 +254,15 @@ func TestDefaultLogger(t *testing.T) {
268254
LevelVar.Set(slog.LevelDebug)
269255

270256
// Test basic functionality
271-
if !DefaultLogger.Enabled(context.Background(), slog.LevelInfo) {
272-
t.Error("DefaultLogger should be enabled for INFO level when LevelVar is set to DEBUG")
273-
}
257+
assert.True(t,
258+
DefaultLogger.Enabled(context.Background(),
259+
slog.LevelInfo),
260+
"DefaultLogger should be enabled for INFO",
261+
)
274262
}
275263

276264
func TestJSONLogger(t *testing.T) {
277-
if JSONLogger == nil {
278-
t.Error("JSONLogger should not be nil")
279-
}
265+
assert.NotNil(t, JSONLogger, "JSONLogger should not be nil")
280266

281267
// Save original level and restore at end
282268
originalLevel := LevelVar.Level()
@@ -286,24 +272,22 @@ func TestJSONLogger(t *testing.T) {
286272
LevelVar.Set(slog.LevelDebug)
287273

288274
// Test that JSONLogger works
289-
if !JSONLogger.Enabled(context.Background(), slog.LevelInfo) {
290-
t.Error("JSONLogger should be enabled for INFO level when LevelVar is set to DEBUG")
291-
}
275+
assert.True(
276+
t,
277+
JSONLogger.Enabled(context.Background(), slog.LevelInfo),
278+
"JSONLogger should be enabled for INFO level when LevelVar is set to DEBUG",
279+
)
292280
}
293281

294282
func TestLevelVar(t *testing.T) {
295-
if LevelVar == nil {
296-
t.Error("LevelVar should not be nil")
297-
}
283+
assert.NotNil(t, LevelVar, "LevelVar should not be nil")
298284

299285
// Test that we can get and set the level
300286
originalLevel := LevelVar.Level()
301287

302288
LevelVar.Set(slog.LevelDebug)
303289

304-
if LevelVar.Level() != slog.LevelDebug {
305-
t.Error("LevelVar.Set() should update the level")
306-
}
290+
assert.Equal(t, slog.LevelDebug, LevelVar.Level(), "LevelVar.Set() should update the level")
307291

308292
// Restore original level
309293
LevelVar.Set(originalLevel)
@@ -325,7 +309,5 @@ func TestLoggerKey(t *testing.T) {
325309
key1 := loggerKey{}
326310
key2 := loggerKey{}
327311

328-
if key1 != key2 {
329-
t.Error("loggerKey instances should be equal")
330-
}
312+
assert.Equal(t, key1, key2, "loggerKey instances should be equal")
331313
}

0 commit comments

Comments
 (0)