mirror of
https://github.com/taigrr/gopher-os
synced 2026-04-01 08:18:42 -07:00
Change early.Printf calls to kfmt.Printf
This commit is contained in:
@@ -168,10 +168,10 @@ func TestPrintfToRingBuffer(t *testing.T) {
|
||||
}()
|
||||
|
||||
exp := "hello world"
|
||||
Fprintf(&buf, exp)
|
||||
Printf(exp)
|
||||
|
||||
var buf bytes.Buffer
|
||||
SetOutputSink(buf)
|
||||
SetOutputSink(&buf)
|
||||
|
||||
if got := buf.String(); got != exp {
|
||||
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
|
||||
|
||||
@@ -3,7 +3,6 @@ package kfmt
|
||||
import (
|
||||
"gopheros/kernel"
|
||||
"gopheros/kernel/cpu"
|
||||
"gopheros/kernel/kfmt/early"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -31,12 +30,12 @@ func Panic(e interface{}) {
|
||||
err = errRuntimePanic
|
||||
}
|
||||
|
||||
early.Printf("\n-----------------------------------\n")
|
||||
Printf("\n-----------------------------------\n")
|
||||
if err != nil {
|
||||
early.Printf("[%s] unrecoverable error: %s\n", err.Module, err.Message)
|
||||
Printf("[%s] unrecoverable error: %s\n", err.Module, err.Message)
|
||||
}
|
||||
early.Printf("*** kernel panic: system halted ***")
|
||||
early.Printf("\n-----------------------------------\n")
|
||||
Printf("*** kernel panic: system halted ***")
|
||||
Printf("\n-----------------------------------\n")
|
||||
|
||||
cpuHaltFn()
|
||||
}
|
||||
|
||||
@@ -5,17 +5,18 @@ import (
|
||||
"errors"
|
||||
"gopheros/kernel"
|
||||
"gopheros/kernel/cpu"
|
||||
"gopheros/kernel/driver/video/console"
|
||||
"gopheros/kernel/hal"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestPanic(t *testing.T) {
|
||||
defer func() {
|
||||
cpuHaltFn = cpu.Halt
|
||||
SetOutputSink(nil)
|
||||
}()
|
||||
|
||||
var buf bytes.Buffer
|
||||
SetOutputSink(&buf)
|
||||
|
||||
var cpuHaltCalled bool
|
||||
cpuHaltFn = func() {
|
||||
cpuHaltCalled = true
|
||||
@@ -23,14 +24,14 @@ func TestPanic(t *testing.T) {
|
||||
|
||||
t.Run("with *kernel.Error", func(t *testing.T) {
|
||||
cpuHaltCalled = false
|
||||
fb := mockTTY()
|
||||
buf.Reset()
|
||||
err := &kernel.Error{Module: "test", Message: "panic test"}
|
||||
|
||||
Panic(err)
|
||||
|
||||
exp := "\n-----------------------------------\n[test] unrecoverable error: panic test\n*** kernel panic: system halted ***\n-----------------------------------"
|
||||
exp := "\n-----------------------------------\n[test] unrecoverable error: panic test\n*** kernel panic: system halted ***\n-----------------------------------\n"
|
||||
|
||||
if got := readTTY(fb); got != exp {
|
||||
if got := buf.String(); got != exp {
|
||||
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
|
||||
}
|
||||
|
||||
@@ -41,14 +42,14 @@ func TestPanic(t *testing.T) {
|
||||
|
||||
t.Run("with error", func(t *testing.T) {
|
||||
cpuHaltCalled = false
|
||||
fb := mockTTY()
|
||||
buf.Reset()
|
||||
err := errors.New("go error")
|
||||
|
||||
Panic(err)
|
||||
|
||||
exp := "\n-----------------------------------\n[rt] unrecoverable error: go error\n*** kernel panic: system halted ***\n-----------------------------------"
|
||||
exp := "\n-----------------------------------\n[rt] unrecoverable error: go error\n*** kernel panic: system halted ***\n-----------------------------------\n"
|
||||
|
||||
if got := readTTY(fb); got != exp {
|
||||
if got := buf.String(); got != exp {
|
||||
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
|
||||
}
|
||||
|
||||
@@ -59,14 +60,14 @@ func TestPanic(t *testing.T) {
|
||||
|
||||
t.Run("with string", func(t *testing.T) {
|
||||
cpuHaltCalled = false
|
||||
fb := mockTTY()
|
||||
buf.Reset()
|
||||
err := "string error"
|
||||
|
||||
Panic(err)
|
||||
|
||||
exp := "\n-----------------------------------\n[rt] unrecoverable error: string error\n*** kernel panic: system halted ***\n-----------------------------------"
|
||||
exp := "\n-----------------------------------\n[rt] unrecoverable error: string error\n*** kernel panic: system halted ***\n-----------------------------------\n"
|
||||
|
||||
if got := readTTY(fb); got != exp {
|
||||
if got := buf.String(); got != exp {
|
||||
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
|
||||
}
|
||||
|
||||
@@ -77,13 +78,13 @@ func TestPanic(t *testing.T) {
|
||||
|
||||
t.Run("without error", func(t *testing.T) {
|
||||
cpuHaltCalled = false
|
||||
fb := mockTTY()
|
||||
buf.Reset()
|
||||
|
||||
Panic(nil)
|
||||
|
||||
exp := "\n-----------------------------------\n*** kernel panic: system halted ***\n-----------------------------------"
|
||||
exp := "\n-----------------------------------\n*** kernel panic: system halted ***\n-----------------------------------\n"
|
||||
|
||||
if got := readTTY(fb); got != exp {
|
||||
if got := buf.String(); got != exp {
|
||||
t.Fatalf("expected to get:\n%q\ngot:\n%q", exp, got)
|
||||
}
|
||||
|
||||
@@ -92,30 +93,3 @@ func TestPanic(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func readTTY(fb []byte) string {
|
||||
var buf bytes.Buffer
|
||||
for i := 0; i < len(fb); i += 2 {
|
||||
ch := fb[i]
|
||||
if ch == 0 {
|
||||
if i+2 < len(fb) && fb[i+2] != 0 {
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
buf.WriteByte(ch)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func mockTTY() []byte {
|
||||
// Mock a tty to handle early.Printf output
|
||||
mockConsoleFb := make([]byte, 160*25)
|
||||
mockConsole := &console.Ega{}
|
||||
mockConsole.Init(80, 25, uintptr(unsafe.Pointer(&mockConsoleFb[0])))
|
||||
hal.ActiveTerminal.AttachTo(mockConsole)
|
||||
|
||||
return mockConsoleFb
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user