Skip to content

Commit

Permalink
fixes after reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Dec 1, 2024
1 parent 40d42bb commit 3ebc755
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
8 changes: 6 additions & 2 deletions internal/callgraph/language/java/callgraph.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package java

import (
"fmt"
"regexp"

"github.com/debricked/cli/internal/callgraph/cgexec"
Expand Down Expand Up @@ -74,13 +75,16 @@ func (cg *Callgraph) javaVersion(path string) (string, error) {

cmd := cgexec.NewCommand(osCmd)
err = cgexec.RunCommand(*cmd, cg.ctx)
if err != nil {
return "", err
}
javaVersionRegex := regexp.MustCompile(`\b(\d+)\.\d+\.\d+\b`)
match := javaVersionRegex.FindStringSubmatch(cmd.GetStdOut().String())
if len(match) > 1 {
return match[1], nil
} else {
return "", fmt.Errorf("no version found in 'java --version' output, are you using a non-numeric version?")
}

return "", err
}

func (cg *Callgraph) RunCallGraph(callgraphJarPath string) error {
Expand Down
29 changes: 8 additions & 21 deletions internal/callgraph/language/java/callgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,13 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRunCallGraphWithSetupMock(t *testing.T) {

cmdMock := testdata.NewEchoCmdFactory()
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)

err := cg.RunCallGraphWithSetup()

assert.Nil(t, err)
}

func TestRunCallGraphWithSetupSootWrapperError(t *testing.T) {

cmdMock := testdata.NewEchoCmdFactory()
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{GetSootWrapperError: fmt.Errorf("")}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
shMock := testdata.MockSootHandler{GetSootWrapperError: fmt.Errorf("")}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, shMock)

err := cg.RunCallGraphWithSetup()

Expand All @@ -40,8 +27,8 @@ func TestRunCallGraphWithSetupSootVersionError(t *testing.T) {
cmdMock := testdata.CmdFactoryMock{JavaVersionErr: fmt.Errorf("version error")}
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
shMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, shMock)

err := cg.RunCallGraphWithSetup()

Expand All @@ -52,8 +39,8 @@ func TestRunCallGraphMock(t *testing.T) {
cmdMock := testdata.NewEchoCmdFactory()
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
shMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, shMock)

err := cg.RunCallGraph(".")

Expand All @@ -65,8 +52,8 @@ func TestRunCallGraphErrorMock(t *testing.T) {
cmdMock.CallGraphGenErr = fmt.Errorf("error")
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
shMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, shMock)

err := cg.RunCallGraph(".")

Expand Down
1 change: 0 additions & 1 deletion internal/callgraph/language/java/soot_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func (sh SootHandler) downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile
version,
".zip",
}, "")
fmt.Println("URL=", fullURLFile)

client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (f CmdFactoryMock) MakeBuildMavenCmd(workingDirectory string, ctx cgexec.IC
}

func (f CmdFactoryMock) MakeJavaVersionCmd(workingDirectory string, ctx cgexec.IContext) (*exec.Cmd, error) {
return exec.Command(f.JavaVersionName, "JavaVersion"), f.JavaVersionErr
return exec.Command(
f.JavaVersionName,
"\"openjdk 23.0.1 2024-10-15\nOpenJDK Runtime Environment Homebrew (build 23.0.1)\nOpenJDK 64-Bit Server VM Homebrew (build 23.0.1, mixed mode, sharing)\"",
), f.JavaVersionErr
}

type MockSootHandler struct {
Expand Down

0 comments on commit 3ebc755

Please sign in to comment.