53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
|
|
package logger
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestInit(t *testing.T) {
|
||
|
|
// Create temp directory
|
||
|
|
tmpDir := t.TempDir()
|
||
|
|
oldHome := os.Getenv("HOME")
|
||
|
|
os.Setenv("HOME", tmpDir)
|
||
|
|
defer os.Setenv("HOME", oldHome)
|
||
|
|
|
||
|
|
err := Init()
|
||
|
|
if err != nil {
|
||
|
|
t.Errorf("Init() unexpected error: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Check log file was created
|
||
|
|
logFile := filepath.Join(tmpDir, ".config", "grokkit", "grokkit.log")
|
||
|
|
if _, err := os.Stat(logFile); os.IsNotExist(err) {
|
||
|
|
t.Errorf("Log file not created at %s", logFile)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLogging(t *testing.T) {
|
||
|
|
tmpDir := t.TempDir()
|
||
|
|
oldHome := os.Getenv("HOME")
|
||
|
|
os.Setenv("HOME", tmpDir)
|
||
|
|
defer os.Setenv("HOME", oldHome)
|
||
|
|
|
||
|
|
if err := Init(); err != nil {
|
||
|
|
t.Fatalf("Init() failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// These should not panic
|
||
|
|
Info("test info message")
|
||
|
|
Error("test error message")
|
||
|
|
Debug("test debug message")
|
||
|
|
|
||
|
|
// Verify log file has content
|
||
|
|
logFile := filepath.Join(tmpDir, ".config", "grokkit", "grokkit.log")
|
||
|
|
content, err := os.ReadFile(logFile)
|
||
|
|
if err != nil {
|
||
|
|
t.Errorf("Failed to read log file: %v", err)
|
||
|
|
}
|
||
|
|
if len(content) == 0 {
|
||
|
|
t.Errorf("Log file is empty")
|
||
|
|
}
|
||
|
|
}
|