github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/r/parse_description_test.go (about) 1 package r 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/anchore/syft/syft/file" 13 "github.com/anchore/syft/syft/internal/fileresolver" 14 "github.com/anchore/syft/syft/pkg" 15 ) 16 17 func Test_parseDescriptionFile(t *testing.T) { 18 type packageAssertions []func(*testing.T, []pkg.Package) 19 tests := []struct { 20 name string 21 assertions packageAssertions 22 fixture string 23 }{ 24 { 25 name: "no package is returned if no version found", 26 fixture: filepath.Join("test-fixtures", "map-parse", "no-version"), 27 assertions: packageAssertions{ 28 func(t *testing.T, p []pkg.Package) { 29 assert.Empty(t, p) 30 }, 31 }, 32 }, 33 { 34 name: "no package is returned if no package name found", 35 fixture: filepath.Join("test-fixtures", "map-parse", "no-name"), 36 assertions: packageAssertions{ 37 func(t *testing.T, p []pkg.Package) { 38 assert.Empty(t, p) 39 }, 40 }, 41 }, 42 { 43 name: "package return if both name and version found", 44 fixture: filepath.Join("test-fixtures", "map-parse", "simple"), 45 assertions: packageAssertions{ 46 func(t *testing.T, p []pkg.Package) { 47 assert.Equal(t, 1, len(p)) 48 assert.Equal(t, "base", p[0].Name) 49 assert.Equal(t, "4.3.0", p[0].Version) 50 }, 51 }, 52 }, 53 } 54 55 for _, tt := range tests { 56 t.Run(tt.name, func(t *testing.T) { 57 f, err := os.Open(tt.fixture) 58 input := file.LocationReadCloser{ 59 Location: file.NewLocation(tt.fixture), 60 ReadCloser: f, 61 } 62 got, _, err := parseDescriptionFile(context.Background(), fileresolver.Empty{}, nil, input) 63 assert.NoError(t, err) 64 for _, assertion := range tt.assertions { 65 assertion(t, got) 66 } 67 }) 68 } 69 } 70 71 func Test_extractFieldsFromDescriptionFile(t *testing.T) { 72 tests := []struct { 73 name string 74 fixture string 75 want map[string]string 76 }{ 77 { 78 name: "go case", 79 fixture: "test-fixtures/map-parse/simple", 80 want: map[string]string{ 81 "Package": "base", 82 "Version": "4.3.0", 83 "Suggests": "methods", 84 "Built": "R 4.3.0; ; 2023-04-21 11:33:09 UTC; unix", 85 }, 86 }, 87 { 88 name: "bad cases", 89 fixture: "test-fixtures/map-parse/bad", 90 want: map[string]string{ 91 "Key": "", 92 "Whitespace": "", 93 }, 94 }, 95 { 96 name: "multiline key-value", 97 fixture: "test-fixtures/map-parse/multiline", 98 want: map[string]string{ 99 "Description": `A consistent, simple and easy to use set of wrappers around 100 the fantastic 'stringi' package. All function and argument names (and 101 positions) are consistent, all functions deal with "NA"'s and zero 102 length vectors in the same way, and the output from one function is 103 easy to feed into the input of another.`, 104 "License": "MIT + file LICENSE", 105 "Key": "value", 106 }, 107 }, 108 { 109 name: "eof multiline", 110 fixture: "test-fixtures/map-parse/eof-multiline", 111 want: map[string]string{ 112 "License": "MIT + file LICENSE", 113 "Description": `A consistent, simple and easy to use set of wrappers around 114 the fantastic 'stringi' package. All function and argument names (and 115 positions) are consistent, all functions deal with "NA"'s and zero 116 length vectors in the same way, and the output from one function is 117 easy to feed into the input of another.`, 118 }, 119 }, 120 } 121 122 for _, test := range tests { 123 t.Run(test.name, func(t *testing.T) { 124 file, err := os.Open(test.fixture) 125 require.NoError(t, err) 126 127 result := extractFieldsFromDescriptionFile(file) 128 129 assert.Equal(t, test.want, result) 130 }) 131 } 132 133 }