Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed May 15, 2023
1 parent 26591f1 commit 19c5c53
Show file tree
Hide file tree
Showing 25 changed files with 965 additions and 101 deletions.
4 changes: 2 additions & 2 deletions pkg/callgraph/language/java11/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

conf "github.com/debricked/cli/pkg/callgraph/config"
"github.com/debricked/cli/pkg/callgraph/job"
"github.com/debricked/cli/pkg/io/finder"
gfinder "github.com/debricked/cli/pkg/io/finder/gradle"
ioWriter "github.com/debricked/cli/pkg/io/writer"
)

Expand Down Expand Up @@ -52,7 +52,7 @@ func (j *Job) Run() {
}

groovyFilePath := path.Join(workingDirectory, ".debrickedGroovyScript.groovy")
ish := finder.NewScriptHandler(groovyFilePath, "embeded/gradle-script.groovy", ioWriter.FileWriter{})
ish := gfinder.NewScriptHandler(groovyFilePath, "embeded/gradle-script.groovy", ioWriter.FileWriter{})
ish.WriteInitFile()

cmd, err = j.cmdFactory.MakeGradleCopyDependenciesCmd(workingDirectory, gradlew, groovyFilePath)
Expand Down
9 changes: 6 additions & 3 deletions pkg/callgraph/language/java11/strategy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package java

