github.com/verrazzano/verrazzano@v1.7.0/tools/charts-manager/vcm/cmd/helpers/command_test.go (about) 1 // Copyright (c) 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package helpers 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 vcmtesthelpers "github.com/verrazzano/verrazzano/tools/charts-manager/vcm/tests/pkg/helpers" 12 cmdhelpers "github.com/verrazzano/verrazzano/tools/vz/cmd/helpers" 13 ) 14 15 // TestGetMandatoryStringFlagValueOrError tests the execution of GetMandatoryStringFlagValueOrError 16 // GIVEN a call to GetMandatoryStringFlagValueOrError with specific parameters to get non empty value of a flag 17 // 18 // WHEN the input flag is not declared 19 // THEN the execution results in an error. 20 // 21 // WHEN the flag value is empty or nil 22 // THEN the execution results in an error. 23 // 24 // WHEN the flag value is non empty 25 // THEN the execution returns the flag value. 26 func TestGetMandatoryStringFlagValueOrError(t *testing.T) { 27 anyError := fmt.Errorf("") 28 rc, cleanup, err := vcmtesthelpers.ContextSetup() 29 assert.NoError(t, err) 30 defer cleanup() 31 tests := []struct { 32 name string 33 flagName string 34 flagValue string 35 testFlagName string 36 testFlagValue string 37 wantError error 38 }{ 39 { 40 name: "testGetMandatoryStringFlagValueOrErrorNoFlagsCommand", 41 testFlagName: "testFlag", 42 wantError: anyError, 43 }, 44 { 45 name: "testGetMandatoryStringFlagValueOrErrorNilFlagValue", 46 flagName: "testFlag", 47 testFlagName: "testFlag", 48 wantError: fmt.Errorf(ErrFormatMustSpecifyFlag, "testFlag", "testFlag", ""), 49 }, 50 { 51 name: "testGetMandatoryStringFlagValueOrErrorEmptyFlagValue", 52 flagName: "testFlag", 53 flagValue: "\t\t\n", 54 testFlagName: "testFlag", 55 wantError: fmt.Errorf(ErrFormatNotEmpty, "testFlag"), 56 }, 57 { 58 name: "testGetMandatoryStringFlagValueOrErrorValidFlag", 59 flagName: "testFlag", 60 flagValue: "value", 61 testFlagName: "testFlag", 62 testFlagValue: "value", 63 wantError: nil, 64 }, 65 } 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 cmd := cmdhelpers.NewCommand(rc, "test", "", "") 69 if tt.flagName != "" { 70 cmd.PersistentFlags().StringP(tt.flagName, "", "", "") 71 } 72 73 if tt.flagValue != "" { 74 cmd.PersistentFlags().Set(tt.flagName, tt.flagValue) 75 } 76 77 value, err := GetMandatoryStringFlagValueOrError(cmd, tt.testFlagName, "") 78 if err != nil && tt.wantError == nil { 79 t.Errorf("unexpected error %v", err) 80 } 81 82 if err != nil && tt.wantError != nil && tt.wantError != anyError && err.Error() != tt.wantError.Error() { 83 t.Errorf("error %v, expected %v", err, tt.wantError) 84 } 85 86 if err == nil && tt.wantError != nil { 87 t.Errorf("resulted in no error, expected %v", tt.wantError) 88 } 89 90 if tt.wantError == anyError { 91 assert.Error(t, err) 92 } 93 94 assert.Equal(t, tt.testFlagValue, value) 95 }) 96 } 97 }