constellation/cli/internal/cmd/spinner_test.go

117 lines
2.4 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
import (
"bytes"
"fmt"
"strings"
2022-10-07 13:35:07 -04:00
"sync/atomic"
"testing"
"time"
2022-10-07 13:35:07 -04:00
"github.com/stretchr/testify/assert"
)
const (
baseWait = 1
baseText = "Loading"
)
func TestSpinnerInitialState(t *testing.T) {
2022-10-07 13:35:07 -04:00
assert := assert.New(t)
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
2022-10-07 13:35:07 -04:00
s, _ := newSpinner(cmd, nil)
s.Start(baseText, true)
time.Sleep(baseWait * time.Second)
s.Stop()
2022-10-07 13:35:07 -04:00
assert.True(out.Len() > 0)
assert.True(errOut.Len() == 0)
outStr := out.String()
2022-10-07 13:35:07 -04:00
assert.True(strings.HasPrefix(outStr, generateAllStatesAsString(baseText, true)))
}
func TestSpinnerFinalState(t *testing.T) {
2022-10-07 13:35:07 -04:00
assert := assert.New(t)
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
2022-10-07 13:35:07 -04:00
s, _ := newSpinner(cmd, nil)
s.Start(baseText, true)
time.Sleep(baseWait * time.Second)
s.Stop()
2022-10-07 13:35:07 -04:00
assert.True(out.Len() > 0)
assert.True(errOut.Len() == 0)
outStr := out.String()
2022-10-07 13:35:07 -04:00
assert.True(strings.HasSuffix(outStr, baseText+"... \n"))
}
func TestSpinnerDisabledShowDotsFlag(t *testing.T) {
2022-10-07 13:35:07 -04:00
assert := assert.New(t)
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
2022-10-07 13:35:07 -04:00
s, _ := newSpinner(cmd, nil)
s.Start(baseText, false)
time.Sleep(baseWait * time.Second)
s.Stop()
2022-10-07 13:35:07 -04:00
assert.True(out.Len() > 0)
assert.True(errOut.Len() == 0)
outStr := out.String()
2022-10-07 13:35:07 -04:00
assert.True(strings.HasPrefix(outStr, generateAllStatesAsString(baseText, false)))
assert.True(strings.HasSuffix(outStr, baseText+" \n"))
}
func TestSpinnerInterruptWriter(t *testing.T) {
assert := assert.New(t)
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
s, interruptWriter := newSpinner(cmd, &out)
s.Start(baseText, false)
time.Sleep(200 * time.Millisecond)
_, err := interruptWriter.Write([]byte("test"))
assert.NoError(err)
assert.Equal(int32(1), atomic.LoadInt32(&s.stop))
assert.True(strings.HasSuffix(out.String(), "test"))
}
func generateAllStatesAsString(text string, showDots bool) string {
var builder strings.Builder
for i := 0; i < len(spinnerStates); i++ {
dotsState := ""
if showDots {
dotsState = dotsStates[i%len(dotsStates)]
}
builder.WriteString(fmt.Sprintf("\r%s %s%s", spinnerStates[i], text, dotsState))
}
return builder.String()
}