import (
"path/filepath"
"testing"

"github.com/debricked/cli/pkg/callgraph/config"
Expand Down Expand Up @@ -65,14 +66,16 @@ func TestInvokeManyFilesWCorrectFilters(t *testing.T) {
conf := config.NewConfig("java", []string{"arg1"}, map[string]string{"kwarg": "val"})
finder := testdata.NewEmptyFinderMock()
testFiles := []string{"file-1", "file-2", "file-3"}
finder.FindMavenRootsNames = []string{"file-3"}
finder.FindMavenRootsNames = []string{"file-3/pom.xml"}
finder.FindJavaClassDirsNames = []string{"file-3/test.class"}
s := NewStrategy(conf, testFiles, finder)
jobs, _ := s.Invoke()
assert.Len(t, jobs, 1)
for _, job := range jobs {
assert.Equal(t, job.GetFiles(), []string{"file-3/"})
assert.Equal(t, job.GetDir(), "file-3")
file, _ := filepath.Abs("file-3/")
dir, _ := filepath.Abs("file-3/")
assert.Equal(t, job.GetFiles(), []string{file + "/"}) // Get files return gcd path
assert.Equal(t, job.GetDir(), dir)

}
}
15 changes: 10 additions & 5 deletions pkg/io/finder/finder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package finder

import "path/filepath"
import (
"path/filepath"

"github.com/debricked/cli/pkg/io/finder/gradle"
"github.com/debricked/cli/pkg/io/finder/maven"
)

type IFinder interface {
FindMavenRoots(files []string) ([]string, error)
Expand All @@ -12,7 +17,7 @@ type Finder struct{}

func (f Finder) FindMavenRoots(files []string) ([]string, error) {
pomFiles := FilterFiles(files, "pom.xml")
ps := PomService{}
ps := maven.PomService{}
rootFiles := ps.GetRootPomFiles(pomFiles)
return rootFiles, nil
}
Expand All @@ -34,7 +39,7 @@ func (f Finder) FindJavaClassDirs(files []string) ([]string, error) {

func (f Finder) FindGradleRoots(files []string) ([]string, error) {
gradleBuildFiles := FilterFiles(files, "gradle.build(.kts)?")
gradleSetup := NewGradleSetup()
gradleSetup := gradle.NewGradleSetup()
err := gradleSetup.Configure(files)
if err != nil {

Expand All @@ -43,15 +48,15 @@ func (f Finder) FindGradleRoots(files []string) ([]string, error) {

gradleMainDirs := make(map[string]bool)
for _, gradleProject := range gradleSetup.GradleProjects {
dir := gradleProject.dir
dir := gradleProject.Dir
if _, ok := gradleMainDirs[dir]; ok {
continue
}
gradleMainDirs[dir] = true
}
for _, file := range gradleBuildFiles {
dir, _ := filepath.Abs(filepath.Dir(file))
if _, ok := gradleSetup.subProjectMap[dir]; ok {
if _, ok := gradleSetup.SubProjectMap[dir]; ok {
continue
}
if _, ok := gradleMainDirs[dir]; ok {
Expand Down
18 changes: 18 additions & 0 deletions pkg/io/finder/gradle/embeded/gradle-init-script.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def debrickedOutputFile = new File('.debricked.multiprojects.txt')

allprojects {
task debrickedFindSubProjectPaths() {
String output = project.projectDir
doLast {
synchronized(debrickedOutputFile) {
debrickedOutputFile << output + System.getProperty("line.separator")
}
}
}
}

allprojects {
task debrickedAllDeps(type: DependencyReportTask) {
outputFile = file('./.debricked-gradle-dependencies.txt')
}
}
2 changes: 1 addition & 1 deletion pkg/io/finder/err.go → pkg/io/finder/gradle/err.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package finder
package gradle

type SetupScriptError struct {
message string
Expand Down
62 changes: 31 additions & 31 deletions pkg/io/finder/gradle.go → pkg/io/finder/gradle/gradle.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package finder
package gradle

import (
"bufio"
Expand Down Expand Up @@ -28,18 +28,18 @@ type ISetup interface {
}

type Project struct {
dir string
gradlew string
mainBuildFile string
Dir string
Gradlew string
MainBuildFile string
}

type Setup struct {
gradlewMap map[string]string
settingsMap map[string]string
subProjectMap map[string]string
groovyScriptPath string
gradlewOsName string
settingsFilenames []string
GradlewMap map[string]string
SettingsMap map[string]string
SubProjectMap map[string]string
GroovyScriptPath string
GradlewOsName string
SettingsFilenames []string
GradleProjects []Project
MetaFileFinder IMetaFileFinder
Writer writer.IFileWriter
Expand All @@ -57,12 +57,12 @@ func NewGradleSetup() *Setup {
ish := InitScriptHandler{groovyScriptPath, "embeded/gradle-init-script.groovy", writer}

return &Setup{
gradlewMap: map[string]string{},
settingsMap: map[string]string{},
subProjectMap: map[string]string{},
groovyScriptPath: groovyScriptPath,
gradlewOsName: gradlewOsName,
settingsFilenames: []string{"settings.gradle", "settings.gradle.kts"},
GradlewMap: map[string]string{},
SettingsMap: map[string]string{},
SubProjectMap: map[string]string{},
GroovyScriptPath: groovyScriptPath,
GradlewOsName: gradlewOsName,
SettingsFilenames: []string{"settings.gradle", "settings.gradle.kts"},
GradleProjects: []Project{},
MetaFileFinder: MetaFileFinder{filepath: FilePath{}},
Writer: writer,
Expand All @@ -78,8 +78,8 @@ func (gs *Setup) Configure(files []string) error {
return err
}
settingsMap, gradlewMap, err := gs.MetaFileFinder.Find(files)
gs.gradlewMap = gradlewMap
gs.settingsMap = settingsMap
gs.GradlewMap = gradlewMap
gs.SettingsMap = settingsMap
if err != nil {

return err
Expand All @@ -96,16 +96,16 @@ func (gs *Setup) Configure(files []string) error {
func (gs *Setup) setupFilePathMappings(files []string) {
for _, file := range files {
dir, _ := filepath.Abs(filepath.Dir(file))
possibleGradlew := filepath.Join(dir, gs.gradlewOsName)
possibleGradlew := filepath.Join(dir, gs.GradlewOsName)
_, err := os.Stat(possibleGradlew)
if err == nil {
gs.gradlewMap[dir] = possibleGradlew
gs.GradlewMap[dir] = possibleGradlew
}
for _, settingsFilename := range gs.settingsFilenames {
for _, settingsFilename := range gs.SettingsFilenames {
possibleSettings := filepath.Join(dir, settingsFilename)
_, err := os.Stat(possibleSettings)
if err == nil {
gs.settingsMap[dir] = possibleSettings
gs.SettingsMap[dir] = possibleSettings
}
}
}
Expand All @@ -114,17 +114,17 @@ func (gs *Setup) setupFilePathMappings(files []string) {
func (gs *Setup) setupGradleProjectMappings() error {
var errors SetupError
var settingsDirs []string
for k := range gs.settingsMap {
for k := range gs.SettingsMap {
settingsDirs = append(settingsDirs, k)
}
sort.Strings(settingsDirs)
for _, dir := range settingsDirs {
if _, ok := gs.subProjectMap[dir]; ok {
if _, ok := gs.SubProjectMap[dir]; ok {
continue
}
gradlew := gs.GetGradleW(dir)
mainFile := gs.settingsMap[dir]
gradleProject := Project{dir: dir, gradlew: gradlew, mainBuildFile: mainFile}
mainFile := gs.SettingsMap[dir]
gradleProject := Project{Dir: dir, Gradlew: gradlew, MainBuildFile: mainFile}
err := gs.setupSubProjectPaths(gradleProject)

if err != nil {
Expand Down Expand Up @@ -152,7 +152,7 @@ func (cf CmdFactory) MakeFindSubGraphCmd(workingDirectory string, gradlew string
}

func (gs *Setup) setupSubProjectPaths(gp Project) error {
dependenciesCmd, _ := gs.CmdFactory.MakeFindSubGraphCmd(gp.dir, gp.gradlew, gs.groovyScriptPath)
dependenciesCmd, _ := gs.CmdFactory.MakeFindSubGraphCmd(gp.Dir, gp.Gradlew, gs.GroovyScriptPath)
var stderr bytes.Buffer
dependenciesCmd.Stderr = &stderr
_, err := dependenciesCmd.Output()
Expand All @@ -162,7 +162,7 @@ func (gs *Setup) setupSubProjectPaths(gp Project) error {

return SetupSubprojectError{message: errorOutput + err.Error()}
}
multiProject := filepath.Join(gp.dir, multiProjectFilename)
multiProject := filepath.Join(gp.Dir, multiProjectFilename)
file, err := os.Open(multiProject)
if err != nil {

Expand All @@ -174,7 +174,7 @@ func (gs *Setup) setupSubProjectPaths(gp Project) error {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
subProjectPath := scanner.Text()
gs.subProjectMap[subProjectPath] = gp.dir
gs.SubProjectMap[subProjectPath] = gp.Dir
}

if err := scanner.Err(); err != nil {
Expand All @@ -186,11 +186,11 @@ func (gs *Setup) setupSubProjectPaths(gp Project) error {

func (gs *Setup) GetGradleW(dir string) string {
gradlew := initGradle
val, ok := gs.gradlewMap[dir]
val, ok := gs.GradlewMap[dir]
if ok {
gradlew = val
} else {
for dirPath, gradlePath := range gs.gradlewMap {
for dirPath, gradlePath := range gs.GradlewMap {
// potential improvement, sort gradlewMap in longest path first"
rel, err := filepath.Rel(dirPath, dir)
isRelative := !strings.HasPrefix(rel, "..") && rel != ".."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package finder
package gradle

import (
"fmt"
Expand Down Expand Up @@ -38,35 +38,35 @@ func TestSetupFilePathMappings(t *testing.T) {
files := []string{filepath.Join("testdata", "project", "build.gradle")}
gs.setupFilePathMappings(files)

assert.Len(t, gs.gradlewMap, 1)
assert.Len(t, gs.settingsMap, 1)
assert.Len(t, gs.GradlewMap, 1)
assert.Len(t, gs.SettingsMap, 1)
}

func TestSetupFilePathMappingsNoFiles(t *testing.T) {
gs := NewGradleSetup()
gs.setupFilePathMappings([]string{})

assert.Len(t, gs.gradlewMap, 0)
assert.Len(t, gs.settingsMap, 0)
assert.Len(t, gs.GradlewMap, 0)
assert.Len(t, gs.SettingsMap, 0)
}

func TestSetupFilePathMappingsNoGradlew(t *testing.T) {
gs := NewGradleSetup()
files := []string{filepath.Join("testdata", "project", "subproject", "build.gradle")}
gs.setupFilePathMappings(files)

assert.Len(t, gs.gradlewMap, 0)
assert.Len(t, gs.settingsMap, 0)
assert.Len(t, gs.GradlewMap, 0)
assert.Len(t, gs.SettingsMap, 0)
}

func TestSetupGradleProjectMappings(t *testing.T) {
gs := NewGradleSetup()
gs.CmdFactory = &mockCmdFactory{}

gs.settingsMap = map[string]string{
gs.SettingsMap = map[string]string{
filepath.Join("testdata", "project"): filepath.Join("testdata", "project", "settings.gradle"),
}
gs.subProjectMap = map[string]string{}
gs.SubProjectMap = map[string]string{}
err := gs.setupGradleProjectMappings()
// assert GradleSetupSubprojectError
assert.NotNil(t, err)
Expand Down Expand Up @@ -108,35 +108,35 @@ func TestSetupSubProjectPathsNoFileCreated(t *testing.T) {
gs.CmdFactory = &mockCmdFactory{createFile: false}

absPath, _ := filepath.Abs(filepath.Join("testdata", "project"))
gradleProject := Project{dir: absPath, gradlew: filepath.Join("testdata", "project", "gradlew")}
gradleProject := Project{Dir: absPath, Gradlew: filepath.Join("testdata", "project", "gradlew")}
err := gs.setupSubProjectPaths(gradleProject)
fmt.Println(err)
assert.NotNil(t, err)
assert.Len(t, gs.subProjectMap, 0)
assert.Len(t, gs.SubProjectMap, 0)
}

func TestSetupSubProjectPaths(t *testing.T) {
gs := NewGradleSetup()
gs.CmdFactory = &mockCmdFactory{createFile: true}

absPath, _ := filepath.Abs(filepath.Join("testdata", "project"))
gradleProject := Project{dir: absPath, gradlew: filepath.Join("testdata", "project", "gradlew")}
gradleProject := Project{Dir: absPath, Gradlew: filepath.Join("testdata", "project", "gradlew")}
err := gs.setupSubProjectPaths(gradleProject)
assert.Nil(t, err)
assert.Len(t, gs.subProjectMap, 1)
assert.Len(t, gs.SubProjectMap, 1)

absPath, _ = filepath.Abs(filepath.Join("testdata", "project", "subproject"))
gradleProject = Project{dir: absPath, gradlew: filepath.Join("testdata", "project", "gradlew")}
gradleProject = Project{Dir: absPath, Gradlew: filepath.Join("testdata", "project", "gradlew")}
err = gs.setupSubProjectPaths(gradleProject)
assert.Nil(t, err)
assert.Len(t, gs.subProjectMap, 2)
assert.Len(t, gs.SubProjectMap, 2)
}

func TestSetupSubProjectPathsError(t *testing.T) {
gs := NewGradleSetup()

absPath, _ := filepath.Abs(filepath.Join("testdata", "project"))
gradleProject := Project{dir: absPath, gradlew: filepath.Join("testdata", "project", "gradlew")}
gradleProject := Project{Dir: absPath, Gradlew: filepath.Join("testdata", "project", "gradlew")}
err := gs.setupSubProjectPaths(gradleProject)

assert.NotNil(t, err)
Expand All @@ -145,7 +145,7 @@ func TestSetupSubProjectPathsError(t *testing.T) {
func TestGetGradleW(t *testing.T) {
gs := NewGradleSetup()

gs.gradlewMap = map[string]string{
gs.GradlewMap = map[string]string{
filepath.Join("testdata", "project"): filepath.Join("testdata", "project", "gradlew"),
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package finder
package gradle

import (
"github.com/debricked/cli/pkg/io/writer"
Expand Down
Loading

0 comments on commit 19c5c53

Please sign in to comment.