-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_utils_test.go
More file actions
36 lines (31 loc) · 1.03 KB
/
test_utils_test.go
File metadata and controls
36 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package gatekeeper
import (
"log"
"os"
"strings"
"testing"
)
// testLogger returns a logger that logs to the test's t.Log.
func testLogger(t *testing.T) *log.Logger {
// Using a more unique prefix to avoid potential collisions if other packages also log
// and to make it clear these logs are from gatekeeper tests.
return log.New(&testLogWriter{t}, "[GK_TEST] ", 0) // No flags for cleaner test output
}
// testLogWriter adapts testing.T to io.Writer for log.New.
type testLogWriter struct {
t *testing.T
}
// Write sends the log output to t.Log(), trimming whitespace.
func (tlw *testLogWriter) Write(p []byte) (n int, err error) {
// TrimSpace to remove leading/trailing newlines often added by log package
tlw.t.Log(strings.TrimSpace(string(p)))
return len(p), nil
}
// Helper function to get environment variables with a fallback
// Useful for tests that might depend on optional environment configurations.
func getEnv(key, fallback string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return fallback
}