gittuf/internal/display/display_test.go
Aditya Sirish c3148721b9
rsl: Fix gittuf rsl log buffering
This commit fixes how gittuf rsl log loads RSL entries to print.
Earlier, all entries were loaded into memory first and then printed.
This commit switches over to a buffered write approach, meaning the user
starts seeing output right away.

Signed-off-by: Aditya Sirish <aditya@saky.in>
Co-authored-by: Hao Tran <haoanhtran7@gmail.com>
2025-02-01 17:51:37 -05:00

66 lines
1.2 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package display
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func getPagerTestCat() string {
return "cat"
}
func getPagerTestNone() string {
return ""
}
func TestNewDisplayWriter(t *testing.T) {
tests := map[string]struct {
contents []byte
page bool
}{
"without paging": {
contents: []byte("Hello, world!"),
page: false,
},
"with paging": {
contents: []byte("Hello, world!"),
page: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if test.page {
getPager = getPagerTestCat
} else {
getPager = getPagerTestNone
}
output := &bytes.Buffer{}
writer := NewDisplayWriter(output)
_, err := writer.Write(test.contents)
if err != nil {
t.Fatal(err)
}
if err := writer.Close(); err != nil {
t.Fatal(err)
}
gotOutput := output.String()
if runtime.GOOS == "windows" {
gotOutput = strings.TrimSpace(gotOutput)
}
assert.Equal(t, string(test.contents), gotOutput, fmt.Sprintf("unexpected result in test '%s', got '%s', want '%s'", name, gotOutput, string(test.contents)))
})
}
}