1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

fixed some codeclimate issues

This commit is contained in:
Sascha Vornheder 2019-11-13 14:43:53 +01:00
parent c6f5dbf5ee
commit 1a609e5418
5 changed files with 360 additions and 362 deletions

View File

@ -75,7 +75,7 @@ func readContextDefaultDescription(contextDefaultPath string) map[string]string
// generates the step documentation and replaces the template with the generated documentation
//func generateStepDocumentation(stepData config.StepData, docTemplateFilePath string, docTemplate io.ReadCloser, docFileWriter func(f string, d []byte, p os.FileMode) error) {
func generateStepDocumentation(stepData config.StepData, docuHelperData DocuHelperData) error{
func generateStepDocumentation(stepData config.StepData, docuHelperData DocuHelperData) error {
fmt.Printf("Generate docu for: %v\n", stepData.Metadata.Name)
//create the file path for the template and open it.
@ -85,13 +85,12 @@ func generateStepDocumentation(stepData config.StepData, docuHelperData DocuHelp
//check if there is an error during opening the template (true : skip docu generation for this meta data file)
if err != nil {
return fmt.Errorf("Error occured: %v\n", err)
return fmt.Errorf("error occured: %v", err)
}
content := readAndAdjustTemplate(docTemplate)
if len(content) <= 0 {
fmt.Printf("Error occured: No content inside of the template\n")
return fmt.Errorf("Error occured: No content inside of the template\n")
return fmt.Errorf("error occured: no content inside of the template")
}
// binding of functions and placeholder

View File

@ -1,126 +1,126 @@
package helper
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
var expectedResultDocument string = "# testStep\n\n\t## Description \n\nLong Test description\n\n\t\n\t## Prerequisites\n\t\n\tnone\n\n\t\n\t\n\t## Parameters\n\n| name | mandatory | default |\n| ---- | --------- | ------- |\n | param0 | No | val0 | \n | param1 | No | <nil> | \n | param2 | Yes | <nil> | \n ## Details\n * ` param0 ` : param0 description \n * ` param1 ` : param1 description \n * ` param2 ` : param1 description \n \n\t\n\t## We recommend to define values of step parameters via [config.yml file](../configuration.md). \n\nIn following sections of the config.yml the configuration is possible:\n\n| parameter | general | step/stage |\n|-----------|---------|------------|\n | param0 | X | | \n | param1 | | | \n | param2 | | | \n \n\t\n\t## Side effects\n\t\n\tnone\n\t\n\t## Exceptions\n\t\n\tnone\n\t\n\t## Example\n\n\tnone\n"
func configMetaDataMock(name string) (io.ReadCloser, error) {
meta1 := `metadata:
name: testStep
description: Test description
longDescription: |
Long Test description
spec:
inputs:
params:
- name: param0
type: string
description: param0 description
default: val0
scope:
- GENERAL
- PARAMETERS
mandatory: true
- name: param1
type: string
description: param1 description
scope:
- PARAMETERS
- name: param2
type: string
description: param1 description
scope:
- PARAMETERS
mandatory: true
`
var r string
switch name {
case "test.yaml":
r = meta1
default:
r = ""
}
return ioutil.NopCloser(strings.NewReader(r)), nil
}
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)
}
}
var resultDocumentContent string
func docFileWriterMock(docTemplateFilePath string, data []byte, perm os.FileMode) error {
resultDocumentContent = string(data)
switch docTemplateFilePath {
case "testStep.md":
return nil
default:
return fmt.Errorf("Wrong Path: %v", docTemplateFilePath)
}
}
func TestGenerateStepDocumentationSuccess(t *testing.T) {
var stepData config.StepData
contentMetaData, _ := configMetaDataMock("test.yaml")
stepData.ReadPipelineStepData(contentMetaData)
generateStepDocumentation(stepData,DocuHelperData{true, "" ,configOpenDocTemplateFileMock , docFileWriterMock})
t.Run("Docu Generation Success", func(t *testing.T) {
assert.Equal(t, expectedResultDocument, resultDocumentContent)
})
}
func TestGenerateStepDocumentationError(t *testing.T) {
var stepData config.StepData
contentMetaData, _ := configMetaDataMock("test.yaml")
stepData.ReadPipelineStepData(contentMetaData)
err := generateStepDocumentation(stepData, DocuHelperData{true, "Dummy" ,configOpenDocTemplateFileMock , docFileWriterMock})
t.Run("Docu Generation Success", func(t *testing.T) {
assert.Error(t, err, fmt.Sprintf("Error occured: %v\n", err))
})
}
package helper
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
var expectedResultDocument string = "# testStep\n\n\t## Description \n\nLong Test description\n\n\t\n\t## Prerequisites\n\t\n\tnone\n\n\t\n\t\n\t## Parameters\n\n| name | mandatory | default |\n| ---- | --------- | ------- |\n | param0 | No | val0 | \n | param1 | No | <nil> | \n | param2 | Yes | <nil> | \n ## Details\n * ` param0 ` : param0 description \n * ` param1 ` : param1 description \n * ` param2 ` : param1 description \n \n\t\n\t## We recommend to define values of step parameters via [config.yml file](../configuration.md). \n\nIn following sections of the config.yml the configuration is possible:\n\n| parameter | general | step/stage |\n|-----------|---------|------------|\n | param0 | X | | \n | param1 | | | \n | param2 | | | \n \n\t\n\t## Side effects\n\t\n\tnone\n\t\n\t## Exceptions\n\t\n\tnone\n\t\n\t## Example\n\n\tnone\n"
func configMetaDataMock(name string) (io.ReadCloser, error) {
meta1 := `metadata:
name: testStep
description: Test description
longDescription: |
Long Test description
spec:
inputs:
params:
- name: param0
type: string
description: param0 description
default: val0
scope:
- GENERAL
- PARAMETERS
mandatory: true
- name: param1
type: string
description: param1 description
scope:
- PARAMETERS
- name: param2
type: string
description: param1 description
scope:
- PARAMETERS
mandatory: true
`
var r string
switch name {
case "test.yaml":
r = meta1
default:
r = ""
}
return ioutil.NopCloser(strings.NewReader(r)), nil
}
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)
}
}
var resultDocumentContent string
func docFileWriterMock(docTemplateFilePath string, data []byte, perm os.FileMode) error {
resultDocumentContent = string(data)
switch docTemplateFilePath {
case "testStep.md":
return nil
default:
return fmt.Errorf("Wrong Path: %v", docTemplateFilePath)
}
}
func TestGenerateStepDocumentationSuccess(t *testing.T) {
var stepData config.StepData
contentMetaData, _ := configMetaDataMock("test.yaml")
stepData.ReadPipelineStepData(contentMetaData)
generateStepDocumentation(stepData, DocuHelperData{true, "", configOpenDocTemplateFileMock, docFileWriterMock})
t.Run("Docu Generation Success", func(t *testing.T) {
assert.Equal(t, expectedResultDocument, resultDocumentContent)
})
}
func TestGenerateStepDocumentationError(t *testing.T) {
var stepData config.StepData
contentMetaData, _ := configMetaDataMock("test.yaml")
stepData.ReadPipelineStepData(contentMetaData)
err := generateStepDocumentation(stepData, DocuHelperData{true, "Dummy", configOpenDocTemplateFileMock, docFileWriterMock})
t.Run("Docu Generation Success", func(t *testing.T) {
assert.Error(t, err, fmt.Sprintf("Error occured: %v\n", err))
})
}

