github.com/jaylevin/jenkins-library@v1.230.4/pkg/piperenv/CPEMap_test.go (about) 1 package piperenv 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 func Test_writeMapToDisk(t *testing.T) { 15 t.Parallel() 16 testMap := CPEMap{ 17 "A/B": "Hallo", 18 "sub": map[string]interface{}{ 19 "A/B": "Test", 20 }, 21 "number": 5, 22 } 23 24 tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*") 25 require.NoError(t, err) 26 t.Cleanup(func() { 27 os.RemoveAll(tmpDir) 28 }) 29 err = testMap.WriteToDisk(tmpDir) 30 require.NoError(t, err) 31 32 testData := []struct { 33 Path string 34 ExpectedValue string 35 }{ 36 { 37 Path: "A/B", 38 ExpectedValue: "Hallo", 39 }, 40 { 41 Path: "sub.json", 42 ExpectedValue: "{\"A/B\":\"Test\"}", 43 }, 44 { 45 Path: "number.json", 46 ExpectedValue: "5", 47 }, 48 } 49 50 for _, testCase := range testData { 51 t.Run(fmt.Sprintf("check path %s", testCase.Path), func(t *testing.T) { 52 tPath := path.Join(tmpDir, testCase.Path) 53 bytes, err := ioutil.ReadFile(tPath) 54 require.NoError(t, err) 55 require.Equal(t, testCase.ExpectedValue, string(bytes)) 56 }) 57 } 58 } 59 60 func TestCPEMap_LoadFromDisk(t *testing.T) { 61 t.Parallel() 62 tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*") 63 require.NoError(t, err) 64 t.Cleanup(func() { 65 os.RemoveAll(tmpDir) 66 }) 67 68 err = ioutil.WriteFile(path.Join(tmpDir, "Foo"), []byte("Bar"), 0644) 69 require.NoError(t, err) 70 err = ioutil.WriteFile(path.Join(tmpDir, "Hello"), []byte("World"), 0644) 71 require.NoError(t, err) 72 subPath := path.Join(tmpDir, "Batman") 73 err = os.Mkdir(subPath, 0744) 74 require.NoError(t, err) 75 err = ioutil.WriteFile(path.Join(subPath, "Bruce"), []byte("Wayne"), 0644) 76 require.NoError(t, err) 77 err = ioutil.WriteFile(path.Join(subPath, "Robin"), []byte("toBeEmptied"), 0644) 78 require.NoError(t, err) 79 err = ioutil.WriteFile(path.Join(subPath, "Test.json"), []byte("54"), 0644) 80 require.NoError(t, err) 81 82 cpe := CPEMap{} 83 err = cpe.LoadFromDisk(tmpDir) 84 require.NoError(t, err) 85 86 require.Equal(t, "Bar", cpe["Foo"]) 87 require.Equal(t, "World", cpe["Hello"]) 88 require.Equal(t, "", cpe["Batman/Robin"]) 89 require.Equal(t, "Wayne", cpe["Batman/Bruce"]) 90 require.Equal(t, json.Number("54"), cpe["Batman/Test"]) 91 } 92 93 func TestNumbersArePassedCorrectly(t *testing.T) { 94 t.Parallel() 95 tmpDir, err := ioutil.TempDir(os.TempDir(), "test-data-*") 96 require.NoError(t, err) 97 t.Cleanup(func() { 98 os.RemoveAll(tmpDir) 99 }) 100 101 const jsonNumber = "5.5000" 102 err = ioutil.WriteFile(path.Join(tmpDir, "test.json"), []byte(jsonNumber), 0644) 103 require.NoError(t, err) 104 105 cpeMap := CPEMap{} 106 err = cpeMap.LoadFromDisk(tmpDir) 107 require.NoError(t, err) 108 109 rawJSON, err := json.Marshal(cpeMap["test"]) 110 require.NoError(t, err) 111 require.Equal(t, jsonNumber, string(rawJSON)) 112 } 113 114 func TestCommonPipelineEnvDirNotPresent(t *testing.T) { 115 cpe := CPEMap{} 116 err := cpe.LoadFromDisk("/path/does/not/exist") 117 require.NoError(t, err) 118 require.Len(t, cpe, 0) 119 }