github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/javascript/parse_yarn_lock_test.go (about)

     1  package javascript
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/artifact"
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/pkg"
    11  	"github.com/lineaje-labs/syft/syft/pkg/cataloger/internal/pkgtest"
    12  )
    13  
    14  func TestParseYarnBerry(t *testing.T) {
    15  	var expectedRelationships []artifact.Relationship
    16  	fixture := "test-fixtures/yarn-berry/yarn.lock"
    17  	locations := file.NewLocationSet(file.NewLocation(fixture))
    18  
    19  	expectedPkgs := []pkg.Package{
    20  		{
    21  			Name:      "@babel/code-frame",
    22  			Version:   "7.10.4",
    23  			Locations: locations,
    24  			PURL:      "pkg:npm/%40babel/code-frame@7.10.4",
    25  			Language:  pkg.JavaScript,
    26  			Type:      pkg.NpmPkg,
    27  		},
    28  		{
    29  			Name:      "@types/minimatch",
    30  			Version:   "3.0.3",
    31  			Locations: locations,
    32  			PURL:      "pkg:npm/%40types/minimatch@3.0.3",
    33  			Language:  pkg.JavaScript,
    34  			Type:      pkg.NpmPkg,
    35  		},
    36  		{
    37  			Name:      "@types/qs",
    38  			Version:   "6.9.4",
    39  			Locations: locations,
    40  			PURL:      "pkg:npm/%40types/qs@6.9.4",
    41  			Language:  pkg.JavaScript,
    42  			Type:      pkg.NpmPkg,
    43  		},
    44  		{
    45  			Name:      "ajv",
    46  			Version:   "6.12.3",
    47  			Locations: locations,
    48  			PURL:      "pkg:npm/ajv@6.12.3",
    49  			Language:  pkg.JavaScript,
    50  			Type:      pkg.NpmPkg,
    51  		},
    52  		{
    53  			Name:      "asn1.js",
    54  			Version:   "4.10.1",
    55  			Locations: locations,
    56  			PURL:      "pkg:npm/asn1.js@4.10.1",
    57  			Language:  pkg.JavaScript,
    58  			Type:      pkg.NpmPkg,
    59  		},
    60  		{
    61  			Name:      "atob",
    62  			Version:   "2.1.2",
    63  			Locations: locations,
    64  			PURL:      "pkg:npm/atob@2.1.2",
    65  			Language:  pkg.JavaScript,
    66  			Type:      pkg.NpmPkg,
    67  		},
    68  		{
    69  			Name:      "aws-sdk",
    70  			Version:   "2.706.0",
    71  			PURL:      "pkg:npm/aws-sdk@2.706.0",
    72  			Locations: locations,
    73  			Language:  pkg.JavaScript,
    74  			Type:      pkg.NpmPkg,
    75  		},
    76  		{
    77  			Name:      "c0n-fab_u.laTION",
    78  			Version:   "7.7.7",
    79  			Locations: locations,
    80  			PURL:      "pkg:npm/c0n-fab_u.laTION@7.7.7",
    81  			Language:  pkg.JavaScript,
    82  			Type:      pkg.NpmPkg,
    83  		},
    84  		{
    85  			Name:      "jhipster-core",
    86  			Version:   "7.3.4",
    87  			Locations: locations,
    88  			PURL:      "pkg:npm/jhipster-core@7.3.4",
    89  			Language:  pkg.JavaScript,
    90  			Type:      pkg.NpmPkg,
    91  		},
    92  	}
    93  
    94  	pkgtest.TestFileParser(t, fixture, parseYarnLock, expectedPkgs, expectedRelationships)
    95  
    96  }
    97  
    98  func TestParseYarnLock(t *testing.T) {
    99  	var expectedRelationships []artifact.Relationship
   100  	fixture := "test-fixtures/yarn/yarn.lock"
   101  	locations := file.NewLocationSet(file.NewLocation(fixture))
   102  
   103  	expectedPkgs := []pkg.Package{
   104  		{
   105  			Name:      "@babel/code-frame",
   106  			Version:   "7.10.4",
   107  			Locations: locations,
   108  			PURL:      "pkg:npm/%40babel/code-frame@7.10.4",
   109  			Language:  pkg.JavaScript,
   110  			Type:      pkg.NpmPkg,
   111  		},
   112  		{
   113  			Name:      "@types/minimatch",
   114  			Version:   "3.0.3",
   115  			Locations: locations,
   116  			PURL:      "pkg:npm/%40types/minimatch@3.0.3",
   117  			Language:  pkg.JavaScript,
   118  			Type:      pkg.NpmPkg,
   119  		},
   120  		{
   121  			Name:      "@types/qs",
   122  			Version:   "6.9.4",
   123  			Locations: locations,
   124  			PURL:      "pkg:npm/%40types/qs@6.9.4",
   125  			Language:  pkg.JavaScript,
   126  			Type:      pkg.NpmPkg,
   127  		},
   128  		{
   129  			Name:      "ajv",
   130  			Version:   "6.12.3",
   131  			Locations: locations,
   132  			PURL:      "pkg:npm/ajv@6.12.3",
   133  			Language:  pkg.JavaScript,
   134  			Type:      pkg.NpmPkg,
   135  		},
   136  		{
   137  			Name:      "asn1.js",
   138  			Version:   "4.10.1",
   139  			Locations: locations,
   140  			PURL:      "pkg:npm/asn1.js@4.10.1",
   141  			Language:  pkg.JavaScript,
   142  			Type:      pkg.NpmPkg,
   143  		},
   144  		{
   145  			Name:      "atob",
   146  			Version:   "2.1.2",
   147  			Locations: locations,
   148  
   149  			PURL:     "pkg:npm/atob@2.1.2",
   150  			Language: pkg.JavaScript,
   151  			Type:     pkg.NpmPkg,
   152  		},
   153  		{
   154  			Name:      "aws-sdk",
   155  			Version:   "2.706.0",
   156  			Locations: locations,
   157  			PURL:      "pkg:npm/aws-sdk@2.706.0",
   158  			Language:  pkg.JavaScript,
   159  			Type:      pkg.NpmPkg,
   160  		},
   161  		{
   162  			Name:      "jhipster-core",
   163  			Version:   "7.3.4",
   164  			Locations: locations,
   165  			PURL:      "pkg:npm/jhipster-core@7.3.4",
   166  			Language:  pkg.JavaScript,
   167  			Type:      pkg.NpmPkg,
   168  		},
   169  
   170  		{
   171  			Name:      "something-i-made-up",
   172  			Version:   "7.7.7",
   173  			Locations: locations,
   174  			PURL:      "pkg:npm/something-i-made-up@7.7.7",
   175  			Language:  pkg.JavaScript,
   176  			Type:      pkg.NpmPkg,
   177  		},
   178  	}
   179  
   180  	pkgtest.TestFileParser(t, fixture, parseYarnLock, expectedPkgs, expectedRelationships)
   181  
   182  }
   183  
   184  func TestParseYarnFindPackageNames(t *testing.T) {
   185  	tests := []struct {
   186  		line     string
   187  		expected string
   188  	}{
   189  		{
   190  			line:     `"@babel/code-frame@npm:7.10.4":`,
   191  			expected: "@babel/code-frame",
   192  		},
   193  		{
   194  			line:     `"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4":`,
   195  			expected: "@babel/code-frame",
   196  		},
   197  		{
   198  			line:     "ajv@^6.10.2, ajv@^6.5.5:",
   199  			expected: "ajv",
   200  		},
   201  		{
   202  			line:     "aws-sdk@2.706.0:",
   203  			expected: "aws-sdk",
   204  		},
   205  		{
   206  			line:     "asn1.js@^4.0.0:",
   207  			expected: "asn1.js",
   208  		},
   209  		{
   210  			line:     "c0n-fab_u.laTION@^7.0.0",
   211  			expected: "c0n-fab_u.laTION",
   212  		},
   213  		{
   214  			line:     `"newtest@workspace:.":`,
   215  			expected: "newtest",
   216  		},
   217  		{
   218  			line:     `"color-convert@npm:^1.9.0":`,
   219  			expected: "color-convert",
   220  		},
   221  		{
   222  			line:     `"@npmcorp/code-frame@^7.1.0", "@npmcorp/code-frame@^7.10.4":`,
   223  			expected: "@npmcorp/code-frame",
   224  		},
   225  		{
   226  			line:     `"@npmcorp/code-frame@^7.2.3":`,
   227  			expected: "@npmcorp/code-frame",
   228  		},
   229  		{
   230  			line:     `"@s/odd-name@^7.1.2":`,
   231  			expected: "@s/odd-name",
   232  		},
   233  		{
   234  			line:     `"@/code-frame@^7.3.4":`,
   235  			expected: "",
   236  		},
   237  		{
   238  			line:     `"code-frame":`,
   239  			expected: "",
   240  		},
   241  	}
   242  
   243  	for _, test := range tests {
   244  		t.Run(test.expected, func(t *testing.T) {
   245  			t.Parallel()
   246  			actual := findPackageName(test.line)
   247  			assert.Equal(t, test.expected, actual)
   248  		})
   249  	}
   250  }
   251  
   252  func TestParseYarnFindPackageVersions(t *testing.T) {
   253  	tests := []struct {
   254  		line     string
   255  		expected string
   256  	}{
   257  		{
   258  			line:     `  version "7.10.4"`,
   259  			expected: "7.10.4",
   260  		},
   261  		{
   262  			line:     ` version "7.11.5"`,
   263  			expected: "7.11.5",
   264  		},
   265  		{
   266  			line:     `version "7.12.6"`,
   267  			expected: "",
   268  		},
   269  		{
   270  			line:     `  version "0.0.0"`,
   271  			expected: "0.0.0",
   272  		},
   273  		{
   274  			line:     `  version "2" `,
   275  			expected: "2",
   276  		},
   277  		{
   278  			line:     `  version "9.3"`,
   279  			expected: "9.3",
   280  		},
   281  		{
   282  			line:     "ajv@^6.10.2, ajv@^6.5.5",
   283  			expected: "",
   284  		},
   285  		{
   286  			line:     "atob@^2.1.2:",
   287  			expected: "",
   288  		},
   289  		{
   290  			line:     `"color-convert@npm:^1.9.0":`,
   291  			expected: "",
   292  		},
   293  		{
   294  			line:     "  version: 1.9.3",
   295  			expected: "1.9.3",
   296  		},
   297  		{
   298  			line:     "  version: 2",
   299  			expected: "2",
   300  		},
   301  		{
   302  			line:     "  version: 9.3",
   303  			expected: "9.3",
   304  		},
   305  		{
   306  			line:     "ajv@^6.10.2, ajv@^6.5.5",
   307  			expected: "",
   308  		},
   309  		{
   310  			line:     "atob@^2.1.2:",
   311  			expected: "",
   312  		},
   313  		{
   314  			line:     "  version: 1.0.0-alpha+001",
   315  			expected: "1.0.0-alpha",
   316  		},
   317  		{
   318  			line:     "  version: 1.0.0-beta_test+exp.sha.5114f85",
   319  			expected: "1.0.0-beta_test",
   320  		},
   321  		{
   322  			line:     "  version: 1.0.0+21AF26D3-117B344092BD",
   323  			expected: "1.0.0",
   324  		},
   325  		{
   326  			line:     "  version: 0.0.0-use.local",
   327  			expected: "0.0.0-use.local",
   328  		},
   329  	}
   330  
   331  	for _, test := range tests {
   332  		t.Run(test.expected, func(t *testing.T) {
   333  			t.Parallel()
   334  			actual := findPackageVersion(test.line)
   335  			assert.Equal(t, test.expected, actual)
   336  		})
   337  	}
   338  }