github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/documentation/generator/main_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package generator
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/SAP/jenkins-library/pkg/config"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestReadAndAdjustTemplate(t *testing.T) {
    17  
    18  	t.Run("Success Case", func(t *testing.T) {
    19  
    20  		tmpl, _ := configOpenDocTemplateFileMock("testStep.md")
    21  		content := readAndAdjustTemplate(tmpl)
    22  
    23  		cases := []struct {
    24  			x, y string
    25  		}{
    26  			{"{{StepName .}}", "${docGenStepName}"},
    27  			{"{{Parameters .}}", "${docGenParameters}"},
    28  			{"{{Description .}}", "${docGenDescription}"},
    29  			{"", "${docGenConfiguration}"},
    30  			{"", "${docJenkinsPluginDependencies}"},
    31  		}
    32  		for _, c := range cases {
    33  			if len(c.x) > 0 {
    34  				assert.Contains(t, content, c.x)
    35  			}
    36  			if len(c.y) > 0 {
    37  				assert.NotContains(t, content, c.y)
    38  			}
    39  		}
    40  	})
    41  }
    42  
    43  func configOpenDocTemplateFileMock(docTemplateFilePath string) (io.ReadCloser, error) {
    44  	meta1 := `# ${docGenStepName}
    45  
    46  	## ${docGenDescription}
    47  
    48  	## Prerequisites
    49  
    50  	none
    51  
    52  	## ${docJenkinsPluginDependencies}
    53  
    54  	## ${docGenParameters}
    55  
    56  	## ${docGenConfiguration}
    57  
    58  	## Side effects
    59  
    60  	none
    61  
    62  	## Exceptions
    63  
    64  	none
    65  
    66  	## Example
    67  
    68  	none
    69  `
    70  	switch docTemplateFilePath {
    71  	case "testStep.md":
    72  		return io.NopCloser(strings.NewReader(meta1)), nil
    73  	default:
    74  		return io.NopCloser(strings.NewReader("")), fmt.Errorf("Wrong Path: %v", docTemplateFilePath)
    75  	}
    76  }
    77  
    78  func TestSetDefaultAndPossisbleValues(t *testing.T) {
    79  	stepData := config.StepData{
    80  		Spec: config.StepSpec{
    81  			Inputs: config.StepInputs{Parameters: []config.StepParameters{
    82  				{Name: "boolean", Type: "bool"},
    83  				{Name: "integer", Type: "int"},
    84  			}},
    85  		},
    86  	}
    87  	setDefaultAndPossisbleValues(&stepData)
    88  	assert.Equal(t, false, stepData.Spec.Inputs.Parameters[0].Default)
    89  	assert.Equal(t, 0, stepData.Spec.Inputs.Parameters[1].Default)
    90  	assert.Equal(t, []interface{}{true, false}, stepData.Spec.Inputs.Parameters[0].PossibleValues)
    91  
    92  }
    93  
    94  func TestGetBadge(t *testing.T) {
    95  	tt := []struct {
    96  		in       string
    97  		expected string
    98  	}{
    99  		{in: "Jenkins", expected: "[![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)](#)"},
   100  		{in: "jenkins", expected: "[![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)](#)"},
   101  		{in: "Azure", expected: "[![Azure only](https://img.shields.io/badge/-Azure%20only-yellowgreen)](#)"},
   102  		{in: "azure", expected: "[![Azure only](https://img.shields.io/badge/-Azure%20only-yellowgreen)](#)"},
   103  		{in: "Github Actions", expected: "[![Github Actions only](https://img.shields.io/badge/-Github%20Actions%20only-yellowgreen)](#)"},
   104  		{in: "github actions", expected: "[![Github Actions only](https://img.shields.io/badge/-Github%20Actions%20only-yellowgreen)](#)"},
   105  	}
   106  
   107  	for _, test := range tt {
   108  		assert.Equal(t, test.expected, getBadge(test.in))
   109  	}
   110  }
   111  
   112  func TestGetStepConditionDetails(t *testing.T) {
   113  	tt := []struct {
   114  		name     string
   115  		step     config.Step
   116  		expected string
   117  	}{
   118  		{name: "noCondition", step: config.Step{Conditions: []config.StepCondition{}}, expected: "**active** by default - deactivate explicitly"},
   119  		{name: "config", step: config.Step{Conditions: []config.StepCondition{{Config: map[string][]interface{}{"configKey1": {"keyVal1", "keyVal2"}}}}}, expected: "<i>config:</i><ul><li>`configKey1`: `keyVal1`</li><li>`configKey1`: `keyVal2`</li></ul>"},
   120  		{name: "configKey", step: config.Step{Conditions: []config.StepCondition{{ConfigKey: "configKey"}}}, expected: "<i>config key:</i>&nbsp;`configKey`<br />"},
   121  		{name: "filePattern", step: config.Step{Conditions: []config.StepCondition{{FilePattern: "testPattern"}}}, expected: "<i>file pattern:</i>&nbsp;`testPattern`<br />"},
   122  		{name: "filePatternFromConfig", step: config.Step{Conditions: []config.StepCondition{{FilePatternFromConfig: "patternConfigKey"}}}, expected: "<i>file pattern from config:</i>&nbsp;`patternConfigKey`<br />"},
   123  		{name: "inactive", step: config.Step{Conditions: []config.StepCondition{{Inactive: true}}}, expected: "**inactive** by default - activate explicitly"},
   124  		{name: "npmScript", step: config.Step{Conditions: []config.StepCondition{{NpmScript: "testScript"}}}, expected: "<i>npm script:</i>&nbsp;`testScript`<br />"},
   125  		{name: "multiple conditions", step: config.Step{Conditions: []config.StepCondition{{ConfigKey: "configKey"}, {FilePattern: "testPattern"}}}, expected: "<i>config key:</i>&nbsp;`configKey`<br /><i>file pattern:</i>&nbsp;`testPattern`<br />"},
   126  	}
   127  
   128  	for _, test := range tt {
   129  		t.Run(test.name, func(t *testing.T) {
   130  			assert.Equal(t, test.expected, getStepConditionDetails(test.step))
   131  		})
   132  	}
   133  }