Skip to content

Commit

Permalink
added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 19, 2024
1 parent 52be2cc commit 682be8b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
85 changes: 57 additions & 28 deletions commands/audit/sca/yarn/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,69 @@ import (
"github.com/stretchr/testify/assert"
)

func TestParseYarnDependenciesList(t *testing.T) {
func TestParseYarnDependenciesMap(t *testing.T) {
npmId := techutils.Npm.GetPackageTypeId()
yarnDependencies := map[string]*bibuildutils.YarnDependency{
"pack1@npm:1.0.0": {Value: "pack1@npm:1.0.0", Details: bibuildutils.YarnDepDetails{Version: "1.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack4@npm:4.0.0"}}}},
"pack2@npm:2.0.0": {Value: "pack2@npm:2.0.0", Details: bibuildutils.YarnDepDetails{Version: "2.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack4@npm:4.0.0"}, {Locator: "pack5@npm:5.0.0"}}}},
"@jfrog/pack3@npm:3.0.0": {Value: "@jfrog/pack3@npm:3.0.0", Details: bibuildutils.YarnDepDetails{Version: "3.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack1@virtual:c192f6b3b32cd5d11a443144e162ec3bc#npm:1.0.0"}, {Locator: "pack2@npm:2.0.0"}}}},
"pack4@npm:4.0.0": {Value: "pack4@npm:4.0.0", Details: bibuildutils.YarnDepDetails{Version: "4.0.0"}},
"pack5@npm:5.0.0": {Value: "pack5@npm:5.0.0", Details: bibuildutils.YarnDepDetails{Version: "5.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack2@npm:2.0.0"}}}},
}

rootXrayId := npmId + "@jfrog/pack3:3.0.0"
expectedTree := &xrayUtils.GraphNode{
Id: rootXrayId,
Nodes: []*xrayUtils.GraphNode{
{Id: npmId + "pack1:1.0.0",
Nodes: []*xrayUtils.GraphNode{
{Id: npmId + "pack4:4.0.0",
Nodes: []*xrayUtils.GraphNode{}},
}},
{Id: npmId + "pack2:2.0.0",
testCases := []struct {
name string
yarnDependencies map[string]*bibuildutils.YarnDependency
rootXrayId string
expectedTree *xrayUtils.GraphNode
expectedUniqueDeps []string
errorExpected bool
}{
{
name: "Successful tree construction",
yarnDependencies: map[string]*bibuildutils.YarnDependency{
"pack1@npm:1.0.0": {Value: "pack1@npm:1.0.0", Details: bibuildutils.YarnDepDetails{Version: "1.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack4@npm:4.0.0"}}}},
"pack2@npm:2.0.0": {Value: "pack2@npm:2.0.0", Details: bibuildutils.YarnDepDetails{Version: "2.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack4@npm:4.0.0"}, {Locator: "pack5@npm:5.0.0"}}}},
"@jfrog/pack3@npm:3.0.0": {Value: "@jfrog/pack3@npm:3.0.0", Details: bibuildutils.YarnDepDetails{Version: "3.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack1@virtual:c192f6b3b32cd5d11a443144e162ec3bc#npm:1.0.0"}, {Locator: "pack2@npm:2.0.0"}}}},
"pack4@npm:4.0.0": {Value: "pack4@npm:4.0.0", Details: bibuildutils.YarnDepDetails{Version: "4.0.0"}},
"pack5@npm:5.0.0": {Value: "pack5@npm:5.0.0", Details: bibuildutils.YarnDepDetails{Version: "5.0.0", Dependencies: []bibuildutils.YarnDependencyPointer{{Locator: "pack2@npm:2.0.0"}}}},
},
rootXrayId: npmId + "@jfrog/pack3:3.0.0",
expectedTree: &xrayUtils.GraphNode{
Id: npmId + "@jfrog/pack3:3.0.0",
Nodes: []*xrayUtils.GraphNode{
{Id: npmId + "pack4:4.0.0",
Nodes: []*xrayUtils.GraphNode{}},
{Id: npmId + "pack5:5.0.0",
Nodes: []*xrayUtils.GraphNode{}},
}},
{Id: npmId + "pack1:1.0.0",
Nodes: []*xrayUtils.GraphNode{
{Id: npmId + "pack4:4.0.0",
Nodes: []*xrayUtils.GraphNode{}},
}},
{Id: npmId + "pack2:2.0.0",
Nodes: []*xrayUtils.GraphNode{
{Id: npmId + "pack4:4.0.0",
Nodes: []*xrayUtils.GraphNode{}},
{Id: npmId + "pack5:5.0.0",
Nodes: []*xrayUtils.GraphNode{}},
}},
},
},
expectedUniqueDeps: []string{npmId + "pack1:1.0.0", npmId + "pack2:2.0.0", npmId + "pack4:4.0.0", npmId + "pack5:5.0.0", npmId + "@jfrog/pack3:3.0.0"},
errorExpected: false,
},
{
name: "Incorrect formatted dependency name - error expected",
yarnDependencies: map[string]*bibuildutils.YarnDependency{
"@privateDep": {Value: "", Details: bibuildutils.YarnDepDetails{Version: "privateDep"}},
},
rootXrayId: npmId + "@jfrog/pack3:3.0.0",
errorExpected: true,
},
}
expectedUniqueDeps := []string{npmId + "pack1:1.0.0", npmId + "pack2:2.0.0", npmId + "pack4:4.0.0", npmId + "pack5:5.0.0", npmId + "@jfrog/pack3:3.0.0"}

