github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/internal/task/file_tasks_test.go (about)

     1  package task
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/internal/sbomsync"
     9  	"github.com/anchore/syft/syft/artifact"
    10  	"github.com/anchore/syft/syft/file"
    11  	"github.com/anchore/syft/syft/pkg"
    12  	"github.com/anchore/syft/syft/sbom"
    13  )
    14  
    15  func Test_coordinatesForSelection(t *testing.T) {
    16  
    17  	tests := []struct {
    18  		name      string
    19  		selection file.Selection
    20  		sbom      *sbom.SBOM
    21  		files     []file.Coordinates
    22  		ok        bool
    23  	}{
    24  		{
    25  			name:      "all files",
    26  			selection: file.AllFilesSelection,
    27  			files:     nil,
    28  			ok:        true,
    29  		},
    30  		{
    31  			name:      "no files",
    32  			selection: file.NoFilesSelection,
    33  			files:     nil,
    34  			ok:        false,
    35  		},
    36  		{
    37  			name:      "specific files with hits",
    38  			selection: file.FilesOwnedByPackageSelection,
    39  			sbom: &sbom.SBOM{
    40  				Relationships: []artifact.Relationship{
    41  					{
    42  						From: pkg.Package{},
    43  						To: file.Coordinates{
    44  							RealPath:     "path",
    45  							FileSystemID: "fs",
    46  						},
    47  						Type: artifact.ContainsRelationship,
    48  					},
    49  				},
    50  			},
    51  			files: []file.Coordinates{
    52  				{
    53  					RealPath:     "path",
    54  					FileSystemID: "fs",
    55  				},
    56  			},
    57  			ok: true,
    58  		},
    59  		{
    60  			name:      "specific files no hits (by wrong type)",
    61  			selection: file.FilesOwnedByPackageSelection,
    62  			sbom: &sbom.SBOM{
    63  				Relationships: []artifact.Relationship{
    64  					{
    65  						From: pkg.Package{},
    66  						To: file.Coordinates{
    67  							RealPath:     "path",
    68  							FileSystemID: "fs",
    69  						},
    70  						// wrong type
    71  						Type: artifact.DependencyOfRelationship,
    72  					},
    73  				},
    74  			},
    75  			files: nil,
    76  			ok:    false,
    77  		},
    78  		{
    79  			name:      "specific files no hits (by wrong node types)",
    80  			selection: file.FilesOwnedByPackageSelection,
    81  			sbom: &sbom.SBOM{
    82  				Relationships: []artifact.Relationship{
    83  					{
    84  						From: file.Coordinates{}, // wrong type
    85  						To: file.Coordinates{
    86  							RealPath:     "path",
    87  							FileSystemID: "fs",
    88  						},
    89  						Type: artifact.ContainsRelationship,
    90  					},
    91  				},
    92  			},
    93  			files: nil,
    94  			ok:    false,
    95  		},
    96  	}
    97  	for _, tt := range tests {
    98  		t.Run(tt.name, func(t *testing.T) {
    99  			files, ok := coordinatesForSelection(tt.selection, sbomsync.NewBuilder(tt.sbom).(sbomsync.Accessor))
   100  			assert.Equal(t, tt.files, files)
   101  			assert.Equal(t, tt.ok, ok)
   102  		})
   103  	}
   104  }