github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/test/integration/node_packages_test.go (about)

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