mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
0f4e30e9db
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `ioutil.TempDir` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
75 lines
2.8 KiB
Go
75 lines
2.8 KiB
Go
package piperutils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPersistReportAndLinks(t *testing.T) {
|
|
t.Run("default", func(t *testing.T) {
|
|
workspace := t.TempDir()
|
|
|
|
reports := []Path{{Target: "testFile1.json", Mandatory: true}, {Target: "testFile2.json"}}
|
|
links := []Path{{Target: "https://1234568.com/test", Name: "Weblink"}}
|
|
PersistReportsAndLinks("checkmarxExecuteScan", workspace, reports, links)
|
|
|
|
reportsJSONPath := filepath.Join(workspace, "checkmarxExecuteScan_reports.json")
|
|
assert.FileExists(t, reportsJSONPath)
|
|
|
|
linksJSONPath := filepath.Join(workspace, "checkmarxExecuteScan_links.json")
|
|
assert.FileExists(t, linksJSONPath)
|
|
|
|
var reportsLoaded []Path
|
|
var linksLoaded []Path
|
|
reportsFileData, err := ioutil.ReadFile(reportsJSONPath)
|
|
reportsDataString := string(reportsFileData)
|
|
println(reportsDataString)
|
|
assert.NoError(t, err, "No error expected but got one")
|
|
|
|
linksFileData, err := ioutil.ReadFile(linksJSONPath)
|
|
linksDataString := string(linksFileData)
|
|
println(linksDataString)
|
|
assert.NoError(t, err, "No error expected but got one")
|
|
json.Unmarshal(reportsFileData, &reportsLoaded)
|
|
json.Unmarshal(linksFileData, &linksLoaded)
|
|
|
|
assert.Equal(t, 2, len(reportsLoaded), "wrong number of reports")
|
|
assert.Equal(t, 1, len(linksLoaded), "wrong number of links")
|
|
assert.Equal(t, true, reportsLoaded[0].Mandatory, "mandatory flag on report 1 has wrong value")
|
|
assert.Equal(t, "testFile1.json", reportsLoaded[0].Target, "target value on report 1 has wrong value")
|
|
assert.Equal(t, false, reportsLoaded[1].Mandatory, "mandatory flag on report 2 has wrong value")
|
|
assert.Equal(t, "testFile2.json", reportsLoaded[1].Target, "target value on report 1 has wrong value")
|
|
assert.Equal(t, false, linksLoaded[0].Mandatory, "mandatory flag on link 1 has wrong value")
|
|
assert.Equal(t, "https://1234568.com/test", linksLoaded[0].Target, "target value on link 1 has wrong value")
|
|
assert.Equal(t, "Weblink", linksLoaded[0].Name, "name value on link 1 has wrong value")
|
|
})
|
|
|
|
t.Run("empty list", func(t *testing.T) {
|
|
// init
|
|
workspace := t.TempDir()
|
|
|
|
reportsJSONPath := filepath.Join(workspace, "sonarExecuteScan_reports.json")
|
|
linksJSONPath := filepath.Join(workspace, "sonarExecuteScan_links.json")
|
|
|
|
// prepare uninitialised parameters
|
|
var reports, links []Path
|
|
require.Empty(t, reports)
|
|
require.Empty(t, links)
|
|
|
|
// test
|
|
PersistReportsAndLinks("sonarExecuteScan", workspace, reports, links)
|
|
// assert
|
|
for _, reportFile := range []string{reportsJSONPath, linksJSONPath} {
|
|
assert.FileExists(t, reportFile)
|
|
reportsFileData, err := ioutil.ReadFile(reportFile)
|
|
require.NoError(t, err, "No error expected but got one")
|
|
assert.Equal(t, "[]", string(reportsFileData))
|
|
}
|
|
})
|
|
}
|