github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/r/parse_description_test.go (about)

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