github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/swift/parse_package_resolved_test.go (about)

     1  package swift
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/anchore/syft/syft/artifact"
    11  	"github.com/anchore/syft/syft/file"
    12  	"github.com/anchore/syft/syft/pkg"
    13  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    14  )
    15  
    16  func TestParsePackageResolved(t *testing.T) {
    17  	fixture := "test-fixtures/Package.resolved"
    18  	locations := file.NewLocationSet(file.NewLocation(fixture))
    19  	expectedPkgs := []pkg.Package{
    20  		{
    21  			Name:      "swift-algorithms",
    22  			Version:   "1.0.0",
    23  			PURL:      "pkg:swift/github.com/apple/swift-algorithms.git/swift-algorithms@1.0.0",
    24  			Locations: locations,
    25  			Language:  pkg.Swift,
    26  			Type:      pkg.SwiftPkg,
    27  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
    28  				Revision: "b14b7f4c528c942f121c8b860b9410b2bf57825e",
    29  			},
    30  		},
    31  		{
    32  			Name:      "swift-async-algorithms",
    33  			Version:   "0.1.0",
    34  			PURL:      "pkg:swift/github.com/apple/swift-async-algorithms.git/swift-async-algorithms@0.1.0",
    35  			Locations: locations,
    36  			Language:  pkg.Swift,
    37  			Type:      pkg.SwiftPkg,
    38  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
    39  				Revision: "9cfed92b026c524674ed869a4ff2dcfdeedf8a2a",
    40  			},
    41  		},
    42  		{
    43  			Name:      "swift-atomics",
    44  			Version:   "1.1.0",
    45  			PURL:      "pkg:swift/github.com/apple/swift-atomics.git/swift-atomics@1.1.0",
    46  			Locations: locations,
    47  			Language:  pkg.Swift,
    48  			Type:      pkg.SwiftPkg,
    49  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
    50  				Revision: "6c89474e62719ddcc1e9614989fff2f68208fe10",
    51  			},
    52  		},
    53  		{
    54  			Name:      "swift-collections",
    55  			Version:   "1.0.4",
    56  			PURL:      "pkg:swift/github.com/apple/swift-collections.git/swift-collections@1.0.4",
    57  			Locations: locations,
    58  			Language:  pkg.Swift,
    59  			Type:      pkg.SwiftPkg,
    60  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
    61  				Revision: "937e904258d22af6e447a0b72c0bc67583ef64a2",
    62  			},
    63  		},
    64  		{
    65  			Name:      "swift-numerics",
    66  			Version:   "1.0.2",
    67  			PURL:      "pkg:swift/github.com/apple/swift-numerics/swift-numerics@1.0.2",
    68  			Locations: locations,
    69  			Language:  pkg.Swift,
    70  			Type:      pkg.SwiftPkg,
    71  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
    72  				Revision: "0a5bc04095a675662cf24757cc0640aa2204253b",
    73  			},
    74  		},
    75  	}
    76  
    77  	// TODO: no relationships are under test yet
    78  	var expectedRelationships []artifact.Relationship
    79  
    80  	pkgtest.TestFileParser(t, fixture, parsePackageResolved, expectedPkgs, expectedRelationships)
    81  }
    82  
    83  func TestParsePackageResolvedV3(t *testing.T) {
    84  	fixture := "test-fixtures/PackageV3.resolved"
    85  	locations := file.NewLocationSet(file.NewLocation(fixture))
    86  	expectedPkgs := []pkg.Package{
    87  		{
    88  			Name:      "swift-mmio",
    89  			Version:   "",
    90  			PURL:      "pkg:swift/github.com/apple/swift-mmio/swift-mmio",
    91  			Locations: locations,
    92  			Language:  pkg.Swift,
    93  			Type:      pkg.SwiftPkg,
    94  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
    95  				Revision: "80c109b87511041338a4d8d88064088c8dfc079b",
    96  			},
    97  		},
    98  		{
    99  			Name:      "swift-syntax",
   100  			Version:   "509.1.1",
   101  			PURL:      "pkg:swift/github.com/apple/swift-syntax.git/swift-syntax@509.1.1",
   102  			Locations: locations,
   103  			Language:  pkg.Swift,
   104  			Type:      pkg.SwiftPkg,
   105  			Metadata: pkg.SwiftPackageManagerResolvedEntry{
   106  				Revision: "64889f0c732f210a935a0ad7cda38f77f876262d",
   107  			},
   108  		},
   109  	}
   110  
   111  	// TODO: no relationships are under test yet
   112  	var expectedRelationships []artifact.Relationship
   113  
   114  	pkgtest.TestFileParser(t, fixture, parsePackageResolved, expectedPkgs, expectedRelationships)
   115  }
   116  
   117  func TestParsePackageResolved_empty(t *testing.T) {
   118  	// regression for https://github.com/anchore/syft/issues/2225
   119  	fixture := "test-fixtures/empty-packages.resolved"
   120  
   121  	pkgtest.TestFileParser(t, fixture, parsePackageResolved, nil, nil)
   122  
   123  	dir := t.TempDir()
   124  	fixture = filepath.Join(dir, "Package.resolved")
   125  	_, err := os.Create(fixture)
   126  	require.NoError(t, err)
   127  
   128  	pkgtest.TestFileParser(t, fixture, parsePackageResolved, nil, nil)
   129  }
   130  
   131  func TestParsePackageResolved_versionNotANumber(t *testing.T) {
   132  	// regression for https://github.com/anchore/syft/issues/2225
   133  	fixture := "test-fixtures/bad-version-packages.resolved"
   134  
   135  	pkgtest.NewCatalogTester().FromFile(t, fixture).WithError().TestParser(t, parsePackageResolved)
   136  }
   137  
   138  func Test_corruptPackageResolved(t *testing.T) {
   139  	pkgtest.NewCatalogTester().
   140  		FromFile(t, "test-fixtures/bad-version-packages.resolved").
   141  		WithError().
   142  		TestParser(t, parsePackageResolved)
   143  }