xrayDependenciesTree, uniqueDeps, err := parseYarnDependenciesMap(yarnDependencies, rootXrayId)
assert.NoError(t, err)
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
assert.True(t, tests.CompareTree(expectedTree, xrayDependenciesTree), "expected:", expectedTree.Nodes, "got:", xrayDependenciesTree.Nodes)
for _, testcase := range testCases {
t.Run(testcase.name, func(t *testing.T) {
xrayDependenciesTree, uniqueDeps, err := parseYarnDependenciesMap(testcase.yarnDependencies, testcase.rootXrayId)
if !testcase.errorExpected {
assert.NoError(t, err)
assert.ElementsMatch(t, uniqueDeps, testcase.expectedUniqueDeps, "First is actual, Second is Expected")
assert.True(t, tests.CompareTree(testcase.expectedTree, xrayDependenciesTree), "expected:", testcase.expectedTree.Nodes, "got:", xrayDependenciesTree.Nodes)
} else {
assert.Error(t, err)
}
})
}
}

func TestIsInstallRequired(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ require (

// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 dev

replace github.com/jfrog/build-info-go => github.com/eranturgeman/build-info-go v0.0.0-20241217151909-f011a1dd6a48
replace github.com/jfrog/build-info-go => github.com/eranturgeman/build-info-go v0.0.0-20241219075929-fd4ec6b12702

// replace github.com/jfrog/froggit-go => github.com/jfrog/froggit-go dev
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcej
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/eranturgeman/build-info-go v0.0.0-20241217151909-f011a1dd6a48 h1:TidHfgRqcmpE159oSroAOqNEiQoI4z0NBPJV70YDU7g=
github.com/eranturgeman/build-info-go v0.0.0-20241217151909-f011a1dd6a48/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/eranturgeman/build-info-go v0.0.0-20241219075929-fd4ec6b12702 h1:ZFlHiDiDXfQ1VOwx4ZfLk7q11Dxqhioj8F8wQF8tgxg=
github.com/eranturgeman/build-info-go v0.0.0-20241219075929-fd4ec6b12702/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/forPelevin/gomoji v1.2.0 h1:9k4WVSSkE1ARO/BWywxgEUBvR/jMnao6EZzrql5nxJ8=
Expand Down

0 comments on commit 682be8b

Please sign in to comment.