/
export_test.go
87 lines (73 loc) · 2.07 KB
/
export_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package advisory
import (
"context"
"io"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/wolfi-dev/wolfictl/pkg/configs"
v2 "github.com/wolfi-dev/wolfictl/pkg/configs/advisory/v2"
rwos "github.com/wolfi-dev/wolfictl/pkg/configs/rwfs/os"
)
func Test_ExportFuncs(t *testing.T) {
const testdataDir = "./testdata/export/advisories"
cases := []struct {
name string
exportFuncUnderTest func(ExportOptions) (io.Reader, error)
pathToExpectedData string
}{
{
name: "csv",
exportFuncUnderTest: ExportCSV,
pathToExpectedData: "./testdata/export/expected.csv",
},
{
name: "yaml",
exportFuncUnderTest: ExportYAML,
pathToExpectedData: "./testdata/export/expected.yaml",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
advisoryFsys := rwos.DirFS(testdataDir)
advisoryDocs, err := v2.NewIndex(context.Background(), advisoryFsys)
require.NoError(t, err)
indices := []*configs.Index[v2.Document]{advisoryDocs}
opts := ExportOptions{
AdvisoryDocIndices: indices,
}
exported, err := tt.exportFuncUnderTest(opts)
assert.NoError(t, err)
exportedBytes, err := io.ReadAll(exported)
require.NoError(t, err)
if p := tt.pathToExpectedData; p != "" {
expectedBytes, err := os.ReadFile(p)
require.NoError(t, err)
if diff := cmp.Diff(string(expectedBytes), string(exportedBytes)); diff != "" {
t.Errorf("ExportCSV() produced unexpected data (-want +got):\n%s", diff)
}
}
})
}
}
func TestImportAdvisoriesYAML(t *testing.T) {
cases := []struct {
name string
pathToInputData string
}{
{
name: "test-yaml",
pathToInputData: "./testdata/export/expected.yaml",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
tempDir, importedDocuments, err := ImporAdvisoriesYAML(tt.pathToInputData)
require.NoError(t, err)
require.Equal(t, 3, importedDocuments.Select().Len())
defer os.RemoveAll(tempDir)
})
}
}