Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail Start() if no CharSet has been set for the spinner #57

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 45 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,21 @@ func TestSpinner_Start(t *testing.T) {
stopFailColorFn: fmt.Sprintf,
stopMsg: "stop msg",
stopFailMsg: "stop fail msg",
maxWidth: 3,
chars: []character{
character{
Value: ".",
Size: 1,
},
character{
Value: "..",
Size: 21,
},
character{
Value: "...",
Size: 3,
},
},
},
},
{
Expand All @@ -823,6 +853,21 @@ func TestSpinner_Start(t *testing.T) {
stopMsg: "stop msg",
stopFailMsg: "stop fail msg",
isNotTTY: true,
maxWidth: 3,
chars: []character{
character{
Value: ".",
Size: 1,
},
character{
Value: "..",
Size: 21,
},
character{
Value: "...",
Size: 3,
},
},
},
},
}
Expand All @@ -831,7 +876,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