Skip to content

Add an option to provide descriptor name in Conan graph info command #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion commands/audit/sca/conan/conan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os/exec"
"path/filepath"

"github.com/jfrog/gofrog/datastructures"
"github.com/jfrog/gofrog/io"
Expand Down Expand Up @@ -108,7 +109,16 @@ func calculateUniqueDependencies(nodes map[string]conanRef) []string {
}

func calculateDependencies(executablePath, workingDir string, params utils.AuditParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) {
graphInfo := append([]string{"info", ".", "--format=json"}, params.Args()...)
graphInfo := []string{"info"}
if params.PipRequirementsFile() != "" {
// We allow passing a custom name for the descriptor file to be used in the 'graph info' command. If non has provided we execute the command on the current dir.
// Since this ability already exists for python we leverage this ability
graphInfo = append(graphInfo, filepath.Join(workingDir, params.PipRequirementsFile()))
} else {
graphInfo = append(graphInfo, ".")
}
graphInfo = append(graphInfo, "--format=json")
graphInfo = append(graphInfo, params.Args()...)
conanGraphInfoContent, err := getConanCmd(executablePath, workingDir, "graph", graphInfo...).RunWithOutput()
if err != nil {
return
Expand Down
44 changes: 35 additions & 9 deletions commands/audit/sca/conan/conan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package conan

import (
"encoding/json"
"github.com/stretchr/testify/require"
"os"
"os/exec"
"path/filepath"
"testing"

Expand Down Expand Up @@ -42,16 +44,40 @@ func TestParseConanDependencyTree(t *testing.T) {
}

func TestBuildDependencyTree(t *testing.T) {
dir, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "conan"))
defer cleanUp()
params := &utils.AuditBasicParams{}
params.SetConanProfile(filepath.Join(dir, "profile"))
graph, uniqueDeps, err := BuildDependencyTree(params)
assert.NoError(t, err)
if !tests.CompareTree(expectedResult, graph[0]) {
t.Errorf("expected %+v, got: %+v", expectedResult.Nodes, graph)
testcases := []struct {
name string
descriptorName string
}{
{
name: "default descriptor file",
},
{
name: "custom descriptor file",
descriptorName: "conanfile-system.txt",
},
}
for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
dir, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "conan"))
defer cleanUp()
params := &utils.AuditBasicParams{}
if testcase.descriptorName != "" {
// changing the name of the descriptor to verify the work with a non-default descriptor name
changeNameCmd := exec.Command("mv", filepath.Join(dir, "conanfile.txt"), filepath.Join(dir, testcase.descriptorName))
_, err := changeNameCmd.CombinedOutput()
require.NoError(t, err)
require.FileExists(t, filepath.Join(dir, testcase.descriptorName))
params.SetPipRequirementsFile(testcase.descriptorName)
}
params.SetConanProfile(filepath.Join(dir, "profile"))
graph, uniqueDeps, err := BuildDependencyTree(params)
assert.NoError(t, err)
if !tests.CompareTree(expectedResult, graph[0]) {
t.Errorf("expected %+v, got: %+v", expectedResult.Nodes, graph)
}
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
})
}
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
}

func TestCalculateUniqueDeps(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions commands/audit/scarunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func getRequestedDescriptors(params *AuditParams) map[techutils.Technology][]str
requestedDescriptors := map[techutils.Technology][]string{}
if params.PipRequirementsFile() != "" {
requestedDescriptors[techutils.Pip] = []string{params.PipRequirementsFile()}
// We leverage the ability to pass a custom descriptor name for Conan through PipRequirementsFile as well, therefore we set also Conan with the provided descriptor name.
requestedDescriptors[techutils.Conan] = []string{params.PipRequirementsFile()}
}
return requestedDescriptors
}
Expand Down
Loading