1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/pkg/piperenv/CPEMap_test.go
Kevin Stiehl 6c7814e4d5
feat(cpm): Add read and write CPE Go step (#2888)
* add read write cpe go steps

* Update pkg/piperenv/CPEMap.go

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Update pkg/piperenv/CPEMap.go

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Update pkg/piperenv/environment.go

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* rename file

* add error handling

* add error handling

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2021-06-11 16:17:40 +02:00

87 lines
1.9 KiB
Go

package piperenv
import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path"
"testing"
)
func Test_writeMapToDisk(t *testing.T) {
t.Parallel()
testMap := CPEMap{
"A/B": "Hallo",
"sub": map[string]interface{}{
"A/B": "Test",
},
"number": 5,
}
tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*")
assert.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
err = testMap.WriteToDisk(tmpDir)
assert.NoError(t, err)
testData := []struct {
Path string
ExpectedValue string
}{
{
Path: "A/B",
ExpectedValue: "Hallo",
},
{
Path: "sub.json",
ExpectedValue: "{\"A/B\":\"Test\"}",
},
{
Path: "number.json",
ExpectedValue: "5",
},
}
for _, testCase := range testData {
t.Run(fmt.Sprintf("check path %s", testCase.Path), func(t *testing.T) {
tPath := path.Join(tmpDir, testCase.Path)
bytes, err := ioutil.ReadFile(tPath)
assert.NoError(t, err)
assert.Equal(t, testCase.ExpectedValue, string(bytes))
})
}
}
func TestCPEMap_LoadFromDisk(t *testing.T) {
t.Parallel()
tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*")
assert.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(tmpDir)
})
err = ioutil.WriteFile(path.Join(tmpDir, "Foo"), []byte("Bar"), 0644)
assert.NoError(t, err)
err = ioutil.WriteFile(path.Join(tmpDir, "Hello"), []byte("World"), 0644)
assert.NoError(t, err)
subPath := path.Join(tmpDir, "Batman")
err = os.Mkdir(subPath, 0744)
assert.NoError(t, err)
err = ioutil.WriteFile(path.Join(subPath, "Bruce"), []byte("Wayne"), 0644)
assert.NoError(t, err)
err = ioutil.WriteFile(path.Join(subPath, "Test.json"), []byte("54"), 0644)
assert.NoError(t, err)
cpe := CPEMap{}
err = cpe.LoadFromDisk(tmpDir)
assert.NoError(t, err)
assert.Equal(t, "Bar", cpe["Foo"])
assert.Equal(t, "World", cpe["Hello"])
assert.Equal(t, "Wayne", cpe["Batman/Bruce"])
assert.Equal(t, float64(54), cpe["Batman/Test"])
}