mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-22 02:47:48 +08:00
The test trims all whitespace bytes from the output of 'ipfs cat' but if the random bytes end in a whitespace char then it trims that too, resulting in random test failure. Instead this updates the test harness to only trim a single trailing newline char, so that it doesn't end up chomping legitimate output.
54 lines
933 B
Go
54 lines
933 B
Go
package harness
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/ipfs/kubo/test/cli/testutils"
|
|
)
|
|
|
|
// Buffer is a thread-safe byte buffer.
|
|
type Buffer struct {
|
|
b strings.Builder
|
|
m sync.Mutex
|
|
}
|
|
|
|
func (b *Buffer) Write(p []byte) (n int, err error) {
|
|
b.m.Lock()
|
|
defer b.m.Unlock()
|
|
return b.b.Write(p)
|
|
}
|
|
|
|
func (b *Buffer) String() string {
|
|
b.m.Lock()
|
|
defer b.m.Unlock()
|
|
return b.b.String()
|
|
}
|
|
|
|
// Trimmed returns the bytes as a string, but with the trailing newline removed.
|
|
// This only removes a single trailing newline, not all whitespace.
|
|
func (b *Buffer) Trimmed() string {
|
|
b.m.Lock()
|
|
defer b.m.Unlock()
|
|
s := b.b.String()
|
|
if len(s) == 0 {
|
|
return s
|
|
}
|
|
if s[len(s)-1] == '\n' {
|
|
return s[:len(s)-1]
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (b *Buffer) Bytes() []byte {
|
|
b.m.Lock()
|
|
defer b.m.Unlock()
|
|
return []byte(b.b.String())
|
|
}
|
|
|
|
func (b *Buffer) Lines() []string {
|
|
b.m.Lock()
|
|
defer b.m.Unlock()
|
|
return testutils.SplitLines(b.b.String())
|
|
}
|