Files
iris-context/tests/crash_test.go
T
VERSE 3d76a3ee88 feat: toml config support (#9)
## What's new
- TOML config support 
- add config init command to justfile
https://github.com/versenilvis/IRIS/pull/9/commits/a5bb6c23d74ca73385b3590d2eb275ce6429906b
- add copy binary file to local bin on reload
https://github.com/versenilvis/IRIS/pull/9/commits/4da3acdbc58c28f20fa0021d15fec82823ba392b
- init config on setup
https://github.com/versenilvis/IRIS/pull/9/commits/fb48b6bb832f38e24fab9211d92858bab9a58a07

## Fix
- add lsof to support macos because /proc filesystem is Linux-specific
and does not exist on macOS 0439d829573864fcf902fc4127f7d45384bfab01
- git push doesnt suggest current branch, fix
https://github.com/versenilvis/IRIS/pull/9/commits/b498532b401b764a4a1a2c70b3cb44b7caf72320
- use timeout context for lsof command in reload handler
https://github.com/versenilvis/IRIS/pull/9/commits/1a2baee951e6538a729da12aedfe53960e51102d
2026-05-30 22:05:06 +07:00

104 lines
2.7 KiB
Go

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")
origCache := os.Getenv("XDG_CACHE_HOME")
defer func() {
_ = os.Setenv("HOME", origHome)
_ = os.Setenv("XDG_CACHE_HOME", origCache)
}()
_ = os.Setenv("HOME", tmpDir)
_ = os.Setenv("XDG_CACHE_HOME", filepath.Join(tmpDir, ".cache"))
testErr := "test panic message"
root.WriteCrashLog(testErr)
dir := filepath.Join(tmpDir, ".cache", "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")
origCache := os.Getenv("XDG_CACHE_HOME")
defer func() {
_ = os.Setenv("HOME", origHome)
_ = os.Setenv("XDG_CACHE_HOME", origCache)
}()
_ = os.Setenv("HOME", tmpDir)
_ = os.Setenv("XDG_CACHE_HOME", filepath.Join(tmpDir, ".cache"))
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{})
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, ".cache", "iris", "crashes")
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Errorf("expected crashes directory to be deleted, but it exists")
}
}