mirror of
https://github.com/vee1e/gittuf.git
synced 2026-09-03 11:17:08 +00:00
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>
66 lines
1.2 KiB
Go
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)))
|
|
})
|
|
}
|
|
}
|