feat: pin vantage-shared v0.5.0, add package-manager output tail buffer

This commit is contained in:
2026-09-15 07:33:59 +00:00
parent b777ffcf58
commit 3aa24bb938
4 changed files with 71 additions and 3 deletions
+34
View File
@@ -0,0 +1,34 @@
package updates
import "sync"
// outputTailMax bounds the package-manager output carried back in a
// PatchResult. The end of the output is where apt and dnf say what failed, so
// the newest bytes are the ones kept.
const outputTailMax = 64 << 10
// tailBuffer is an io.Writer that retains only the last max bytes written.
// Stdout and stderr are both pointed at one, so it is safe for concurrent use.
type tailBuffer struct {
mu sync.Mutex
max int
buf []byte
}
func newTailBuffer(max int) *tailBuffer { return &tailBuffer{max: max} }
func (t *tailBuffer) Write(p []byte) (int, error) {
t.mu.Lock()
defer t.mu.Unlock()
t.buf = append(t.buf, p...)
if over := len(t.buf) - t.max; over > 0 {
t.buf = append([]byte(nil), t.buf[over:]...)
}
return len(p), nil
}
func (t *tailBuffer) String() string {
t.mu.Lock()
defer t.mu.Unlock()
return string(t.buf)
}
+34
View File
@@ -0,0 +1,34 @@
package updates
import (
"strings"
"testing"
)
func TestTailBufferKeepsNewestBytes(t *testing.T) {
b := newTailBuffer(10)
_, _ = b.Write([]byte("0123456789"))
_, _ = b.Write([]byte("abcde"))
if got := b.String(); got != "56789abcde" {
t.Fatalf("got %q, want %q", got, "56789abcde")
}
}
func TestTailBufferSingleWriteLargerThanMax(t *testing.T) {
b := newTailBuffer(4)
n, err := b.Write([]byte("abcdefgh"))
if err != nil || n != 8 {
t.Fatalf("Write must report the full length consumed, got %d, %v", n, err)
}
if got := b.String(); got != "efgh" {
t.Fatalf("got %q", got)
}
}
func TestTailBufferDefaultCap(t *testing.T) {
b := newTailBuffer(outputTailMax)
_, _ = b.Write([]byte(strings.Repeat("x", outputTailMax+100)))
if len(b.String()) != outputTailMax {
t.Fatalf("len %d, want %d", len(b.String()), outputTailMax)
}
}