Skip to content

Commit

Permalink
Add first iteration of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed May 11, 2023
1 parent 49e984a commit 71bc9bf
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/callgraph/job/base_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewBaseJob(dir string, files []string) BaseJob {
return BaseJob{
dir: dir,
files: files,
errs: err.NewErrors("test"),
errs: err.NewErrors(dir),
status: make(chan string),
}
}
Expand Down
102 changes: 102 additions & 0 deletions pkg/callgraph/job/base_job_test.go
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())
}
47 changes: 47 additions & 0 deletions pkg/callgraph/job/testdata/job_mock.go
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)
}
30 changes: 30 additions & 0 deletions pkg/callgraph/job/testdata/job_test_util.go
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()
}
}
50 changes: 50 additions & 0 deletions pkg/callgraph/language/java11/cmd_factory_test.go
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 pkg/callgraph/language/java11/testdata/cmd_factory_mock.go
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
}
2 changes: 1 addition & 1 deletion pkg/cmd/callgraph/callgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func RunE(callgraph callgraph.IGenerator) func(_ *cobra.Command, args []string)
}
configs := []conf.IConfig{
conf.NewConfig("java", []string{}, map[string]string{"pm": "maven"}),
conf.NewConfig("java", []string{}, map[string]string{"pm": "gradle"}),
// conf.NewConfig("java", []string{}, map[string]string{"pm": "gradle"}),
}

// err := callgraph.GenerateWithTimer(args, viper.GetStringSlice(ExclusionFlag), configs, 10)
Expand Down

0 comments on commit 71bc9bf

Please sign in to comment.