Skip to content

Commit 609e3d6

Browse files
authored
Add an option to provide descriptor name in Conan graph info command (jfrog#412)
1 parent c9370fb commit 609e3d6

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

commands/audit/sca/conan/conan.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"os/exec"
8+
"path/filepath"
89

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

110111
func calculateDependencies(executablePath, workingDir string, params utils.AuditParams) (dependencyTrees []*xrayUtils.GraphNode, uniqueDeps []string, err error) {
111-
graphInfo := append([]string{"info", ".", "--format=json"}, params.Args()...)
112+
graphInfo := []string{"info"}
113+
if params.PipRequirementsFile() != "" {
114+
// 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.
115+
// Since this ability already exists for python we leverage this ability
116+
graphInfo = append(graphInfo, filepath.Join(workingDir, params.PipRequirementsFile()))
117+
} else {
118+
graphInfo = append(graphInfo, ".")
119+
}
120+
graphInfo = append(graphInfo, "--format=json")
121+
graphInfo = append(graphInfo, params.Args()...)
112122
conanGraphInfoContent, err := getConanCmd(executablePath, workingDir, "graph", graphInfo...).RunWithOutput()
113123
if err != nil {
114124
return

commands/audit/sca/conan/conan_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package conan
22

33
import (
44
"encoding/json"
5+
"github.com/stretchr/testify/require"
56
"os"
7+
"os/exec"
68
"path/filepath"
79
"testing"
810

@@ -42,16 +44,40 @@ func TestParseConanDependencyTree(t *testing.T) {
4244
}
4345

4446
func TestBuildDependencyTree(t *testing.T) {
45-
dir, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "conan"))
46-
defer cleanUp()
47-
params := &utils.AuditBasicParams{}
48-
params.SetConanProfile(filepath.Join(dir, "profile"))
49-
graph, uniqueDeps, err := BuildDependencyTree(params)
50-
assert.NoError(t, err)
51-
if !tests.CompareTree(expectedResult, graph[0]) {
52-
t.Errorf("expected %+v, got: %+v", expectedResult.Nodes, graph)
47+
testcases := []struct {
48+
name string
49+
descriptorName string
50+
}{
51+
{
52+
name: "default descriptor file",
53+
},
54+
{
55+
name: "custom descriptor file",
56+
descriptorName: "conanfile-system.txt",
57+
},
58+
}
59+
for _, testcase := range testcases {
60+
t.Run(testcase.name, func(t *testing.T) {
61+
dir, cleanUp := sca.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "conan"))
62+
defer cleanUp()
63+
params := &utils.AuditBasicParams{}
64+
if testcase.descriptorName != "" {
65+
// changing the name of the descriptor to verify the work with a non-default descriptor name
66+
changeNameCmd := exec.Command("mv", filepath.Join(dir, "conanfile.txt"), filepath.Join(dir, testcase.descriptorName))
67+
_, err := changeNameCmd.CombinedOutput()
68+
require.NoError(t, err)
69+
require.FileExists(t, filepath.Join(dir, testcase.descriptorName))
70+
params.SetPipRequirementsFile(testcase.descriptorName)
71+
}
72+
params.SetConanProfile(filepath.Join(dir, "profile"))
73+
graph, uniqueDeps, err := BuildDependencyTree(params)
74+
assert.NoError(t, err)
75+
if !tests.CompareTree(expectedResult, graph[0]) {
76+
t.Errorf("expected %+v, got: %+v", expectedResult.Nodes, graph)
77+
}
78+
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
79+
})
5380
}
54-
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
5581
}
5682

5783
func TestCalculateUniqueDeps(t *testing.T) {

commands/audit/scarunner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ func getRequestedDescriptors(params *AuditParams) map[techutils.Technology][]str
117117
requestedDescriptors := map[techutils.Technology][]string{}
118118
if params.PipRequirementsFile() != "" {
119119
requestedDescriptors[techutils.Pip] = []string{params.PipRequirementsFile()}
120+
// 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.
121+
requestedDescriptors[techutils.Conan] = []string{params.PipRequirementsFile()}
120122
}
121123
return requestedDescriptors
122124
}

0 commit comments

Comments
 (0)