1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-06 04:13:55 +02:00
sap-jenkins-library/pkg/documentation/generator/main_test.go
Christopher Fenner 0797f4049a
feat(docs): use custom defaults during docs generation (#2022)
* remove docs generator code from step-generator

* add docs generator to dedicated package

* add test cases

* add entry point for docs generation

* make output more readable

* read additional defaults

* add custo defaults parameters

* remove commented code

* adjust custom default parameter in workflow

* remove conflict leftovers

* handle custom default values

* remove comment

* extract code to function

* extract metadata reading to function

* do not print empty strings in favor of PIPER_* env vars

* extract new code to own metadata file

* only reset default on booleans

* remove obsolete test case
2020-10-16 12:50:39 +02:00

91 lines
1.8 KiB
Go

package generator
import (
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestReadAndAdjustTemplate(t *testing.T) {
t.Run("Success Case", func(t *testing.T) {
tmpl, _ := configOpenDocTemplateFileMock("testStep.md")
content := readAndAdjustTemplate(tmpl)
cases := []struct {
x, y string
}{
{"{{StepName .}}", "${docGenStepName}"},
{"{{Parameters .}}", "${docGenParameters}"},
{"{{Description .}}", "${docGenDescription}"},
{"", "${docGenConfiguration}"},
{"", "${docJenkinsPluginDependencies}"},
}
for _, c := range cases {
if len(c.x) > 0 {
assert.Contains(t, content, c.x)
}
if len(c.y) > 0 {
assert.NotContains(t, content, c.y)
}
}
})
}
func configOpenDocTemplateFileMock(docTemplateFilePath string) (io.ReadCloser, error) {
meta1 := `# ${docGenStepName}
## ${docGenDescription}
## Prerequisites
none
## ${docJenkinsPluginDependencies}
## ${docGenParameters}
## ${docGenConfiguration}
## Side effects
none
## Exceptions
none
## Example
none
`
switch docTemplateFilePath {
case "testStep.md":
return ioutil.NopCloser(strings.NewReader(meta1)), nil
default:
return ioutil.NopCloser(strings.NewReader("")), fmt.Errorf("Wrong Path: %v", docTemplateFilePath)
}
}
func TestSetDefaultAndPossisbleValues(t *testing.T) {
stepData := config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{Parameters: []config.StepParameters{
{Name: "boolean", Type: "bool"},
{Name: "integer", Type: "int"},
}},
},
}
setDefaultAndPossisbleValues(&stepData)
assert.Equal(t, false, stepData.Spec.Inputs.Parameters[0].Default)
assert.Equal(t, 0, stepData.Spec.Inputs.Parameters[1].Default)
assert.Equal(t, []interface{}{true, false}, stepData.Spec.Inputs.Parameters[0].PossibleValues)
}