From a9d6443e4f6200306724d6cf6e05fa5260cc5b4f Mon Sep 17 00:00:00 2001 From: Chandra Pamuluri Date: Fri, 19 Jan 2024 22:43:45 -0600 Subject: [PATCH] tests --- component/output_spinner.go | 3 + component/output_spinner_test.go | 107 +++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 component/output_spinner_test.go diff --git a/component/output_spinner.go b/component/output_spinner.go index b4a5af3a9..4fedb3053 100644 --- a/component/output_spinner.go +++ b/component/output_spinner.go @@ -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. @@ -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() } diff --git a/component/output_spinner_test.go b/component/output_spinner_test.go new file mode 100644 index 000000000..f4e708d7c --- /dev/null +++ b/component/output_spinner_test.go @@ -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") +}