github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/javascript/cataloger_test.go (about)

     1  package javascript
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/anchore/syft/syft/file"
     8  	"github.com/anchore/syft/syft/pkg"
     9  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
    10  )
    11  
    12  func Test_JavascriptCataloger(t *testing.T) {
    13  	ctx := context.TODO()
    14  	locationSet := file.NewLocationSet(file.NewLocation("package-lock.json"))
    15  	expectedPkgs := []pkg.Package{
    16  		{
    17  			Name:      "@actions/core",
    18  			Version:   "1.6.0",
    19  			FoundBy:   "javascript-lock-cataloger",
    20  			PURL:      "pkg:npm/%40actions/core@1.6.0",
    21  			Locations: locationSet,
    22  			Language:  pkg.JavaScript,
    23  			Type:      pkg.NpmPkg,
    24  			Licenses: pkg.NewLicenseSet(
    25  				pkg.NewLicenseFromLocationsWithContext(ctx, "MIT", file.NewLocation("package-lock.json")),
    26  			),
    27  			Metadata: pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/@actions/core/-/core-1.6.0.tgz", Integrity: "sha512-NB1UAZomZlCV/LmJqkLhNTqtKfFXJZAUPcfl/zqG7EfsQdeUJtaWO98SGbuQ3pydJ3fHl2CvI/51OKYlCYYcaw=="},
    28  		},
    29  		{
    30  			Name:      "ansi-regex",
    31  			Version:   "3.0.0",
    32  			FoundBy:   "javascript-lock-cataloger",
    33  			PURL:      "pkg:npm/ansi-regex@3.0.0",
    34  			Locations: locationSet,
    35  			Language:  pkg.JavaScript,
    36  			Type:      pkg.NpmPkg,
    37  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", Integrity: "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg="},
    38  		},
    39  		{
    40  			Name:      "cowsay",
    41  			Version:   "1.4.0",
    42  			FoundBy:   "javascript-lock-cataloger",
    43  			PURL:      "pkg:npm/cowsay@1.4.0",
    44  			Locations: locationSet,
    45  			Language:  pkg.JavaScript,
    46  			Type:      pkg.NpmPkg,
    47  			Licenses: pkg.NewLicenseSet(
    48  				pkg.NewLicenseFromLocationsWithContext(ctx, "MIT", file.NewLocation("package-lock.json")),
    49  			),
    50  			Metadata: pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/cowsay/-/cowsay-1.4.0.tgz", Integrity: "sha512-rdg5k5PsHFVJheO/pmE3aDg2rUDDTfPJau6yYkZYlHFktUz+UxbE+IgnUAEyyCyv4noL5ltxXD0gZzmHPCy/9g=="},
    51  		},
    52  		{
    53  			Name:      "get-stdin",
    54  			Version:   "5.0.1",
    55  			FoundBy:   "javascript-lock-cataloger",
    56  			PURL:      "pkg:npm/get-stdin@5.0.1",
    57  			Locations: locationSet,
    58  			Language:  pkg.JavaScript,
    59  			Type:      pkg.NpmPkg,
    60  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/get-stdin/-/get-stdin-5.0.1.tgz", Integrity: "sha1-Ei4WFZHiH/TFJTAwVpPyDmOTo5g="},
    61  		},
    62  		{
    63  			Name:      "is-fullwidth-code-point",
    64  			Version:   "2.0.0",
    65  			FoundBy:   "javascript-lock-cataloger",
    66  			PURL:      "pkg:npm/is-fullwidth-code-point@2.0.0",
    67  			Locations: locationSet,
    68  			Language:  pkg.JavaScript,
    69  			Type:      pkg.NpmPkg,
    70  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", Integrity: "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8="},
    71  		},
    72  		{
    73  			Name:      "minimist",
    74  			Version:   "0.0.10",
    75  			FoundBy:   "javascript-lock-cataloger",
    76  			PURL:      "pkg:npm/minimist@0.0.10",
    77  			Locations: locationSet,
    78  			Language:  pkg.JavaScript,
    79  			Type:      pkg.NpmPkg,
    80  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", Integrity: "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8="},
    81  		},
    82  		{
    83  			Name:      "optimist",
    84  			Version:   "0.6.1",
    85  			FoundBy:   "javascript-lock-cataloger",
    86  			PURL:      "pkg:npm/optimist@0.6.1",
    87  			Locations: locationSet,
    88  			Language:  pkg.JavaScript,
    89  			Type:      pkg.NpmPkg,
    90  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", Integrity: "sha1-2j6nRob6IaGaERwybpDrFaAZZoY="},
    91  		},
    92  		{
    93  			Name:      "string-width",
    94  			Version:   "2.1.1",
    95  			FoundBy:   "javascript-lock-cataloger",
    96  			PURL:      "pkg:npm/string-width@2.1.1",
    97  			Locations: locationSet,
    98  			Language:  pkg.JavaScript,
    99  			Type:      pkg.NpmPkg,
   100  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", Integrity: "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw=="},
   101  		},
   102  		{
   103  			Name:      "strip-ansi",
   104  			Version:   "4.0.0",
   105  			FoundBy:   "javascript-lock-cataloger",
   106  			PURL:      "pkg:npm/strip-ansi@4.0.0",
   107  			Locations: locationSet,
   108  			Language:  pkg.JavaScript,
   109  			Type:      pkg.NpmPkg,
   110  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", Integrity: "sha1-qEeQIusaw2iocTibY1JixQXuNo8="},
   111  		},
   112  		{
   113  			Name:      "strip-eof",
   114  			Version:   "1.0.0",
   115  			FoundBy:   "javascript-lock-cataloger",
   116  			PURL:      "pkg:npm/strip-eof@1.0.0",
   117  			Locations: locationSet,
   118  			Language:  pkg.JavaScript,
   119  			Type:      pkg.NpmPkg,
   120  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", Integrity: "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8="},
   121  		},
   122  		{
   123  			Name:      "wordwrap",
   124  			Version:   "0.0.3",
   125  			FoundBy:   "javascript-lock-cataloger",
   126  			PURL:      "pkg:npm/wordwrap@0.0.3",
   127  			Locations: locationSet,
   128  			Language:  pkg.JavaScript,
   129  			Type:      pkg.NpmPkg,
   130  			Metadata:  pkg.NpmPackageLockEntry{Resolved: "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz", Integrity: "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="},
   131  		},
   132  	}
   133  
   134  	pkgtest.NewCatalogTester().
   135  		FromDirectory(t, "test-fixtures/pkg-lock").
   136  		Expects(expectedPkgs, nil).
   137  		TestCataloger(t, NewLockCataloger(CatalogerConfig{}))
   138  
   139  }
   140  
   141  func Test_PackageCataloger_Globs(t *testing.T) {
   142  	tests := []struct {
   143  		name     string
   144  		fixture  string
   145  		expected []string
   146  	}{
   147  		{
   148  			name:    "obtain package files",
   149  			fixture: "test-fixtures/glob-paths",
   150  			expected: []string{
   151  				"src/package.json",
   152  			},
   153  		},
   154  	}
   155  
   156  	for _, test := range tests {
   157  		t.Run(test.name, func(t *testing.T) {
   158  			pkgtest.NewCatalogTester().
   159  				FromDirectory(t, test.fixture).
   160  				ExpectsResolverContentQueries(test.expected).
   161  				TestCataloger(t, NewPackageCataloger())
   162  		})
   163  	}
   164  }
   165  
   166  func Test_LockCataloger_Globs(t *testing.T) {
   167  	tests := []struct {
   168  		name     string
   169  		fixture  string
   170  		expected []string
   171  	}{
   172  		{
   173  			name:    "obtain package files",
   174  			fixture: "test-fixtures/glob-paths",
   175  			expected: []string{
   176  				"src/package-lock.json",
   177  				"src/pnpm-lock.yaml",
   178  				"src/yarn.lock",
   179  			},
   180  		},
   181  	}
   182  
   183  	for _, test := range tests {
   184  		t.Run(test.name, func(t *testing.T) {
   185  			pkgtest.NewCatalogTester().
   186  				FromDirectory(t, test.fixture).
   187  				ExpectsResolverContentQueries(test.expected).
   188  				TestCataloger(t, NewLockCataloger(CatalogerConfig{}))
   189  		})
   190  	}
   191  }