1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/piperenv/environment_test.go
Eng Zer Jun 0f4e30e9db
test: use T.TempDir to create temporary test directory (#3721)
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>
2022-07-12 15:19:12 +02:00

93 lines
2.8 KiB
Go

package piperenv
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSetResourceParameter(t *testing.T) {
type args struct {
path string
resourceName string
paramName string
value interface{}
}
tests := []struct {
name string
want string
args args
}{
{name: "string", want: "testVal", args: args{path: "", resourceName: "testRes", paramName: "testParamString", value: "testVal"}},
{name: "boolean", want: "true", args: args{path: "", resourceName: "testRes", paramName: "testParamBool", value: true}},
{name: "integer", want: "1", args: args{path: "", resourceName: "testRes", paramName: "testParamInt", value: 1}},
{name: "float", want: "0.123", args: args{path: "", resourceName: "testRes", paramName: "testParamFloat", value: 0.123}},
{name: "string list", want: "[\"test\",\"abc\"]", args: args{path: "", resourceName: "testRes", paramName: "testParamStringList", value: []string{"test", "abc"}}},
{name: "boolean list", want: "[true,false]", args: args{path: "", resourceName: "testRes", paramName: "testParamBoolList", value: []bool{true, false}}},
{name: "integer list", want: "[1,2]", args: args{path: "", resourceName: "testRes", paramName: "testParamIntList", value: []int{1, 2}}},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
// init
dir := t.TempDir()
targetFile := filepath.Join(dir, testCase.args.resourceName, testCase.args.paramName)
// test
err := SetResourceParameter(dir, testCase.args.resourceName, testCase.args.paramName, testCase.args.value)
// assert
assert.NoError(t, err)
var v []byte
// var err error
switch testCase.args.value.(type) {
case string:
default:
targetFile += ".json"
}
assert.FileExists(t, targetFile)
v, err = ioutil.ReadFile(targetFile)
require.NoError(t, err)
assert.Equal(t, testCase.want, string(v))
})
}
}
func TestGetResourceParameter(t *testing.T) {
type args struct {
path string
resourceName string
paramName string
}
tests := []struct {
name string
args args
want string
}{
{name: "default", args: args{path: "", resourceName: "", paramName: ""}, want: ""},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
// test
result := GetResourceParameter(testCase.args.path, testCase.args.resourceName, testCase.args.paramName)
// assert
assert.Equal(t, testCase.want, result)
})
}
}
func TestSetParameter(t *testing.T) {
dir := t.TempDir()
err := SetParameter(dir, "testParam", "testVal")
assert.NoError(t, err, "Error occurred but none expected")
assert.Equal(t, "testVal", GetParameter(dir, "testParam"))
}
func TestReadFromDisk(t *testing.T) {
dir := t.TempDir()
assert.Equal(t, "", GetParameter(dir, "testParamNotExistingYet"))
}