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