2022-10-04 12:17:05 -04:00
|
|
|
/*
|
|
|
|
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"
|
2022-10-04 12:17:05 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-10-07 13:35:07 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-04 12:17:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
baseWait = 1
|
|
|
|
baseText = "Loading"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSpinnerInitialState(t *testing.T) {
|
2022-10-07 13:35:07 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-04 12:17:05 -04:00
|
|
|
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)
|
2022-10-04 12:17:05 -04:00
|
|
|
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)
|
2022-10-04 12:17:05 -04:00
|
|
|
|
|
|
|
outStr := out.String()
|
2022-10-07 13:35:07 -04:00
|
|
|
assert.True(strings.HasPrefix(outStr, generateAllStatesAsString(baseText, true)))
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpinnerFinalState(t *testing.T) {
|
2022-10-07 13:35:07 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-04 12:17:05 -04:00
|
|
|
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)
|
2022-10-04 12:17:05 -04:00
|
|
|
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)
|
2022-10-04 12:17:05 -04:00
|
|
|
|
|
|
|
outStr := out.String()
|
2022-10-07 13:35:07 -04:00
|
|
|
assert.True(strings.HasSuffix(outStr, baseText+"... \n"))
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpinnerDisabledShowDotsFlag(t *testing.T) {
|
2022-10-07 13:35:07 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-10-04 12:17:05 -04:00
|
|
|
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)
|
2022-10-04 12:17:05 -04:00
|
|
|
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)
|
2022-10-04 12:17:05 -04:00
|
|
|
|
|
|
|
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"))
|
2022-10-04 12:17:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|