1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/pipelineCreateScanSummary_test.go
Oliver Nocon c088e623aa
feat(pipelineCreateSummary): enhance content with reference link (#2837)
* feat(pipelineCreateSummary): enhance content with reference link

* Update cmd/pipelineCreateScanSummary.go

* Update cmd/pipelineCreateScanSummary.go
2021-05-19 07:57:44 +02:00

131 lines
3.8 KiB
Go

package cmd
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type pipelineCreateScanSummaryMockUtils struct {
*mock.FilesMock
}
func newPipelineCreateScanSummaryTestsUtils() pipelineCreateScanSummaryMockUtils {
utils := pipelineCreateScanSummaryMockUtils{
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunPipelineCreateScanSummary(t *testing.T) {
t.Parallel()
t.Run("success", func(t *testing.T) {
t.Parallel()
config := pipelineCreateScanSummaryOptions{
OutputFilePath: "scanSummary.md",
}
utils := newPipelineCreateScanSummaryTestsUtils()
utils.AddFile(".pipeline/stepReports/step1.json", []byte(`{"title":"Title Scan 1"}`))
utils.AddFile(".pipeline/stepReports/step2.json", []byte(`{"title":"Title Scan 2"}`))
utils.AddFile(".pipeline/stepReports/step3.json", []byte(`{"title":"Title Scan 3"}`))
err := runPipelineCreateScanSummary(&config, nil, utils)
assert.NoError(t, err)
reportExists, _ := utils.FileExists("scanSummary.md")
assert.True(t, reportExists)
fileContent, _ := utils.FileRead("scanSummary.md")
fileContentString := string(fileContent)
assert.Contains(t, fileContentString, "Title Scan 1")
assert.Contains(t, fileContentString, "Title Scan 2")
assert.Contains(t, fileContentString, "Title Scan 3")
})
t.Run("success - failed only", func(t *testing.T) {
t.Parallel()
config := pipelineCreateScanSummaryOptions{
OutputFilePath: "scanSummary.md",
FailedOnly: true,
}
utils := newPipelineCreateScanSummaryTestsUtils()
utils.AddFile(".pipeline/stepReports/step1.json", []byte(`{"title":"Title Scan 1", "successfulScan": true}`))
utils.AddFile(".pipeline/stepReports/step2.json", []byte(`{"title":"Title Scan 2", "successfulScan": false}`))
utils.AddFile(".pipeline/stepReports/step3.json", []byte(`{"title":"Title Scan 3", "successfulScan": false}`))
err := runPipelineCreateScanSummary(&config, nil, utils)
assert.NoError(t, err)
reportExists, _ := utils.FileExists("scanSummary.md")
assert.True(t, reportExists)
fileContent, _ := utils.FileRead("scanSummary.md")
fileContentString := string(fileContent)
assert.NotContains(t, fileContentString, "Title Scan 1")
assert.Contains(t, fileContentString, "Title Scan 2")
assert.Contains(t, fileContentString, "Title Scan 3")
})
t.Run("success - with source link", func(t *testing.T) {
t.Parallel()
config := pipelineCreateScanSummaryOptions{
OutputFilePath: "scanSummary.md",
PipelineLink: "https://test.com/link",
}
utils := newPipelineCreateScanSummaryTestsUtils()
err := runPipelineCreateScanSummary(&config, nil, utils)
assert.NoError(t, err)
reportExists, _ := utils.FileExists("scanSummary.md")
assert.True(t, reportExists)
fileContent, _ := utils.FileRead("scanSummary.md")
fileContentString := string(fileContent)
assert.Contains(t, fileContentString, "https://test.com/link")
})
t.Run("error - read file", func(t *testing.T) {
t.Skip()
//ToDo
// so far mock cannot create error for reading files
config := pipelineCreateScanSummaryOptions{
OutputFilePath: "scanSummary.md",
}
utils := newPipelineCreateScanSummaryTestsUtils()
err := runPipelineCreateScanSummary(&config, nil, utils)
assert.Contains(t, fmt.Sprint(err), "failed to read report")
})
t.Run("error - unmarshal json", func(t *testing.T) {
t.Parallel()
config := pipelineCreateScanSummaryOptions{
OutputFilePath: "scanSummary.md",
}
utils := newPipelineCreateScanSummaryTestsUtils()
utils.AddFile(".pipeline/stepReports/step1.json", []byte(`{"title":"Title Scan 1"`))
err := runPipelineCreateScanSummary(&config, nil, utils)
assert.Contains(t, fmt.Sprint(err), "failed to parse report")
})
t.Run("error - write file", func(t *testing.T) {
//ToDo
// so far mock cannot create error
})
}