diff --git a/commands/curation/curationaudit_test.go b/commands/curation/curationaudit_test.go index 117f5cd7b..064c69f32 100644 --- a/commands/curation/curationaudit_test.go +++ b/commands/curation/curationaudit_test.go @@ -885,13 +885,13 @@ func getTestCasesForDoCurationAudit() []testCase { "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem": true, }, - // Expect a report containing the exact blocked package. + // actionpack's subtree collapses every path to activesupport into one entry (XRAY-167222). expectedResp: map[string]*CurationReport{ "Ruby-Project": { packagesStatus: []*PackageStatus{ { Action: "blocked", - ParentName: "actionview", + ParentName: "actionpack", ParentVersion: "5.2.3", BlockedPackageUrl: "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem", PackageName: "activesupport", @@ -906,40 +906,6 @@ func getTestCasesForDoCurationAudit() []testCase { }, }, }, - { - Action: "blocked", - ParentName: "activesupport", - ParentVersion: "5.2.3", - BlockedPackageUrl: "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem", - PackageName: "activesupport", - PackageVersion: "5.2.3", - DepRelation: "direct", - PkgType: "ruby", - BlockingReason: "Policy violations", - Policy: []Policy{ - { - Policy: "pol1", - Condition: "cond1", - }, - }, - }, - { - Action: "blocked", - ParentName: "rails-dom-testing", - ParentVersion: "2.3.0", - BlockedPackageUrl: "/api/gems/ruby-yaml-repo/gems/activesupport-5.2.3.gem", - PackageName: "activesupport", - PackageVersion: "5.2.3", - DepRelation: "indirect", - PkgType: "ruby", - BlockingReason: "Policy violations", - Policy: []Policy{ - { - Policy: "pol1", - Condition: "cond1", - }, - }, - }, }, totalNumberOfPackages: 0, // Ignore package count for cross-platform compatibility }, diff --git a/sca/bom/buildinfo/technologies/gem/gem.go b/sca/bom/buildinfo/technologies/gem/gem.go index da3c15896..193b09f68 100644 --- a/sca/bom/buildinfo/technologies/gem/gem.go +++ b/sca/bom/buildinfo/technologies/gem/gem.go @@ -158,10 +158,9 @@ func getGemCmd(execPath, workingDir, cmd string, args ...string) *io.Command { } // calculateDependencies orchestrates the generation and parsing of Gemfile.lock to build the dependency graph. -// It first runs 'bundle lock' to ensure Gemfile.lock is up-to-date, then parses the lock file, -// -// dependencyTrees: A slice of top-level dependency nodes. -// uniqueDeps: A slice of unique dependency IDs found in the graph. +// It first runs 'bundle lock' to ensure Gemfile.lock is up-to-date, then parses the lock file. +// dependencyTrees must wrap direct deps in one project-root node, like every other tech builder does — +// curation-audit skips auditing whatever Id it sees as a "root". func calculateDependencies(bundleExecPath, workingDir string, _ technologies.BuildInfoBomGeneratorParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) { log.Debug("Ensuring Gemfile.lock is up to date using 'bundle lock'...") if _, err = getGemCmd(bundleExecPath, workingDir, "lock").RunWithOutput(); err != nil { @@ -187,16 +186,17 @@ func calculateDependencies(bundleExecPath, workingDir string, _ technologies.Bui projectRootNode := buildFullGemDependencyGraph(*gemInput, workingDir) + directDeps := []*xrayUtils.GraphNode{} + dependencyTrees = []*xrayUtils.GraphNode{} if projectRootNode != nil { - dependencyTrees = projectRootNode.Nodes + directDeps = projectRootNode.Nodes + dependencyTrees = []*xrayUtils.GraphNode{projectRootNode} } - if dependencyTrees == nil { - dependencyTrees = []*xrayUtils.GraphNode{} - } + // From directDeps, not dependencyTrees — the root's own Id isn't a real package. + uniqueDeps = calculateUniqueDependencies(directDeps) - uniqueDeps = calculateUniqueDependencies(dependencyTrees) - log.Debug("Calculated dependency trees (children of root): %d trees found.", len(dependencyTrees)) + log.Debug(fmt.Sprintf("Calculated gem dependency tree: %d direct dependencies, %d total nodes.", len(directDeps), len(uniqueDeps))) return } diff --git a/sca/bom/buildinfo/technologies/gem/gem_test.go b/sca/bom/buildinfo/technologies/gem/gem_test.go index 34282f05e..675be0b8a 100644 --- a/sca/bom/buildinfo/technologies/gem/gem_test.go +++ b/sca/bom/buildinfo/technologies/gem/gem_test.go @@ -40,21 +40,30 @@ func TestBuildDependencyTree(t *testing.T) { _, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "gem")) defer cleanUp() params := technologies.BuildInfoBomGeneratorParams{SkipAutoInstall: true} - actualTopLevelTrees, uniqueDeps, err := BuildDependencyTree(params) - assert.NoError(t, err, "BuildDependencyTree should not return an error") - expectedTopLevelTrees := expectedResult.Nodes - if !reflect.DeepEqual(expectedTopLevelTrees, actualTopLevelTrees) { - expectedJSON, err := utils.GetAsJsonString(expectedTopLevelTrees, true, false) + actualTrees, uniqueDeps, err := BuildDependencyTree(params) + require.NoError(t, err, "BuildDependencyTree should not return an error") + + // Must return one project-root node wrapping the direct deps, not the deps themselves (XRAY-167222). + require.Len(t, actualTrees, 1, "BuildDependencyTree should return a single project-root node") + actualRoot := actualTrees[0] + assert.NotEmpty(t, actualRoot.Id, "the project-root node should carry the working directory name as its Id") + + expectedDirectDeps := expectedResult.Nodes + actualDirectDeps := actualRoot.Nodes + if !reflect.DeepEqual(expectedDirectDeps, actualDirectDeps) { + expectedJSON, err := utils.GetAsJsonString(expectedDirectDeps, true, false) if err != nil { t.Fatalf("Failed to marshal expected dependency tree to JSON for error reporting: %v", err) } - actualJSON, err := utils.GetAsJsonString(actualTopLevelTrees, true, false) + actualJSON, err := utils.GetAsJsonString(actualDirectDeps, true, false) if err != nil { t.Fatalf("Failed to marshal actual dependency tree to JSON for error reporting: %v", err) } t.Errorf("Dependency tree mismatch.\nExpected (JSON):\n%s\nGot (JSON):\n%s", expectedJSON, actualJSON) } + + // The root's own Id isn't a real package and must stay out of uniqueDeps. assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "Unique dependencies mismatch. First is actual, Second is Expected") }