get.porter.sh/porter@v1.3.0/pkg/porter/version/version_test.go (about)

     1  package version
     2  
     3  import (
     4  	"testing"
     5  
     6  	"get.porter.sh/porter/pkg/printer"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestVersionOptions_Validate(t *testing.T) {
    12  	testcases := []struct {
    13  		name       string
    14  		opts       Options
    15  		wantFormat printer.Format
    16  		wantError  string
    17  	}{
    18  		{"json", Options{printer.PrintOptions{RawFormat: "json"}}, printer.FormatJson, ""},
    19  		{"plaintext", Options{printer.PrintOptions{RawFormat: "plaintext"}}, printer.FormatPlaintext, ""},
    20  		{"default", Options{}, printer.FormatPlaintext, ""},
    21  		{"yaml - unsupported", Options{printer.PrintOptions{RawFormat: "yaml"}}, printer.Format(""), "unsupported"},
    22  		{"oops - invalid", Options{printer.PrintOptions{RawFormat: "oops"}}, printer.Format(""), "invalid"},
    23  	}
    24  
    25  	for _, tc := range testcases {
    26  		t.Run(tc.name, func(t *testing.T) {
    27  			err := tc.opts.Validate()
    28  
    29  			if tc.wantError == "" {
    30  				require.NoError(t, err, "Validate should not have returned an error")
    31  			} else {
    32  				require.Error(t, err, "Validate should have returned an error")
    33  				assert.Contains(t, err.Error(), tc.wantError, "Validate did not return the expected error message")
    34  			}
    35  		})
    36  	}
    37  }