kubo/test/cli/harness/buffer.go
Gus Eggert 579175f81d feat: add basic CLI tests using Go Test
This is intended as a replacement for sharness. These are vanilla Go
tests which can be run in your IDE for quick iteration on end-to-end
CLI tests.

This also removes IPTB by duplicating its functionality in the test
harness. This isn't a big deal...IPTB's complexity is mostly around
the fact that its state needs to be saved to disk in between `iptb`
command invocations, and that it uses Go plugins to inject
functionality, neither of which are relevant here.

If we merge this, we'll have to live with bifurcated tests for a while
until they are all migrated. I'd recommend we self-enforce a rule
that, if we need to touch a sharness test, we migrate it and one more
test over to Go tests first. Then eventually we will have migrated
everything.
2022-12-12 09:43:09 -05:00

46 lines
800 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, with leading and trailing whitespace removed.
func (b *Buffer) Trimmed() string {
b.m.Lock()
defer b.m.Unlock()
return strings.TrimSpace(b.b.String())
}
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())
}