2021-06-11 16:17:40 +02:00
|
|
|
package piperenv
|
|
|
|
|
|
|
|
import (
|
2021-06-23 20:20:43 +02:00
|
|
|
"encoding/json"
|
2021-06-11 16:17:40 +02:00
|
|
|
"fmt"
|
2021-06-23 20:20:43 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-11 16:17:40 +02:00
|
|
|
"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-*")
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
})
|
|
|
|
err = testMap.WriteToDisk(tmpDir)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
|
|
|
|
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)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, testCase.ExpectedValue, string(bytes))
|
2021-06-11 16:17:40 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCPEMap_LoadFromDisk(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*")
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
})
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(path.Join(tmpDir, "Foo"), []byte("Bar"), 0644)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
err = ioutil.WriteFile(path.Join(tmpDir, "Hello"), []byte("World"), 0644)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
subPath := path.Join(tmpDir, "Batman")
|
|
|
|
err = os.Mkdir(subPath, 0744)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
err = ioutil.WriteFile(path.Join(subPath, "Bruce"), []byte("Wayne"), 0644)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
err = ioutil.WriteFile(path.Join(subPath, "Test.json"), []byte("54"), 0644)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
|
|
|
|
cpe := CPEMap{}
|
|
|
|
err = cpe.LoadFromDisk(tmpDir)
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-11 16:17:40 +02:00
|
|
|
|
2021-06-23 20:20:43 +02:00
|
|
|
require.Equal(t, "Bar", cpe["Foo"])
|
|
|
|
require.Equal(t, "World", cpe["Hello"])
|
|
|
|
require.Equal(t, "Wayne", cpe["Batman/Bruce"])
|
|
|
|
require.Equal(t, json.Number("54"), cpe["Batman/Test"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNumbersArePassedCorrectly(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
})
|
|
|
|
|
|
|
|
const jsonNumber = "5.5000"
|
|
|
|
err = ioutil.WriteFile(path.Join(tmpDir, "test.json"), []byte(jsonNumber), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cpeMap := CPEMap{}
|
|
|
|
err = cpeMap.LoadFromDisk(tmpDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rawJSON, err := json.Marshal(cpeMap["test"])
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, jsonNumber, string(rawJSON))
|
2021-06-11 16:17:40 +02:00
|
|
|
}
|
2021-06-16 11:46:55 +02:00
|
|
|
|
|
|
|
func TestCommonPipelineEnvDirNotPresent(t *testing.T) {
|
|
|
|
cpe := CPEMap{}
|
|
|
|
err := cpe.LoadFromDisk("/path/does/not/exist")
|
2021-06-23 20:20:43 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, cpe, 0)
|
2021-06-16 11:46:55 +02:00
|
|
|
}
|