github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/test/integration/node_packages_test.go (about)

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