Files
aquasecurity-trivy/pkg/dependency/parser/nodejs/pnpm/parse_test.go
Teppei Fukuda 3eecfc6b6e refactor: unify Library and Package structs (#6633)
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Co-authored-by: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com>
Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
2024-05-07 12:25:52 +00:00

211 lines
5.2 KiB
Go

package pnpm
import (
"os"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
)
func TestParse(t *testing.T) {
tests := []struct {
name string
file string // Test input file
want []ftypes.Package
wantDeps []ftypes.Dependency
}{
{
name: "normal",
file: "testdata/pnpm-lock_normal.yaml",
want: pnpmNormal,
wantDeps: pnpmNormalDeps,
},
{
name: "with dev deps",
file: "testdata/pnpm-lock_with_dev.yaml",
want: pnpmWithDev,
wantDeps: pnpmWithDevDeps,
},
{
name: "many",
file: "testdata/pnpm-lock_many.yaml",
want: pnpmMany,
wantDeps: pnpmManyDeps,
},
{
name: "archives",
file: "testdata/pnpm-lock_archives.yaml",
want: pnpmArchives,
wantDeps: pnpmArchivesDeps,
},
{
name: "v6",
file: "testdata/pnpm-lock_v6.yaml",
want: pnpmV6,
wantDeps: pnpmV6Deps,
},
{
name: "v6 with dev deps",
file: "testdata/pnpm-lock_v6_with_dev.yaml",
want: pnpmV6WithDev,
wantDeps: pnpmV6WithDevDeps,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(tt.file)
require.NoError(t, err)
got, deps, err := NewParser().Parse(f)
require.NoError(t, err)
sort.Sort(ftypes.Packages(got))
sort.Sort(ftypes.Packages(tt.want))
assert.Equal(t, tt.want, got)
if tt.wantDeps != nil {
sort.Sort(ftypes.Dependencies(deps))
sort.Sort(ftypes.Dependencies(tt.wantDeps))
for _, dep := range deps {
sort.Strings(dep.DependsOn)
}
for _, dep := range tt.wantDeps {
sort.Strings(dep.DependsOn)
}
assert.Equal(t, tt.wantDeps, deps)
}
})
}
}
func Test_parsePackage(t *testing.T) {
tests := []struct {
name string
lockFileVer float64
pkg string
wantName string
wantVersion string
}{
{
name: "v5 - relative path",
lockFileVer: 5.0,
pkg: "/lodash/4.17.10",
wantName: "lodash",
wantVersion: "4.17.10",
},
{
name: "v5 - registry",
lockFileVer: 5.0,
pkg: "registry.npmjs.org/lodash/4.17.10",
wantName: "lodash",
wantVersion: "4.17.10",
},
{
name: "v5 - relative path with slash",
lockFileVer: 5.0,
pkg: "/@babel/generator/7.21.9",
wantName: "@babel/generator",
wantVersion: "7.21.9",
},
{
name: "v5 - registry path with slash",
lockFileVer: 5.0,
pkg: "registry.npmjs.org/@babel/generator/7.21.9",
wantName: "@babel/generator",
wantVersion: "7.21.9",
},
{
name: "v5 - relative path with slash and peer deps",
lockFileVer: 5.0,
pkg: "/@babel/helper-compilation-targets/7.21.5_@babel+core@7.21.8",
wantName: "@babel/helper-compilation-targets",
wantVersion: "7.21.5",
},
{
name: "v5 - relative path with underline and peer deps",
lockFileVer: 5.0,
pkg: "/lodash._baseclone/4.5.7_@babel+core@7.21.8",
wantName: "lodash._baseclone",
wantVersion: "4.5.7",
},
{
name: "v5 - registry with slash and peer deps",
lockFileVer: 5.0,
pkg: "registry.npmjs.org/@babel/helper-compilation-targets/7.21.5_@babel+core@7.21.8",
wantName: "@babel/helper-compilation-targets",
wantVersion: "7.21.5",
},
{
name: "v5 - relative path with wrong version",
lockFileVer: 5.0,
pkg: "/lodash/4-wrong",
wantName: "",
wantVersion: "",
},
{
name: "v6 - relative path",
lockFileVer: 6.0,
pkg: "/update-browserslist-db@1.0.11",
wantName: "update-browserslist-db",
wantVersion: "1.0.11",
},
{
name: "v6 - registry",
lockFileVer: 6.0,
pkg: "registry.npmjs.org/lodash@4.17.10",
wantName: "lodash",
wantVersion: "4.17.10",
},
{
name: "v6 - relative path with slash",
lockFileVer: 6.0,
pkg: "/@babel/helper-annotate-as-pure@7.18.6",
wantName: "@babel/helper-annotate-as-pure",
wantVersion: "7.18.6",
},
{
name: "v6 - registry with slash",
lockFileVer: 6.0,
pkg: "registry.npmjs.org/@babel/helper-annotate-as-pure@7.18.6",
wantName: "@babel/helper-annotate-as-pure",
wantVersion: "7.18.6",
},
{
name: "v6 - relative path with slash and peer deps",
lockFileVer: 6.0,
pkg: "/@babel/helper-compilation-targets@7.21.5(@babel/core@7.20.7)",
wantName: "@babel/helper-compilation-targets",
wantVersion: "7.21.5",
},
{
name: "v6 - registry with slash and peer deps",
lockFileVer: 6.0,
pkg: "registry.npmjs.org/@babel/helper-compilation-targets@7.21.5(@babel/core@7.20.7)",
wantName: "@babel/helper-compilation-targets",
wantVersion: "7.21.5",
},
{
name: "v6 - relative path with wrong version",
lockFileVer: 6.0,
pkg: "/lodash@4-wrong",
wantName: "",
wantVersion: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewParser()
gotName, gotVersion := p.parsePackage(tt.pkg, tt.lockFileVer)
assert.Equal(t, tt.wantName, gotName)
assert.Equal(t, tt.wantVersion, gotVersion)
})
}
}