package generator import ( "fmt" "testing" "github.com/SAP/jenkins-library/pkg/config" "github.com/stretchr/testify/assert" ) func TestCreateParameterOverview(t *testing.T) { stepData := config.StepData{ Spec: config.StepSpec{ Inputs: config.StepInputs{ Resources: []config.StepResources{ {Name: "testStash", Type: "stash"}, }, Parameters: []config.StepParameters{ {Name: "param1"}, {Name: "stashContent", Default: "testStash"}, }, }, }, } expected := `| Name | Mandatory | Additional information | | ---- | --------- | ---------------------- | | [param1](#param1) | no | | | [stashContent](#stashcontent) | no | [![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)](#) | ` stepParameterNames = []string{"param1"} assert.Equal(t, expected, createParameterOverview(&stepData)) stepParameterNames = []string{} } func TestParameterFurtherInfo(t *testing.T) { tt := []struct { paramName string stepParams []string stepData *config.StepData contains string notContains []string }{ {paramName: "verbose", stepParams: []string{}, stepData: nil, contains: "activates debug output"}, {paramName: "script", stepParams: []string{}, stepData: nil, contains: "reference to Jenkins main pipeline script"}, {paramName: "contextTest", stepParams: []string{}, stepData: &config.StepData{}, contains: "Jenkins only", notContains: []string{"pipeline script", "id of credentials"}}, {paramName: "noop", stepParams: []string{"noop"}, stepData: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{}}}}, contains: ""}, { paramName: "testCredentialId", stepData: &config.StepData{ Spec: config.StepSpec{ Inputs: config.StepInputs{ Secrets: []config.StepSecrets{{Name: "testCredentialId", Type: "jenkins"}}, }, }, }, contains: "id of credentials", }, { paramName: "testSecret1", stepParams: []string{"testSecret1"}, stepData: &config.StepData{ Spec: config.StepSpec{ Inputs: config.StepInputs{ Parameters: []config.StepParameters{ {Name: "testSecret1", Secret: true, ResourceRef: []config.ResourceReference{{Name: "mytestSecret", Type: "secret"}}}, }, }, }, }, contains: "credentials ([`mytestSecret`](#mytestsecret))", }, { paramName: "testSecret2", stepParams: []string{"testSecret2"}, stepData: &config.StepData{ Spec: config.StepSpec{ Inputs: config.StepInputs{ Parameters: []config.StepParameters{ {Name: "testSecret2"}, }, }, }, }, contains: "", }, } for _, test := range tt { stepParameterNames = test.stepParams res := parameterFurtherInfo(test.paramName, test.stepData) stepParameterNames = []string{} if len(test.contains) == 0 { assert.Equalf(t, test.contains, res, fmt.Sprintf("param %v", test.paramName)) } else { assert.Containsf(t, res, test.contains, fmt.Sprintf("param %v", test.paramName)) } for _, notThere := range test.notContains { assert.NotContainsf(t, res, notThere, fmt.Sprintf("param %v", test.paramName)) } } } func TestCreateParameterDetails(t *testing.T) { stepData := config.StepData{ Spec: config.StepSpec{ Inputs: config.StepInputs{ Parameters: []config.StepParameters{ { Name: "param1", Aliases: []config.Alias{{Name: "param1Alias"}, {Name: "paramAliasDeprecated", Deprecated: true}}, Mandatory: true, Default: "param1Default", LongDescription: "long description", PossibleValues: []interface{}{"val1", "val2"}, Scope: []string{"STEPS"}, Secret: true, Type: "string", }, }, }, }, } res := createParameterDetails(&stepData) assert.Contains(t, res, "#### param1") assert.Contains(t, res, "long description") assert.Contains(t, res, "`param1Alias`") assert.Contains(t, res, "`paramAliasDeprecated` (**deprecated**)") assert.Contains(t, res, "string") assert.Contains(t, res, "param1Default") assert.Contains(t, res, "val1") assert.Contains(t, res, "val2") assert.Contains(t, res, "no") assert.Contains(t, res, "**yes**") assert.Contains(t, res, "steps") } func TestFormatDefault(t *testing.T) { tt := []struct { param config.StepParameters stepParams []string expected string contains []string }{ {param: config.StepParameters{Name: "test1"}, stepParams: []string{"test1"}, expected: "`$PIPER_test1` (if set)"}, {param: config.StepParameters{Name: "jenkins1"}, stepParams: []string{}, expected: ""}, { param: config.StepParameters{Name: "test1", Default: []conditionDefault{{key: "key1", value: "val1", def: "def1"}, {key: "key2", value: "val2", def: "def2"}}}, stepParams: []string{"test1"}, contains: []string{"key1=`val1`: `def1`", "key2=`val2`: `def2`"}, }, { param: config.StepParameters{Name: "test1", Default: []interface{}{conditionDefault{key: "key1", value: "val1", def: "def1"}, "def2"}}, stepParams: []string{"test1"}, contains: []string{"key1=`val1`: `def1`", "- `def2`"}, }, { param: config.StepParameters{Name: "test1", Default: map[string]string{"key1": "def1", "key2": "def2"}}, stepParams: []string{"test1"}, contains: []string{"`key1`: `def1`", "`key2`: `def2`"}, }, {param: config.StepParameters{Name: "test1", Default: ""}, stepParams: []string{"test1"}, expected: "`''`"}, {param: config.StepParameters{Name: "test1", Default: "def1"}, stepParams: []string{"test1"}, expected: "`def1`"}, {param: config.StepParameters{Name: "test1", Default: 1}, stepParams: []string{"test1"}, expected: "`1`"}, } for _, test := range tt { if len(test.contains) > 0 { res := formatDefault(test.param, test.stepParams) for _, check := range test.contains { assert.Contains(t, res, check) } } else { assert.Equal(t, test.expected, formatDefault(test.param, test.stepParams)) } } } func TestAliasList(t *testing.T) { tt := []struct { aliases []config.Alias expected string contains []string }{ {aliases: []config.Alias{}, expected: "-"}, {aliases: []config.Alias{{Name: "alias1"}}, expected: "`alias1`"}, {aliases: []config.Alias{{Name: "alias1", Deprecated: true}}, expected: "`alias1` (**deprecated**)"}, {aliases: []config.Alias{{Name: "alias1"}, {Name: "alias2", Deprecated: true}}, contains: []string{"- `alias1`", "- `alias2` (**deprecated**)"}}, } for _, test := range tt { if len(test.contains) > 0 { res := aliasList(test.aliases) for _, check := range test.contains { assert.Contains(t, res, check) } } else { assert.Equal(t, test.expected, aliasList(test.aliases)) } } } func TestPossibleValueList(t *testing.T) { tt := []struct { possibleValues []interface{} expected string contains []string }{ {possibleValues: []interface{}{}, expected: ""}, {possibleValues: []interface{}{"poss1", 0}, contains: []string{"- `poss1`", "- `0`"}}, } for _, test := range tt { if len(test.contains) > 0 { res := possibleValueList(test.possibleValues) for _, check := range test.contains { assert.Contains(t, res, check) } } else { assert.Equal(t, test.expected, possibleValueList(test.possibleValues)) } } } func TestScopeDetails(t *testing.T) { tt := []struct { scope []string contains []string }{ {scope: []string{"PARAMETERS", "GENERAL", "STEPS", "STAGES"}, contains: []string{"