-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package job | ||
|
||
import ( | ||
"errors" | ||
"os/exec" | ||
"testing" | ||
|
||
err "github.com/debricked/cli/pkg/io/err" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const testDir = "dir" | ||
|
||
var testFiles = []string{"file"} | ||
|
||
func TestNewBaseJob(t *testing.T) { | ||
j := NewBaseJob(testDir, testFiles) | ||
assert.Equal(t, testFiles, j.GetFiles()) | ||
assert.Equal(t, testDir, j.GetDir()) | ||
assert.NotNil(t, j.Errors()) | ||
assert.NotNil(t, j.status) | ||
} | ||
|
||
func TestGetFiles(t *testing.T) { | ||
j := BaseJob{} | ||
j.files = testFiles | ||
assert.Equal(t, testFiles, j.GetFiles()) | ||
} | ||
|
||
func TestGetDir(t *testing.T) { | ||
j := BaseJob{} | ||
j.dir = testDir | ||
assert.Equal(t, testDir, j.GetDir()) | ||
} | ||
|
||
func TestReceiveStatus(t *testing.T) { | ||
j := BaseJob{ | ||
files: testFiles, | ||
dir: testDir, | ||
errs: nil, | ||
status: make(chan string), | ||
} | ||
|
||
statusChan := j.ReceiveStatus() | ||
assert.NotNil(t, statusChan) | ||
} | ||
|
||
func TestErrors(t *testing.T) { | ||
jobErr := errors.New("error") | ||
j := BaseJob{} | ||
j.dir = testDir | ||
j.errs = err.NewErrors(j.dir) | ||
j.errs.Critical(jobErr) | ||
|
||
assert.Len(t, j.Errors().GetAll(), 1) | ||
assert.Contains(t, j.Errors().GetAll(), jobErr) | ||
} | ||
|
||
func TestSendStatus(t *testing.T) { | ||
j := BaseJob{ | ||
files: testFiles, | ||
dir: testDir, | ||
errs: nil, | ||
status: make(chan string), | ||
} | ||
|
||
go func() { | ||
status := <-j.ReceiveStatus() | ||
assert.Equal(t, "status", status) | ||
}() | ||
|
||
j.SendStatus("status") | ||
} | ||
|
||
func TestDifferentNewBaseJob(t *testing.T) { | ||
differentDir := "testDifferentDir" | ||
differentFiles := []string{"testDifferentFile"} | ||
j := NewBaseJob(differentDir, differentFiles) | ||
assert.NotEqual(t, testFiles, j.GetFiles()) | ||
assert.Equal(t, differentFiles, j.GetFiles()) | ||
assert.NotEqual(t, testDir, j.GetDir()) | ||
assert.Equal(t, differentDir, j.GetDir()) | ||
assert.NotNil(t, j.Errors()) | ||
assert.NotNil(t, j.status) | ||
} | ||
|
||
func TestGetExitErrorWithExitError(t *testing.T) { | ||
err := &exec.ExitError{ | ||
ProcessState: nil, | ||
Stderr: []byte("stderr"), | ||
} | ||
j := BaseJob{} | ||
exitErr := j.GetExitError(err) | ||
assert.ErrorContains(t, exitErr, string(err.Stderr)) | ||
} | ||
|
||
func TestGetExitErrorWithNoneExitError(t *testing.T) { | ||
err := &exec.Error{Err: errors.New("none-exit-err")} | ||
j := BaseJob{} | ||
exitErr := j.GetExitError(err) | ||
assert.ErrorContains(t, exitErr, err.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package testdata | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/debricked/cli/pkg/resolution/job" | ||
) | ||
|
||
type JobMock struct { | ||
dir string | ||
files []string | ||
errs job.IErrors | ||
status chan string | ||
} | ||
|
||
func (j *JobMock) ReceiveStatus() chan string { | ||
return j.status | ||
} | ||
|
||
func (j *JobMock) GetDir() string { | ||
return j.dir | ||
} | ||
|
||
func (j *JobMock) GetFiles() string { | ||
return j.file | ||
} | ||
|
||
func (j *JobMock) Errors() job.IErrors { | ||
return j.errs | ||
} | ||
|
||
func (j *JobMock) Run() { | ||
fmt.Println("job mock run") | ||
} | ||
|
||
func NewJobMock(dir string, files []string) *JobMock { | ||
return &JobMock{ | ||
dir: dir, | ||
files: files, | ||
status: make(chan string), | ||
errs: job.NewErrors(file), | ||
} | ||
} | ||
|
||
func (j *JobMock) SetErr(err job.IError) { | ||
j.errs.Critical(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package testdata | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/debricked/cli/pkg/callgraph/job" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func AssertPathErr(t *testing.T, jobErrs job.IErrors) { | ||
var path string | ||
if runtime.GOOS == "windows" { | ||
path = "%PATH%" | ||
} else { | ||
path = "$PATH" | ||
} | ||
errs := jobErrs.GetAll() | ||
assert.Len(t, errs, 1) | ||
err := errs[0] | ||
errMsg := fmt.Sprintf("executable file not found in %s", path) | ||
assert.ErrorContains(t, err, errMsg) | ||
} | ||
|
||
func WaitStatus(j job.IJob) { | ||
for { | ||
<-j.ReceiveStatus() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package java | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMakeGradleCopyDependenciesCmd(t *testing.T) { | ||
workingDir := "dir" | ||
gradlew := "gradlew" | ||
groovyFilePath := "groovyfilename" | ||
cmd, err := CmdFactory{}.MakeGradleCopyDependenciesCmd(workingDir, gradlew, groovyFilePath) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, cmd) | ||
args := cmd.Args | ||
assert.Contains(t, args, "gradlew") | ||
assert.Contains(t, args, "groovyfilename") | ||
assert.ErrorContains(t, err, "executable file not found in") | ||
assert.ErrorContains(t, err, "PATH") | ||
} | ||
|
||
func TestMakeMvnCopyDependenciesCmd(t *testing.T) { | ||
workingDir := "dir" | ||
targetDir := "target" | ||
cmd, err := CmdFactory{}.MakeMvnCopyDependenciesCmd(workingDir, targetDir) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, cmd) | ||
args := cmd.Args | ||
assert.Contains(t, args, "dir") | ||
assert.Contains(t, args, "target") | ||
} | ||
|
||
func TestMakeCallGraphGenerationCmd(t *testing.T) { | ||
jarPath := "jarpath" | ||
workingDir := "dir" | ||
targetClasses := "targetclasses" | ||
dependencyClasses := "dependencypath" | ||
cmd, err := CmdFactory{}.MakeCallGraphGenerationCmd(jarPath, workingDir, targetClasses, dependencyClasses) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, cmd) | ||
args := cmd.Args | ||
assert.Contains(t, args, "jarpath") | ||
assert.Contains(t, args, "dir") | ||
assert.Contains(t, args, "targetclasses") | ||
assert.Contains(t, args, "dependencypath") | ||
} |
32 changes: 32 additions & 0 deletions
32
pkg/callgraph/language/java11/testdata/cmd_factory_mock.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package testdata | ||
|
||
import "os/exec" | ||
|
||
type CmdFactoryMock struct { | ||
GradleCopyDepName string | ||
GradleCopyDepErr error | ||
MvnCopyDepName string | ||
MvnCopyDepErr error | ||
CallGraphGenName string | ||
CallGraphGenErr error | ||
} | ||
|
||
func NewEchoCmdFactory() CmdFactoryMock { | ||
return CmdFactoryMock{ | ||
GradleCopyDepName: "echo", | ||
MvnCopyDepName: "echo", | ||
CallGraphGenName: "echo", | ||
} | ||
} | ||
|
||
func (f CmdFactoryMock) MakeGradleCopyDependenciesCmd(_ string, _ string, _ string) (*exec.Cmd, error) { | ||
return exec.Command(f.GradleCopyDepName, "GradleCopyDep"), f.GradleCopyDepErr | ||
} | ||
|
||
func (f CmdFactoryMock) MakeMvnCopyDependenciesCmd(_ string, _ string) (*exec.Cmd, error) { | ||
return exec.Command(f.MvnCopyDepName, "MvnCopyDep"), f.MvnCopyDepErr | ||
} | ||
|
||
func (f CmdFactoryMock) MakeCallGraphGenerationCmd(_ string, _ string, _ string, _ string) (*exec.Cmd, error) { | ||
return exec.Command(f.CallGraphGenName, "CallGraphGen"), f.CallGraphGenErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters