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