Skip to content

Commit

Permalink
Fail Start() if no CharSet has been set for the spinner
Browse files Browse the repository at this point in the history
I believe the code will panic otherwise. No use causing that sort of behavior
for consumers if we do know they've provided an invalid configuration.
  • Loading branch information
theckman committed Dec 30, 2021
1 parent 334520a commit 1cfaecb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
11 changes: 11 additions & 0 deletions spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ func (s *Spinner) Start() error {

s.mu.Lock()

if len(s.chars) == 0 {
s.mu.Unlock()

// move us to the stopped state
if !atomic.CompareAndSwapUint32(s.status, statusStarting, statusStopped) {
panic("atomic invariant encountered")
}

return errors.New("before starting the spinner a CharSet must be set")
}

s.frequencyUpdateCh = make(chan time.Duration, 4)
s.dataUpdateCh, s.cancelCh = make(chan struct{}, 1), make(chan struct{}, 1)

Expand Down
44 changes: 43 additions & 1 deletion spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,21 @@ func TestSpinner_Start(t *testing.T) {
},
err: "spinner already running or shutting down",
},
{
name: "empty_CharSet",
spinner: &Spinner{
buffer: &bytes.Buffer{},
status: uint32Ptr(statusStopped),
mu: &sync.Mutex{},
frequency: time.Millisecond,
colorFn: fmt.Sprintf,
stopColorFn: fmt.Sprintf,
stopFailColorFn: fmt.Sprintf,
stopMsg: "stop msg",
stopFailMsg: "stop fail msg",
},
err: "before starting the spinner a CharSet must be set",
},
{
name: "spinner",
spinner: &Spinner{
Expand All @@ -808,6 +823,20 @@ func TestSpinner_Start(t *testing.T) {
stopFailColorFn: fmt.Sprintf,
stopMsg: "stop msg",
stopFailMsg: "stop fail msg",
chars: []character{
character{
Value: ".",
Size: 1,
},
character{
Value: "..",
Size: 21,
},
character{
Value: "...",
Size: 3,
},
},
},
},
{
Expand All @@ -823,6 +852,20 @@ func TestSpinner_Start(t *testing.T) {
stopMsg: "stop msg",
stopFailMsg: "stop fail msg",
isNotTTY: true,
chars: []character{
character{
Value: ".",
Size: 1,
},
character{
Value: "..",
Size: 21,
},
character{
Value: "...",
Size: 3,
},
},
},
},
}
Expand All @@ -831,7 +874,6 @@ func TestSpinner_Start(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
tt.spinner.writer = buf
_ = tt.spinner.CharSet(CharSets[26])

err := tt.spinner.Start()

Expand Down

0 comments on commit 1cfaecb

Please sign in to comment.