github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/test/integration/node_packages_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/scylladb/go-set/strset"
     9  
    10  	"github.com/anchore/syft/syft/pkg"
    11  )
    12  
    13  func TestNpmPackageLockDirectory(t *testing.T) {
    14  	sbom, _ := catalogDirectory(t, "test-fixtures/npm-lock")
    15  
    16  	foundPackages := strset.New()
    17  
    18  	for actualPkg := range sbom.Artifacts.Packages.Enumerate(pkg.NpmPkg) {
    19  		for _, actualLocation := range actualPkg.Locations.ToSlice() {
    20  			if strings.Contains(actualLocation.RealPath, "node_modules") {
    21  				t.Errorf("found packages from package-lock.json in node_modules: %s", actualLocation)
    22  			}
    23  		}
    24  		foundPackages.Add(actualPkg.Name)
    25  	}
    26  
    27  	// ensure that integration test commonTestCases stay in sync with the available catalogers
    28  	const expectedPackageCount = 6
    29  	if foundPackages.Size() != expectedPackageCount {
    30  		t.Errorf("found the wrong set of npm package-lock.json packages (expected: %d, actual: %d)", expectedPackageCount, foundPackages.Size())
    31  	}
    32  }
    33  
    34  func TestYarnPackageLockDirectory(t *testing.T) {
    35  	sbom, _ := catalogDirectory(t, "test-fixtures/yarn-lock")
    36  
    37  	foundPackages := strset.New()
    38  	expectedPackages := strset.New("async@0.9.2", "async@3.2.3", "merge-objects@1.0.5", "should-type@1.3.0", "@4lolo/resize-observer-polyfill@1.5.2")
    39  
    40  	for actualPkg := range sbom.Artifacts.Packages.Enumerate(pkg.NpmPkg) {
    41  		for _, actualLocation := range actualPkg.Locations.ToSlice() {
    42  			if strings.Contains(actualLocation.RealPath, "node_modules") {
    43  				t.Errorf("found packages from yarn.lock in node_modules: %s", actualLocation)
    44  			}
    45  		}
    46  		foundPackages.Add(actualPkg.Name + "@" + actualPkg.Version)
    47  	}
    48  
    49  	// ensure that integration test commonTestCases stay in sync with the available catalogers
    50  	if foundPackages.Size() != expectedPackages.Size() {
    51  		t.Errorf("found the wrong set of yarn.lock packages (expected: %d, actual: %d)", expectedPackages.Size(), foundPackages.Size())
    52  	} else if !reflect.DeepEqual(foundPackages, expectedPackages) {
    53  		t.Errorf("found the wrong set of yarn.lock packages (expected: %+q, actual: %+q)", expectedPackages.List(), foundPackages.List())
    54  	}
    55  }