github.com/SAP/jenkins-library@v1.362.0/cmd/getDefaults_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "io" 8 "strings" 9 "testing" 10 11 "github.com/spf13/cobra" 12 flag "github.com/spf13/pflag" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var stageConditionsExample string = `#Piper general purpose pipeline stage configuration including conditions 17 apiVersion: project-piper.io/v1 18 kind: PipelineDefinition 19 metadata: 20 name: sap-piper.general.purpose.pipeline 21 displayName: Piper general purpose pipeline 22 description: |- 23 This is a multiline 24 test description 25 spec: 26 stages: 27 # Init stage 28 - name: init 29 displayName: Init 30 description: |- 31 Test description 32 steps: 33 - name: getConfig 34 description: Read pipeline stage configuration.` 35 36 var stageConditionsExpected string = `"apiVersion: project-piper.io/v1\nkind: PipelineDefinition\nmetadata:\n description: |-\n This is a multiline\n test description\n displayName: Piper general purpose pipeline\n name: sap-piper.general.purpose.pipeline\nspec:\n` + 37 ` stages:\n - description: Test description\n displayName: Init\n name: init\n steps:\n - description: Read pipeline stage configuration.\n name: getConfig\n"` 38 39 func defaultsOpenFileMock(name string, tokens map[string]string) (io.ReadCloser, error) { 40 var r string 41 switch name { 42 case "TestAddCustomDefaults_default1": 43 r = "default1" 44 case "TestAddCustomDefaults_default2": 45 r = "default3" 46 case "stage_conditions.yaml": 47 r = stageConditionsExample 48 default: 49 r = "" 50 } 51 return io.NopCloser(strings.NewReader(r)), nil 52 } 53 54 func TestDefaultsCommand(t *testing.T) { 55 cmd := DefaultsCommand() 56 57 gotReq := []string{} 58 gotOpt := []string{} 59 60 cmd.Flags().VisitAll(func(pflag *flag.Flag) { 61 annotations, found := pflag.Annotations[cobra.BashCompOneRequiredFlag] 62 if found && annotations[0] == "true" { 63 gotReq = append(gotReq, pflag.Name) 64 } else { 65 gotOpt = append(gotOpt, pflag.Name) 66 } 67 }) 68 69 t.Run("Required flags", func(t *testing.T) { 70 exp := []string{"defaultsFile"} 71 assert.Equal(t, exp, gotReq, "required flags incorrect") 72 }) 73 74 t.Run("Optional flags", func(t *testing.T) { 75 exp := []string{"output", "outputFile", "useV1"} 76 assert.Equal(t, exp, gotOpt, "optional flags incorrect") 77 }) 78 79 t.Run("Run", func(t *testing.T) { 80 t.Run("Success case", func(t *testing.T) { 81 defaultsOptions.openFile = defaultsOpenFileMock 82 defaultsOptions.defaultsFiles = []string{"test", "test"} 83 cmd.Run(cmd, []string{}) 84 }) 85 }) 86 } 87 88 func TestGenerateDefaults(t *testing.T) { 89 testParams := []struct { 90 name string 91 defaultsFiles []string 92 useV1 bool 93 expected string 94 }{ 95 { 96 name: "Single defaults file", 97 defaultsFiles: []string{"test"}, 98 expected: `{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test"}`, 99 }, 100 { 101 name: "Multiple defaults files", 102 defaultsFiles: []string{"test1", "test2"}, 103 expected: `[{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test1"},` + 104 `{"content":"general: null\nstages: null\nsteps: null\n","filepath":"test2"}]`, 105 }, 106 { 107 name: "Single file + useV1", 108 defaultsFiles: []string{"stage_conditions.yaml"}, 109 useV1: true, 110 expected: `{"content":` + stageConditionsExpected + `,"filepath":"stage_conditions.yaml"}`, 111 }, 112 { 113 name: "Multiple files + useV1", 114 defaultsFiles: []string{"stage_conditions.yaml", "stage_conditions.yaml"}, 115 useV1: true, 116 expected: `[{"content":` + stageConditionsExpected + `,"filepath":"stage_conditions.yaml"},` + 117 `{"content":` + stageConditionsExpected + `,"filepath":"stage_conditions.yaml"}]`, 118 }, 119 } 120 121 utils := newGetDefaultsUtilsUtils() 122 defaultsOptions.openFile = defaultsOpenFileMock 123 124 for _, test := range testParams { 125 t.Run(test.name, func(t *testing.T) { 126 defaultsOptions.defaultsFiles = test.defaultsFiles 127 defaultsOptions.useV1 = test.useV1 128 result, _ := generateDefaults(utils) 129 assert.Equal(t, test.expected, string(result)) 130 }) 131 } 132 }