2026-09-25 00:36:44 +00:00
|
|
|
//go:build linux || darwin
|
|
|
|
|
|
|
|
|
|
package diagnostics
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-16 03:52:35 +00:00
|
|
|
"context"
|
2026-09-25 00:36:44 +00:00
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"syscall"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestTranscriptStreamDestinations(t *testing.T) {
|
|
|
|
|
fifo := filepath.Join(t.TempDir(), "transcript.pipe")
|
|
|
|
|
if err := syscall.Mkfifo(fifo, 0600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, path := range []string{os.DevNull, fifo} {
|
|
|
|
|
t.Run(filepath.Base(path), func(t *testing.T) {
|
|
|
|
|
before, err := os.Stat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
// Preparation must finish even though the pipe has no reader yet.
|
|
|
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: path})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
var reader *os.File
|
|
|
|
|
if path == fifo {
|
|
|
|
|
// Keep both ends open in the test so publication and reading can be
|
|
|
|
|
// sequential; the small payload fits in the pipe buffer.
|
|
|
|
|
reader, err = os.OpenFile(path, os.O_RDWR|syscall.O_NONBLOCK, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer reader.Close()
|
|
|
|
|
}
|
|
|
|
|
payload := []byte("transcript")
|
2026-11-16 03:52:35 +00:00
|
|
|
if failures := session.finalize(context.Background(), nil, payload, nil); len(failures) != 0 {
|
2026-09-25 00:36:44 +00:00
|
|
|
t.Fatal(failures)
|
|
|
|
|
}
|
|
|
|
|
after, err := os.Stat(path)
|
|
|
|
|
if err != nil || !os.SameFile(before, after) || before.Mode() != after.Mode() {
|
|
|
|
|
t.Fatalf("stream replaced: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if reader != nil {
|
|
|
|
|
got := make([]byte, len(payload))
|
|
|
|
|
if _, err := io.ReadFull(reader, got); err != nil || string(got) != string(payload) {
|
|
|
|
|
t.Fatalf("pipe delivery: %q %v", got, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTranscriptStreamWriteFailure(t *testing.T) {
|
|
|
|
|
if _, err := os.Stat("/dev/full"); os.IsNotExist(err) {
|
|
|
|
|
t.Skip("/dev/full unavailable")
|
|
|
|
|
}
|
|
|
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: "/dev/full"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-16 03:52:35 +00:00
|
|
|
failures := session.finalize(context.Background(), nil, []byte("transcript"), nil)
|
2026-09-25 00:36:44 +00:00
|
|
|
if len(failures) != 1 || failures[0].Kind != "transcript write" {
|
|
|
|
|
t.Fatalf("write failure lost: %+v", failures)
|
|
|
|
|
}
|
|
|
|
|
}
|