Skip to content

Commit

Permalink
migrated to gommons probed writer
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n committed Sep 23, 2023
1 parent 5d732f2 commit 7f5cad7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 57 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/fatih/color v1.15.0
github.com/kopoli/go-terminal-size v0.0.0-20170219200355-5c97524c8b54
github.com/mattn/go-isatty v0.0.19
github.com/sha1n/gommons v0.0.14
github.com/sha1n/gommons v0.0.17
github.com/stretchr/testify v1.8.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sha1n/gommons v0.0.14 h1:LsZeG85USg/vdOdUQonwRVl5kLe3Ryv0pbqn9r48yiU=
github.com/sha1n/gommons v0.0.14/go.mod h1:Qf66Mv+Lrocn720WwqivcpbhVSyQA4u/N/mPo3F3w00=
github.com/sha1n/gommons v0.0.17 h1:RLNOwIeFN3ne2cseYXMZ0Js0hzSkh33xINFtbDYpSVw=
github.com/sha1n/gommons v0.0.17/go.mod h1:Qf66Mv+Lrocn720WwqivcpbhVSyQA4u/N/mPo3F3w00=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
27 changes: 14 additions & 13 deletions spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/sha1n/gommons/pkg/io"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,30 +17,30 @@ const (
)

func TestSpinnerCharSequence(t *testing.T) {
emulatedStdout := NewThreadSafeBuffer()
probedWriter := io.NewUnlimitedProbedWriter(new(bytes.Buffer))

spinner := NewSpinner(emulatedStdout, "", interval, DefaultSpinnerFormatter())
spinner := NewSpinner(probedWriter, "", interval, DefaultSpinnerFormatter())
cancel, err := spinner.Start()
defer cancel()

assert.NoError(t, err)
assert.NotNil(t, cancel)

assertSpinnerCharSequence(t, emulatedStdout)
assertSpinnerCharSequence(t, probedWriter)
}

func TestSpinnerCancellation(t *testing.T) {
emulatedStdout := NewThreadSafeBuffer()
probedWriter := io.NewUnlimitedProbedWriter(new(bytes.Buffer))

spin := NewSpinner(emulatedStdout, "", interval, DefaultSpinnerFormatter())
spin := NewSpinner(probedWriter, "", interval, DefaultSpinnerFormatter())
cancel, err := spin.Start()

assert.NoError(t, err)
assert.NotNil(t, cancel)
assertSpinnerCharSequence(t, emulatedStdout)
assertSpinnerCharSequence(t, probedWriter)

cancel()
assertStoppedEventually(t, emulatedStdout, spin.(*spinner))
assertStoppedEventually(t, probedWriter, spin.(*spinner))
}

func TestSpinnerStartAlreadyRunning(t *testing.T) {
Expand Down Expand Up @@ -135,7 +136,7 @@ func bufferContains(outBuffer *bytes.Buffer, expected string) func() bool {
}
}

func assertStoppedEventually(t *testing.T, outBuffer *ThreadSafeBufferWriter, spinner *spinner) {
func assertStoppedEventually(t *testing.T, probedWriter *io.ProbedWriter, spinner *spinner) {
assert.Eventually(
t,
func() bool { return !spinner.isActiveSafe() },
Expand All @@ -147,24 +148,24 @@ func assertStoppedEventually(t *testing.T, outBuffer *ThreadSafeBufferWriter, sp
assert.Eventually(
t,
func() bool {
outBuffer.Reset()
probedWriter.Reset()
time.Sleep(spinner.interval * 2)
return outBuffer.Len() == 0
return len(probedWriter.Bytes()) == 0

},
timeout,
spinner.interval,
"expected no more output from spinner",
)
}

func assertSpinnerCharSequence(t *testing.T, outBuffer *ThreadSafeBufferWriter) {
func assertSpinnerCharSequence(t *testing.T, probedWriter *io.ProbedWriter) {
charSeq := DefaultSpinnerCharSeq()
expectedCharSequence := strings.Join(charSeq, "")
var read string = ""

for {
read = stripControlCharacters(outBuffer.String())
read = stripControlCharacters(probedWriter.String())
if len(read) >= len(expectedCharSequence)*2 {
break
}
Expand Down
41 changes: 0 additions & 41 deletions thread_safe_buffer.go

This file was deleted.

0 comments on commit 7f5cad7

Please sign in to comment.