constellation/cli/internal/cmd/spinner_test.go
Valentyn Yukhymenko abe40de3e5
Activity indicator for init command (#207)
* first version of spinner

- implemented class with basic method
- covered with dummy test
- integrated with init command

* Style and license remarks

* fixed review remarks

* fixed typo + integration of spinner with terminate command

* integration of spinner with create command
2022-10-04 18:17:05 +02:00

92 lines
1.9 KiB
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
import (
"bytes"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
baseWait = 1
baseText = "Loading"
)
func TestSpinnerInitialState(t *testing.T) {
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
s := newSpinner(cmd, baseText, true)
s.Start()
time.Sleep(baseWait * time.Second)
s.Stop()
require.True(t, out.Len() > 0)
require.True(t, errOut.Len() == 0)
outStr := out.String()
require.True(t, strings.HasPrefix(outStr, generateAllStatesAsString(baseText, true)))
}
func TestSpinnerFinalState(t *testing.T) {
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
s := newSpinner(cmd, baseText, true)
s.Start()
time.Sleep(baseWait * time.Second)
s.Stop()
require.True(t, out.Len() > 0)
require.True(t, errOut.Len() == 0)
outStr := out.String()
require.True(t, strings.HasSuffix(outStr, baseText+"... \n"))
}
func TestSpinnerDisabledShowDotsFlag(t *testing.T) {
cmd := NewInitCmd()
var out bytes.Buffer
cmd.SetOut(&out)
var errOut bytes.Buffer
cmd.SetErr(&errOut)
s := newSpinner(cmd, baseText, false)
s.Start()
time.Sleep(baseWait * time.Second)
s.Stop()
require.True(t, out.Len() > 0)
require.True(t, errOut.Len() == 0)
outStr := out.String()
require.True(t, strings.HasPrefix(outStr, generateAllStatesAsString(baseText, false)))
require.True(t, strings.HasSuffix(outStr, baseText+" \n"))
}
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()
}