View File

@ -1,230 +1,230 @@
package helper
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
func configOpenFileMock(name string) (io.ReadCloser, error) {
meta1 := `metadata:
name: testStep
description: Test description
longDescription: |
Long Test description
spec:
inputs:
params:
- name: param0
type: string
description: param0 description
default: val0
scope:
- GENERAL
- PARAMETERS
mandatory: true
- name: param1
type: string
description: param1 description
scope:
- PARAMETERS
- name: param2
type: string
description: param1 description
scope:
- PARAMETERS
mandatory: true
`
var r string
switch name {
case "test.yaml":
r = meta1
default:
r = ""
}
return ioutil.NopCloser(strings.NewReader(r)), nil
}
var files map[string][]byte
func writeFileMock(filename string, data []byte, perm os.FileMode) error {
if files == nil {
files = make(map[string][]byte)
}
files[filename] = data
return nil
}
func TestProcessMetaFiles(t *testing.T) {
ProcessMetaFiles([]string{"test.yaml"}, configOpenFileMock, writeFileMock, "", DocuHelperData{IsGenerateDocu: false})
t.Run("step code", func(t *testing.T) {
goldenFilePath := filepath.Join("testdata", t.Name()+"_generated.golden")
expected, err := ioutil.ReadFile(goldenFilePath)
if err != nil {
t.Fatalf("failed reading %v", goldenFilePath)
}
assert.Equal(t, expected, files["cmd/testStep_generated.go"])
})
t.Run("test code", func(t *testing.T) {
goldenFilePath := filepath.Join("testdata", t.Name()+"_generated.golden")
expected, err := ioutil.ReadFile(goldenFilePath)
if err != nil {
t.Fatalf("failed reading %v", goldenFilePath)
}
assert.Equal(t, expected, files["cmd/testStep_generated_test.go"])
})
}
func TestSetDefaultParameters(t *testing.T) {
t.Run("success case", func(t *testing.T) {
stepData := config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "string", Default: "val0"},
{Name: "param1", Scope: []string{"STEPS"}, Type: "string"},
{Name: "param2", Scope: []string{"STAGES"}, Type: "bool", Default: true},
{Name: "param3", Scope: []string{"PARAMETERS"}, Type: "bool"},
{Name: "param4", Scope: []string{"ENV"}, Type: "[]string", Default: []string{"val4_1", "val4_2"}},
{Name: "param5", Scope: []string{"ENV"}, Type: "[]string"},
},
},
},
}
expected := []string{
"\"val0\"",
"os.Getenv(\"PIPER_param1\")",
"true",
"false",
"[]string{\"val4_1\", \"val4_2\"}",
"[]string{}",
}
osImport, err := setDefaultParameters(&stepData)
assert.NoError(t, err, "error occured but none expected")
assert.Equal(t, true, osImport, "import of os package required")
for k, v := range expected {
assert.Equal(t, v, stepData.Spec.Inputs.Parameters[k].Default, fmt.Sprintf("default not correct for parameter %v", k))
}
})
t.Run("error case", func(t *testing.T) {
stepData := []config.StepData{
{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "int", Default: 10},
{Name: "param1", Scope: []string{"GENERAL"}, Type: "int"},
},
},
},
},
{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param1", Scope: []string{"GENERAL"}, Type: "int"},
},
},
},
},
}
for k, v := range stepData {
_, err := setDefaultParameters(&v)
assert.Error(t, err, fmt.Sprintf("error expected but none occured for parameter %v", k))
}
})
}
func TestGetStepInfo(t *testing.T) {
stepData := config.StepData{
Metadata: config.StepMetadata{
Name: "testStep",
Description: "Test description",
LongDescription: "Long Test description",
},
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "string", Default: "test"},
},
},
},
}
myStepInfo := getStepInfo(&stepData, true, "")
assert.Equal(t, "testStep", myStepInfo.StepName, "StepName incorrect")
assert.Equal(t, "TestStepCommand", myStepInfo.CobraCmdFuncName, "CobraCmdFuncName incorrect")
assert.Equal(t, "createTestStepCmd", myStepInfo.CreateCmdVar, "CreateCmdVar incorrect")
assert.Equal(t, "Test description", myStepInfo.Short, "Short incorrect")
assert.Equal(t, "Long Test description", myStepInfo.Long, "Long incorrect")
assert.Equal(t, stepData.Spec.Inputs.Parameters, myStepInfo.Metadata, "Metadata incorrect")
assert.Equal(t, "addTestStepFlags", myStepInfo.FlagsFunc, "FlagsFunc incorrect")
assert.Equal(t, "addTestStepFlags", myStepInfo.FlagsFunc, "FlagsFunc incorrect")
}
func TestLongName(t *testing.T) {
tt := []struct {
input string
expected string
}{
{input: "my long name with no ticks", expected: "my long name with no ticks"},
{input: "my long name with `ticks`", expected: "my long name with ` + \"`\" + `ticks` + \"`\" + `"},
}
for k, v := range tt {
assert.Equal(t, v.expected, longName(v.input), fmt.Sprintf("wrong long name for run %v", k))
}
}
func TestGolangName(t *testing.T) {
tt := []struct {
input string
expected string
}{
{input: "testApi", expected: "TestAPI"},
{input: "apiTest", expected: "APITest"},
{input: "testUrl", expected: "TestURL"},
{input: "testId", expected: "TestID"},
{input: "testJson", expected: "TestJSON"},
{input: "jsonTest", expected: "JSONTest"},
}
for k, v := range tt {
assert.Equal(t, v.expected, golangName(v.input), fmt.Sprintf("wrong golang name for run %v", k))
}
}
func TestFlagType(t *testing.T) {
tt := []struct {
input string
expected string
}{
{input: "bool", expected: "BoolVar"},
{input: "string", expected: "StringVar"},
{input: "[]string", expected: "StringSliceVar"},
}
for k, v := range tt {
assert.Equal(t, v.expected, flagType(v.input), fmt.Sprintf("wrong flag type for run %v", k))
}
}
package helper
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
func configOpenFileMock(name string) (io.ReadCloser, error) {
meta1 := `metadata:
name: testStep
description: Test description
longDescription: |
Long Test description
spec:
inputs:
params:
- name: param0
type: string
description: param0 description
default: val0
scope:
- GENERAL
- PARAMETERS
mandatory: true
- name: param1
type: string
description: param1 description
scope:
- PARAMETERS
- name: param2
type: string
description: param1 description
scope:
- PARAMETERS
mandatory: true
`
var r string
switch name {
case "test.yaml":
r = meta1
default:
r = ""
}
return ioutil.NopCloser(strings.NewReader(r)), nil
}
var files map[string][]byte
func writeFileMock(filename string, data []byte, perm os.FileMode) error {
if files == nil {
files = make(map[string][]byte)
}
files[filename] = data
return nil
}
func TestProcessMetaFiles(t *testing.T) {
ProcessMetaFiles([]string{"test.yaml"}, configOpenFileMock, writeFileMock, "", DocuHelperData{IsGenerateDocu: false})
t.Run("step code", func(t *testing.T) {
goldenFilePath := filepath.Join("testdata", t.Name()+"_generated.golden")
expected, err := ioutil.ReadFile(goldenFilePath)
if err != nil {
t.Fatalf("failed reading %v", goldenFilePath)
}
assert.Equal(t, expected, files["cmd/testStep_generated.go"])
})
t.Run("test code", func(t *testing.T) {
goldenFilePath := filepath.Join("testdata", t.Name()+"_generated.golden")
expected, err := ioutil.ReadFile(goldenFilePath)
if err != nil {
t.Fatalf("failed reading %v", goldenFilePath)
}
assert.Equal(t, expected, files["cmd/testStep_generated_test.go"])
})
}
func TestSetDefaultParameters(t *testing.T) {
t.Run("success case", func(t *testing.T) {
stepData := config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "string", Default: "val0"},
{Name: "param1", Scope: []string{"STEPS"}, Type: "string"},
{Name: "param2", Scope: []string{"STAGES"}, Type: "bool", Default: true},
{Name: "param3", Scope: []string{"PARAMETERS"}, Type: "bool"},
{Name: "param4", Scope: []string{"ENV"}, Type: "[]string", Default: []string{"val4_1", "val4_2"}},
{Name: "param5", Scope: []string{"ENV"}, Type: "[]string"},
},
},
},
}
expected := []string{
"\"val0\"",
"os.Getenv(\"PIPER_param1\")",
"true",
"false",
"[]string{\"val4_1\", \"val4_2\"}",
"[]string{}",
}
osImport, err := setDefaultParameters(&stepData)
assert.NoError(t, err, "error occured but none expected")
assert.Equal(t, true, osImport, "import of os package required")
for k, v := range expected {
assert.Equal(t, v, stepData.Spec.Inputs.Parameters[k].Default, fmt.Sprintf("default not correct for parameter %v", k))
}
})
t.Run("error case", func(t *testing.T) {
stepData := []config.StepData{
{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "int", Default: 10},
{Name: "param1", Scope: []string{"GENERAL"}, Type: "int"},
},
},
},
},
{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param1", Scope: []string{"GENERAL"}, Type: "int"},
},
},
},
},
}
for k, v := range stepData {
_, err := setDefaultParameters(&v)
assert.Error(t, err, fmt.Sprintf("error expected but none occured for parameter %v", k))
}
})
}
func TestGetStepInfo(t *testing.T) {
stepData := config.StepData{
Metadata: config.StepMetadata{
Name: "testStep",
Description: "Test description",
LongDescription: "Long Test description",
},
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "string", Default: "test"},
},
},
},
}
myStepInfo := getStepInfo(&stepData, true, "")
assert.Equal(t, "testStep", myStepInfo.StepName, "StepName incorrect")
assert.Equal(t, "TestStepCommand", myStepInfo.CobraCmdFuncName, "CobraCmdFuncName incorrect")
assert.Equal(t, "createTestStepCmd", myStepInfo.CreateCmdVar, "CreateCmdVar incorrect")
assert.Equal(t, "Test description", myStepInfo.Short, "Short incorrect")
assert.Equal(t, "Long Test description", myStepInfo.Long, "Long incorrect")
assert.Equal(t, stepData.Spec.Inputs.Parameters, myStepInfo.Metadata, "Metadata incorrect")
assert.Equal(t, "addTestStepFlags", myStepInfo.FlagsFunc, "FlagsFunc incorrect")
assert.Equal(t, "addTestStepFlags", myStepInfo.FlagsFunc, "FlagsFunc incorrect")
}
func TestLongName(t *testing.T) {
tt := []struct {
input string
expected string
}{
{input: "my long name with no ticks", expected: "my long name with no ticks"},
{input: "my long name with `ticks`", expected: "my long name with ` + \"`\" + `ticks` + \"`\" + `"},
}
for k, v := range tt {
assert.Equal(t, v.expected, longName(v.input), fmt.Sprintf("wrong long name for run %v", k))
}
}
func TestGolangName(t *testing.T) {
tt := []struct {
input string
expected string
}{
{input: "testApi", expected: "TestAPI"},
{input: "apiTest", expected: "APITest"},
{input: "testUrl", expected: "TestURL"},
{input: "testId", expected: "TestID"},
{input: "testJson", expected: "TestJSON"},
{input: "jsonTest", expected: "JSONTest"},
}
for k, v := range tt {
assert.Equal(t, v.expected, golangName(v.input), fmt.Sprintf("wrong golang name for run %v", k))
}
}
func TestFlagType(t *testing.T) {
tt := []struct {
input string
expected string
}{
{input: "bool", expected: "BoolVar"},
{input: "string", expected: "StringVar"},
{input: "[]string", expected: "StringSliceVar"},
}
for k, v := range tt {
assert.Equal(t, v.expected, flagType(v.input), fmt.Sprintf("wrong flag type for run %v", k))
}
}

View File

@ -36,4 +36,3 @@ params:
description: as dockerWorkspace for the sidecar container
- name: stashContent
description: Specific stashes that should be considered for the step execution.

View File

@ -53,7 +53,7 @@ func openDocTemplate(docTemplateFilePath string) (io.ReadCloser, error) {
//check if template exists otherwise print No Template found
if _, err := os.Stat(docTemplateFilePath); os.IsNotExist(err) {
err := fmt.Errorf("No Template found: %v \n", docTemplateFilePath)
err := fmt.Errorf("no template found: %v", docTemplateFilePath)
return nil, err
}