Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrareddyp committed Jan 20, 2024
1 parent 0added0 commit a9d6443
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions component/output_spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type OutputWriterSpinner interface {
OutputWriter
RenderWithSpinner()
StopSpinner()
// SetFinalText sets the spinner final text and prefix log indicator if spinner is active
SetFinalText(finalText string, prefix log.LogTypeIndicator)
}

// outputwriterspinner is our internal implementation.
Expand Down Expand Up @@ -76,6 +78,7 @@ func setAndInitializeSpinner(ows *outputwriterspinner, spinnerText string, start

// Start the spinner only if attached to terminal
attachedToTerminal := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
fmt.Printf("valueeeeee=%v", attachedToTerminal)
if startSpinner && attachedToTerminal {
ows.spinner.Start()
}
Expand Down
107 changes: 107 additions & 0 deletions component/output_spinner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2024 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package component

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/tanzu-plugin-runtime/log"
)

const loading = "Loading..."

func TestNewOutputWriterWithSpinner(t *testing.T) {
output := bytes.Buffer{}
spinnerText := loading
headers := []string{"Name", "Age"}

// Test creating an OutputWriterSpinner with a spinner
ows, err := NewOutputWriterWithSpinner(&output, "table", spinnerText, true, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)

// Test creating an OutputWriterSpinner without a spinner
ows, err = NewOutputWriterWithSpinner(&output, "table", spinnerText, false, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)

// Test creating an OutputWriterSpinner with unsupported output format
ows, err = NewOutputWriterWithSpinner(&output, "unsupported", spinnerText, true, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)
}

func TestNewOutputWriterSpinnerWithOptions(t *testing.T) {
output := bytes.Buffer{}
spinnerText := loading
headers := []string{"Name", "Age"}

// Test creating an OutputWriterSpinner with options and a spinner
opts := []OutputWriterOption{WithAutoStringify()}
ows, err := NewOutputWriterSpinnerWithOptions(&output, "table", spinnerText, true, opts, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)

// Test creating an OutputWriterSpinner with options without a spinner
opts = []OutputWriterOption{WithAutoStringify()}
ows, err = NewOutputWriterSpinnerWithOptions(&output, "table", spinnerText, false, opts, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)

// Test creating an OutputWriterSpinner with unsupported output format
opts = []OutputWriterOption{WithAutoStringify()}
ows, err = NewOutputWriterSpinnerWithOptions(&output, "unsupported", spinnerText, true, opts, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)
}

func TestNewOutputWriterSpinnerWithSpinnerOptions(t *testing.T) {
output := bytes.Buffer{}
spinnerText := loading
headers := []string{"Name", "Age"}

// Test creating an OutputWriterSpinner with spinner options and a spinner
opts := OutputWriterSpinnerOptions{
OutputWriterOptions: []OutputWriterOption{WithAutoStringify()},
SpinnerOptions: []OutputWriterSpinnerOption{WithSpinnerFinalMsg("Done!", log.LogTypeSUCCESS)},
}
ows, err := NewOutputWriterSpinnerWithSpinnerOptions(&output, "table", spinnerText, true, opts, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)

// Test creating an OutputWriterSpinner with spinner options without a spinner
opts = OutputWriterSpinnerOptions{
SpinnerOptions: []OutputWriterSpinnerOption{WithSpinnerFinalMsg("Done!", log.LogTypeSUCCESS)},
}
ows, err = NewOutputWriterSpinnerWithSpinnerOptions(&output, "table", spinnerText, false, opts, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)

// Test creating an OutputWriterSpinner with unsupported output format
opts = OutputWriterSpinnerOptions{}
ows, err = NewOutputWriterSpinnerWithSpinnerOptions(&output, "unsupported", spinnerText, true, opts, headers...)
assert.NoError(t, err)
assert.NotNil(t, ows)
}

func TestOutputWriterSpinnerRenderWithSpinner(t *testing.T) {
output := bytes.Buffer{}
spinnerText := loading
headers := []string{"Name", "Age"}

// Create an OutputWriterSpinner with a spinner
ows, err := NewOutputWriterWithSpinner(&output, "table", spinnerText, true, headers...)
ows.AddRow(map[string]interface{}{"Name": "John", "Age": 30})
assert.NoError(t, err)
assert.NotNil(t, ows)

// Render with spinner
ows.RenderWithSpinner()
assert.Contains(t, output.String(), "NAME")
assert.Contains(t, output.String(), "John")
assert.Contains(t, output.String(), "30")
}

0 comments on commit a9d6443

Please sign in to comment.