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
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

134 lines
3.8 KiB
Go

//go:build unit
// +build unit
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
})
}