mirror of
https://github.com/edgelesssys/constellation.git
synced 2024-12-26 16:09:45 -05:00
abe40de3e5
* 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
92 lines
1.9 KiB
Go
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()
|
|
}
|