feat: add portable directory safeguards
This commit is contained in:
@@ -2,6 +2,8 @@ package logging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -20,3 +22,19 @@ func TestNewWritesStructuredLog(t *testing.T) {
|
||||
func TestNewNilWriterDoesNotPanic(t *testing.T) {
|
||||
New(nil).Info("discarded")
|
||||
}
|
||||
|
||||
func TestOpenFileWritesInsideConfiguredDirectory(t *testing.T) {
|
||||
directory := filepath.Join(t.TempDir(), "logs")
|
||||
logger, closer, err := OpenFile(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logger.Info("file event")
|
||||
if err := closer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
contents, err := os.ReadFile(filepath.Join(directory, "chub.log"))
|
||||
if err != nil || !strings.Contains(string(contents), `"msg":"file event"`) {
|
||||
t.Fatalf("log contents = %q, error = %v", contents, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user