Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inclusion and refactor exclusion #217

Merged
merged 9 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Changed behaviours
- Changes default strictness of resolve command to 1 (Exit with code 1 if all files failed to resolve, otherwise exit with code 0 instead of always exiting with code 0)
- File Fingerprint analysis is on by default, rolling roll-out starting with all repositories that start with the letter "C".
- Added inclusion option to commands to force include patterns which are by default ignored by the CLI
- Refactored how exclusion works for fingerprinting to align it with the rest of the CLI, this includes a breaking change for windows where Unix path separators must be used in patterns.

## Runtime upgrades

Expand Down
2 changes: 1 addition & 1 deletion internal/callgraph/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package finder
type IFinder interface {
FindRoots(files []string) ([]string, error)
FindDependencyDirs(files []string, findJars bool) ([]string, error)
FindFiles(paths []string, exclusions []string) ([]string, error)
FindFiles(paths []string, exclusions []string, inclusions []string) ([]string, error)
}
4 changes: 2 additions & 2 deletions internal/callgraph/finder/golangfinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (f GolangFinder) FindDependencyDirs(files []string, findJars bool) ([]strin
return []string{}, nil
}

func (f GolangFinder) FindFiles(roots []string, exclusions []string) ([]string, error) {
func (f GolangFinder) FindFiles(roots []string, exclusions []string, inclusions []string) ([]string, error) {
files := make(map[string]bool)
var err error = nil

Expand All @@ -78,7 +78,7 @@ func (f GolangFinder) FindFiles(roots []string, exclusions []string) ([]string,
return err
}

excluded := file.Excluded(exclusions, path)
excluded := file.Excluded(exclusions, inclusions, path)

if info.IsDir() && excluded {
return filepath.SkipDir
Expand Down
4 changes: 2 additions & 2 deletions internal/callgraph/finder/golangfinder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGolangFindEntrypointNoMain(t *testing.T) {

func TestFindFiles(t *testing.T) {
f := GolangFinder{}
files, err := f.FindFiles([]string{"testdata"}, nil)
files, err := f.FindFiles([]string{"testdata"}, nil, nil)
assert.Nil(t, err)
assert.NotEmpty(t, files)
assert.Contains(t, files, filepath.Join("testdata", "app.go"))
Expand All @@ -40,7 +40,7 @@ func TestFindDependencyDirs(t *testing.T) {

func TestFindFilesExclusions(t *testing.T) {
finder := GolangFinder{}
files, err := finder.FindFiles([]string{"testdata"}, []string{"testdata"})
files, err := finder.FindFiles([]string{"testdata"}, []string{"testdata"}, nil)
assert.Nil(t, err)
assert.Empty(t, files)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/callgraph/finder/javafinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (f JavaFinder) FindDependencyDirs(files []string, findJars bool) ([]string,
return dirJarFiles, nil
}

func (f JavaFinder) FindFiles(roots []string, exclusions []string) ([]string, error) {
func (f JavaFinder) FindFiles(roots []string, exclusions []string, inclusions []string) ([]string, error) {
files := make(map[string]bool)
var err error = nil

Expand All @@ -48,7 +48,7 @@ func (f JavaFinder) FindFiles(roots []string, exclusions []string) ([]string, er
return err
}

excluded := file.Excluded(exclusions, path)
excluded := file.Excluded(exclusions, inclusions, path)

if info.IsDir() && excluded {
return filepath.SkipDir
Expand Down
14 changes: 7 additions & 7 deletions internal/callgraph/finder/javafinder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func TestFindDependencyDirs(t *testing.T) {

func TestFindFiles(t *testing.T) {
f := JavaFinder{}
files, err := f.FindFiles([]string{"."}, nil)
files, err := f.FindFiles([]string{"."}, nil, nil)
assert.Nil(t, err)
assert.NotEmpty(t, files)
}

func TestFindFilesErr(t *testing.T) {
f := JavaFinder{}
files, err := f.FindFiles([]string{"totaly-not-a-valid-path-123123123"}, nil)
files, err := f.FindFiles([]string{"totaly-not-a-valid-path-123123123"}, nil, nil)
assert.NotNil(t, err)
assert.Empty(t, files)
}
Expand All @@ -52,19 +52,19 @@ func TestFindFilesExcluded(t *testing.T) {
f := JavaFinder{}
project_path, err := filepath.Abs("testdata/test_project")
assert.Nil(t, err)
files, err := f.FindFiles([]string{project_path}, nil)
files, err := f.FindFiles([]string{project_path}, nil, nil)
assert.Nil(t, err)
assert.Len(t, files, 2)
files, err = f.FindFiles([]string{project_path}, []string{"**/excluded_folder/**"})
files, err = f.FindFiles([]string{project_path}, []string{"**/excluded_folder/**"}, nil)
assert.Nil(t, err)
assert.Len(t, files, 1)
files, err = f.FindFiles([]string{project_path}, []string{"excluded_folder"})
files, err = f.FindFiles([]string{project_path}, []string{"excluded_folder"}, nil)
assert.Nil(t, err)
assert.Len(t, files, 2)
files, err = f.FindFiles([]string{project_path}, []string{"**/excluded*/**"})
files, err = f.FindFiles([]string{project_path}, []string{"**/excluded*/**"}, nil)
assert.Nil(t, err)
assert.Len(t, files, 1)
files, err = f.FindFiles([]string{project_path}, []string{"**/excluded_file.txt"})
files, err = f.FindFiles([]string{project_path}, []string{"**/excluded_file.txt"}, nil)
assert.Nil(t, err)
assert.Len(t, files, 1)
}
2 changes: 1 addition & 1 deletion internal/callgraph/finder/testdata/finder_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func (f FinderMock) FindRoots(_ []string) ([]string, error) {
return f.FindRootsNames, f.FindRootsErr
}

func (f FinderMock) FindFiles(_ []string, _ []string) ([]string, error) {
func (f FinderMock) FindFiles(_ []string, _ []string, _ []string) ([]string, error) {
return f.FindFilesNames, f.FindFilesErr
}
26 changes: 17 additions & 9 deletions internal/callgraph/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import (
"github.com/debricked/cli/internal/tui"
)

type DebrickedOptions struct {
Paths []string
Exclusions []string
Inclusions []string
Configs []config.IConfig
Timeout int
}

type IGenerator interface {
GenerateWithTimer(paths []string, exclusions []string, configs []config.IConfig, timeout int) error
Generate(paths []string, exclusions []string, configs []config.IConfig, ctx cgexec.IContext) error
GenerateWithTimer(options DebrickedOptions) error
Generate(options DebrickedOptions, ctx cgexec.IContext) error
}

type Generator struct {
Expand All @@ -32,13 +40,13 @@ func NewGenerator(
}
}

func (g *Generator) GenerateWithTimer(paths []string, exclusions []string, configs []config.IConfig, timeout int) error {
func (g *Generator) GenerateWithTimer(options DebrickedOptions) error {
result := make(chan error)
ctx, cancel := cgexec.NewContext(timeout)
ctx, cancel := cgexec.NewContext(options.Timeout)
defer cancel()

go func() {
result <- g.Generate(paths, exclusions, configs, &ctx)
result <- g.Generate(options, &ctx)
}()

// Wait for the result or timeout
Expand All @@ -47,14 +55,14 @@ func (g *Generator) GenerateWithTimer(paths []string, exclusions []string, confi
return err
}

func (g *Generator) Generate(paths []string, exclusions []string, configs []config.IConfig, ctx cgexec.IContext) error {
func (g *Generator) Generate(options DebrickedOptions, ctx cgexec.IContext) error {
targetPath := ".debrickedTmpFolder"
debrickedExclusions := []string{targetPath}
exclusions = append(exclusions, debrickedExclusions...)
exclusions := append(options.Exclusions, debrickedExclusions...)

var jobs []job.IJob
for _, config := range configs {
s, strategyErr := g.strategyFactory.Make(config, paths, exclusions, ctx)
for _, config := range options.Configs {
s, strategyErr := g.strategyFactory.Make(config, options.Paths, exclusions, options.Inclusions, ctx)
if strategyErr == nil {
newJobs, err := s.Invoke()
if err != nil {
Expand Down
42 changes: 37 additions & 5 deletions internal/callgraph/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ func TestGenerate(t *testing.T) {
config.NewConfig("java", []string{}, map[string]string{"pm": "maven"}, true, "maven"),
}
ctx, _ := ctxTestdata.NewContextMock()
err := g.Generate([]string{"../../go.mod"}, nil, configs, ctx)
err := g.Generate(
DebrickedOptions{
Paths: []string{"../../go.mod"},
Exclusions: []string{},
Inclusions: []string{},
Configs: configs,
}, ctx)
assert.NoError(t, err)
assert.NotEmpty(t, g.Generation.Jobs())
}
Expand All @@ -47,7 +53,15 @@ func TestGenerateWithTimer(t *testing.T) {
configs := []config.IConfig{
config.NewConfig("java", []string{}, map[string]string{"pm": "maven"}, true, "maven"),
}
err := g.GenerateWithTimer([]string{"../../go.mod"}, nil, configs, 1000)
err := g.GenerateWithTimer(
DebrickedOptions{
Paths: []string{"../../go.mod"},
Exclusions: nil,
Inclusions: nil,
Configs: configs,
Timeout: 1000,
},
)
assert.NoError(t, err)
assert.NotEmpty(t, g.Generation.Jobs())
}
Expand All @@ -62,7 +76,13 @@ func TestGenerateInvokeError(t *testing.T) {
config.NewConfig("java", []string{}, map[string]string{"pm": "maven"}, true, "maven"),
}
ctx, _ := ctxTestdata.NewContextMock()
err := g.Generate([]string{"../../go.mod"}, nil, configs, ctx)
err := g.Generate(
DebrickedOptions{
Paths: []string{"../../go.mod"},
Exclusions: []string{},
Inclusions: []string{},
Configs: configs,
}, ctx)
assert.NotNil(t, err)
}

Expand All @@ -77,7 +97,13 @@ func TestGenerateScheduleError(t *testing.T) {
config.NewConfig("java", []string{}, map[string]string{"pm": "maven"}, true, "maven"),
}
ctx, _ := ctxTestdata.NewContextMock()
err := g.Generate([]string{"../../go.mod"}, nil, configs, ctx)
err := g.Generate(
DebrickedOptions{
Paths: []string{"../../go.mod"},
Exclusions: []string{},
Inclusions: []string{},
Configs: configs,
}, ctx)
assert.NotEmpty(t, g.Generation.Jobs())
assert.ErrorIs(t, err, errAssertion)
}
Expand All @@ -89,7 +115,13 @@ func TestGenerateDirWithoutConfig(t *testing.T) {
)

ctx, _ := ctxTestdata.NewContextMock()
err := g.Generate([]string{"invalid-dir"}, nil, nil, ctx)
err := g.Generate(
DebrickedOptions{
Paths: []string{"invalid-dir"},
Exclusions: []string{},
Inclusions: []string{},
Configs: nil,
}, ctx)
assert.Empty(t, g.Generation.Jobs())
assert.NoError(t, err)
}
7 changes: 4 additions & 3 deletions internal/callgraph/language/golang/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Strategy struct {
config conf.IConfig
paths []string
exclusions []string
inclusions []string
finder finder.IFinder
ctx cgexec.IContext
}
Expand All @@ -32,7 +33,7 @@ func (s Strategy) Invoke() ([]job.IJob, error) {

for _, path := range s.paths {

files, err := s.finder.FindFiles([]string{path}, s.exclusions)
files, err := s.finder.FindFiles([]string{path}, s.exclusions, s.inclusions)
if err != nil {
strategyWarning("Error while finding files: " + err.Error())

Expand Down Expand Up @@ -71,8 +72,8 @@ func (s Strategy) Invoke() ([]job.IJob, error) {
return jobs, nil
}

func NewStrategy(config conf.IConfig, paths []string, exclusions []string, finder finder.IFinder, ctx cgexec.IContext) Strategy {
return Strategy{config, paths, exclusions, finder, ctx}
func NewStrategy(config conf.IConfig, paths []string, exclusions []string, inclusions []string, finder finder.IFinder, ctx cgexec.IContext) Strategy {
return Strategy{config, paths, exclusions, inclusions, finder, ctx}
}

func strategyWarning(errMsg string) {
Expand Down
22 changes: 11 additions & 11 deletions internal/callgraph/language/golang/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ import (
)

func TestNewStrategy(t *testing.T) {
s := NewStrategy(nil, nil, nil, nil, nil)
s := NewStrategy(nil, nil, nil, nil, nil, nil)
assert.NotNil(t, s)

s = NewStrategy(nil, []string{}, []string{}, nil, nil)
s = NewStrategy(nil, []string{}, []string{}, []string{}, nil, nil)
assert.NotNil(t, s)

s = NewStrategy(nil, []string{"file"}, []string{}, nil, nil)
s = NewStrategy(nil, []string{"file"}, []string{}, []string{}, nil, nil)
assert.NotNil(t, s)

s = NewStrategy(nil, []string{"file-1", "file-2"}, []string{}, nil, nil)
s = NewStrategy(nil, []string{"file-1", "file-2"}, []string{}, []string{}, nil, nil)
assert.NotNil(t, s)

conf := config.NewConfig("golang", []string{"arg1"}, map[string]string{"kwarg": "val"}, true, "go")
finder := testdata.NewEmptyFinderMock()
testFiles := []string{"file-1"}
finder.FindRootsNames = testFiles
ctx, _ := ctxTestdata.NewContextMock()
s = NewStrategy(conf, []string{"."}, []string{}, finder, ctx)
s = NewStrategy(conf, []string{"."}, []string{}, []string{}, finder, ctx)
assert.NotNil(t, s)
assert.Equal(t, s.config, conf)
}

func TestInvokeNoFiles(t *testing.T) {
s := NewStrategy(nil, []string{}, []string{}, nil, nil)
s := NewStrategy(nil, []string{}, []string{}, []string{}, nil, nil)
jobs, _ := s.Invoke()
assert.Empty(t, jobs)
}
Expand All @@ -44,7 +44,7 @@ func TestInvokeOneFile(t *testing.T) {
testFiles := []string{"file-1"}
finder.FindRootsNames = testFiles
ctx, _ := ctxTestdata.NewContextMock()
s := NewStrategy(conf, []string{"."}, []string{}, finder, ctx)
s := NewStrategy(conf, []string{"."}, []string{}, []string{}, finder, ctx)
jobs, err := s.Invoke()
assert.NoError(t, err)
assert.Len(t, jobs, 1)
Expand All @@ -56,7 +56,7 @@ func TestInvokeManyFiles(t *testing.T) {
testFiles := []string{"file-1", "file-2"}
finder.FindRootsNames = testFiles
ctx, _ := ctxTestdata.NewContextMock()
s := NewStrategy(conf, []string{"."}, []string{}, finder, ctx)
s := NewStrategy(conf, []string{"."}, []string{}, []string{}, finder, ctx)
jobs, _ := s.Invoke()
assert.Len(t, jobs, 2)
}
Expand All @@ -68,14 +68,14 @@ func TestInvokeWithErrors(t *testing.T) {
finder.FindRootsNames = testFiles
finder.FindRootsErr = assert.AnError
ctx, _ := ctxTestdata.NewContextMock()
s := NewStrategy(conf, []string{"."}, []string{}, finder, ctx)
s := NewStrategy(conf, []string{"."}, []string{}, []string{}, finder, ctx)
jobs, err := s.Invoke()
assert.Error(t, err)
assert.Empty(t, jobs)

finder.FindRootsErr = nil
finder.FindFilesErr = assert.AnError
s = NewStrategy(conf, []string{"."}, []string{}, finder, ctx)
s = NewStrategy(conf, []string{"."}, []string{}, []string{}, finder, ctx)
jobs, err = s.Invoke()
assert.Error(t, err)
assert.Empty(t, jobs)
Expand All @@ -87,7 +87,7 @@ func TestInvokeNoRoots(t *testing.T) {
testFiles := []string{}
finder.FindRootsNames = testFiles
ctx, _ := ctxTestdata.NewContextMock()
s := NewStrategy(conf, []string{"."}, []string{}, finder, ctx)
s := NewStrategy(conf, []string{"."}, []string{}, []string{}, finder, ctx)
jobs, err := s.Invoke()
assert.NoError(t, err)
assert.Empty(t, jobs)
Expand Down
9 changes: 5 additions & 4 deletions internal/callgraph/language/java11/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Strategy struct {
cmdFactory ICmdFactory
paths []string
exclusions []string
inclusions []string
finder finder.IFinder
ctx cgexec.IContext
}
Expand All @@ -36,7 +37,7 @@ func (s Strategy) Invoke() ([]job.IJob, error) {

var roots []string
var err error
files, err := s.finder.FindFiles(s.paths, s.exclusions)
files, err := s.finder.FindFiles(s.paths, s.exclusions, s.inclusions)
if err != nil {
strategyWarning("Error while finding files: " + err.Error())

Expand Down Expand Up @@ -67,7 +68,7 @@ func (s Strategy) Invoke() ([]job.IJob, error) {
}

// If build, then we need to find the newly built files
files, _ = s.finder.FindFiles(s.paths, s.exclusions)
files, _ = s.finder.FindFiles(s.paths, s.exclusions, s.inclusions)
}

javaClassDirs, _ := s.finder.FindDependencyDirs(files, false)
Expand Down Expand Up @@ -104,8 +105,8 @@ func (s Strategy) Invoke() ([]job.IJob, error) {
return jobs, nil
}

func NewStrategy(config conf.IConfig, paths []string, exclusions []string, finder finder.IFinder, ctx cgexec.IContext) Strategy {
return Strategy{config, CmdFactory{}, paths, exclusions, finder, ctx}
func NewStrategy(config conf.IConfig, paths []string, exclusions []string, inclusions []string, finder finder.IFinder, ctx cgexec.IContext) Strategy {
return Strategy{config, CmdFactory{}, paths, exclusions, inclusions, finder, ctx}
}

func strategyWarning(errMsg string) {
Expand Down
Loading
Loading