Feat: crash log (#7)

Merged #6
- crash log (db0e1be9a02f70927124f9f3563df5e9dee3a764)
- perf(watchdog): limit buffer size and optimize crash substring search (https://github.com/versenilvis/IRIS/pull/7/commits/7725434fedf8b32929dc0d71e1256fda13cfdfca)
- refactor(crash): use dynamic buffer for stack trace capture (https://github.com/versenilvis/IRIS/pull/7/commits/07809a4330a1236471e8dae0e83afd4323a4c83a)
This commit is contained in:
VERSE
2026-05-23 23:32:36 +07:00
committed by GitHub
parent d06526cf21
commit bf67e50ea6
6 changed files with 438 additions and 11 deletions
+100
View File
@@ -0,0 +1,100 @@
package tests
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/versenilvis/iris/root"
)
func TestWriteCrashLog(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "iris-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
origHome := os.Getenv("HOME")
defer func() {
_ = os.Setenv("HOME", origHome)
}()
_ = os.Setenv("HOME", tmpDir)
testErr := "test panic message"
root.WriteCrashLog(testErr)
dir := filepath.Join(tmpDir, ".iris", "crashes")
files, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("failed to read crashes dir: %v", err)
}
if len(files) != 1 {
t.Fatalf("expected exactly 1 crash log file, got %d", len(files))
}
logPath := filepath.Join(dir, files[0].Name())
data, err := os.ReadFile(logPath)
if err != nil {
t.Fatalf("failed to read crash log: %v", err)
}
content := string(data)
if !strings.Contains(content, "=== IRIS CRASH ") {
t.Errorf("expected header in log, got: %s", content)
}
if !strings.Contains(content, "panic: test panic message") {
t.Errorf("expected panic message in log, got: %s", content)
}
if !strings.Contains(content, "version:") {
t.Errorf("expected version in log, got: %s", content)
}
}
func TestCrashLogCommand(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "iris-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
origHome := os.Getenv("HOME")
defer func() {
_ = os.Setenv("HOME", origHome)
}()
_ = os.Setenv("HOME", tmpDir)
var buf bytes.Buffer
root.CrashCmd.SetOut(&buf)
root.CrashCmd.SetArgs([]string{})
root.ClearLog = false
root.CrashCmd.Run(root.CrashCmd, []string{})
if !strings.Contains(buf.String(), "no crash log found") {
t.Errorf("expected 'no crash log found', got: %q", buf.String())
}
root.WriteCrashLog("mock error")
buf.Reset()
root.CrashCmd.Run(root.CrashCmd, []string{})
// expect the output to contain crash_ and .log, which is the path of the new crash log file
if !strings.Contains(buf.String(), "crash_") || !strings.Contains(buf.String(), ".log") {
t.Errorf("expected crash log path, got: %q", buf.String())
}
buf.Reset()
root.ClearLog = true
root.CrashCmd.Run(root.CrashCmd, []string{})
if !strings.Contains(buf.String(), "crash log cleared") {
t.Errorf("expected 'crash log cleared', got: %q", buf.String())
}
dir := filepath.Join(tmpDir, ".iris", "crashes")
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Errorf("expected crashes directory to be deleted, but it exists")
}
}