Skip to content

Commit

Permalink
log cases where no manifest or lock files were found. Provides detail…
Browse files Browse the repository at this point in the history
…s as to why no files were found.
  • Loading branch information
Duppils committed Jul 10, 2024
1 parent 4268a5a commit 89ffc5f
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 15 deletions.
81 changes: 66 additions & 15 deletions internal/file/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,20 @@ func (finder *Finder) GetConfigPath(rootPath string, exclusions []string, inclus
return configPath
}

// GetGroups return all file groups in specified path recursively.
func (finder *Finder) GetGroups(options DebrickedOptions) (Groups, error) {
func (finder *Finder) GetIncludedGroups(formats []*CompiledFormat, options DebrickedOptions) (Groups, error) {
// NOTE: inefficient because it walks into excluded directories
var groups Groups

formats, err := finder.GetSupportedFormats()
if err != nil {
return groups, err
}
if len(options.RootPath) == 0 {
options.RootPath = filepath.Base("")
}

// Traverse files to find dependency file groups
err = filepath.Walk(
err := filepath.Walk(
options.RootPath,
func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if !fileInfo.IsDir() && !Excluded(options.Exclusions, options.Inclusions, path) {
var excluded = Excluded(options.Exclusions, options.Inclusions, path)

if !fileInfo.IsDir() && !excluded {
for _, format := range formats {
if groups.Match(format, path, options.LockFileOnly) {

break
}
}
Expand All @@ -108,6 +99,66 @@ func (finder *Finder) GetGroups(options DebrickedOptions) (Groups, error) {
return nil
},
)
return groups, err
}

func (finder *Finder) GetExcludedGroups(formats []*CompiledFormat, options DebrickedOptions) (Groups, []string, error) {
var excludedGroups Groups
var excludedFiles []string
err := filepath.Walk(
options.RootPath,
func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if !fileInfo.IsDir() {
for _, format := range formats {
if excludedGroups.Match(format, path, options.LockFileOnly) {
excludedFiles = append(excludedFiles, path)
break
}
}
}
return nil
},
)
return excludedGroups, excludedFiles, err
}

// GetGroups return all file groups in specified path recursively.
func (finder *Finder) GetGroups(options DebrickedOptions) (Groups, error) {
var groups Groups
var noGroupsFound bool

formats, err := finder.GetSupportedFormats()
if err != nil {
return groups, err
}
if len(options.RootPath) == 0 {
options.RootPath = filepath.Base("")
}

// Traverse files to find dependency file groups
groups, err = finder.GetIncludedGroups(formats, options)
noGroupsFound = len(groups.groups) == 0
if noGroupsFound {
// No dependencies found. (should rarely happen)
// Traverse again to see if dependency files were excluded.
_, excludedFiles, excludedErr := finder.GetExcludedGroups(formats, options)
if len(excludedFiles) > 0 {
fmt.Println("The following files were excluded, resulting in no dependency files found.")
for _, file := range excludedFiles {
fmt.Println(file)
}
fmt.Println("Please change the inclusion and exclusion options if an important file or directory was missed.")
} else {
fmt.Println("No dependency file matches found with current configuration.")
fmt.Println("Please change the inclusion and exclusion options if an important file or directory was missed.")
}
if excludedErr != nil {
return groups, err
}
}

groups.FilterGroupsByStrictness(options.Strictness)

Expand Down
69 changes: 69 additions & 0 deletions internal/file/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
"os"
"path"
"path/filepath"
"sort"
Expand Down Expand Up @@ -185,6 +186,74 @@ func TestGetGroupsPIP(t *testing.T) {

}

func CaptureStdout(function func(options DebrickedOptions) (Groups, error), options DebrickedOptions) string {
oldStdout := os.Stdout
read, write, _ := os.Pipe()
os.Stdout = write

function(options)

write.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, read)
return buf.String()
}

func TestGetGroupsAllExcluded(t *testing.T) {
setUp(true)

options := DebrickedOptions{
RootPath: "testdata/misc",
Exclusions: []string{"**/**"},
Inclusions: []string{},
LockFileOnly: false,
Strictness: StrictAll,
}
actualOutput := CaptureStdout(finder.GetGroups, options)

// Define the expected output
expectedStart := "The following files were excluded, resulting in no dependency files found."
expectedEnd := "Please change the inclusion and exclusion options if an important file or directory was missed."
expectedExcludedFile := "requirements.txt"

// Compare the actual output to the expected output
if !strings.Contains(actualOutput, expectedStart) {
t.Errorf("Expected %q but got %q", expectedStart, actualOutput)
}
if !strings.Contains(actualOutput, expectedEnd) {
t.Errorf("Expected %q but got %q", expectedEnd, actualOutput)
}
if !strings.Contains(actualOutput, expectedExcludedFile) {
t.Errorf("Expected %q but got %q", expectedExcludedFile, actualOutput)
}
}

func TestGetGroupsAllExcludedByStrictness(t *testing.T) {
setUp(true)

options := DebrickedOptions{
RootPath: "testdata/misc",
Exclusions: []string{"**/composer.**"}, //the only manifest+lock pair in testdata/misc
Inclusions: []string{},
LockFileOnly: false,
Strictness: StrictPairs,
}
actualOutput := CaptureStdout(finder.GetGroups, options)

// Define the expected output
expectedStart := "The following files and directories were filtered out by strictness flag, resulting in no file matches."
expectedEnd := "Please change the inclusion and exclusion options if an important file or directory was missed."

// Compare the actual output to the expected output
if !strings.Contains(actualOutput, expectedStart) {
t.Errorf("Expected %q but got %q", expectedStart, actualOutput)
}
if !strings.Contains(actualOutput, expectedEnd) {
t.Errorf("Expected %q but got %q", expectedEnd, actualOutput)
}
}

func TestGetGroupsWithOnlyLockFiles(t *testing.T) {
setUp(true)
path := "testdata/misc"
Expand Down
9 changes: 9 additions & 0 deletions internal/file/groups.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"fmt"
"path/filepath"
)

Expand Down Expand Up @@ -79,6 +80,14 @@ func (gs *Groups) FilterGroupsByStrictness(strictness int) {
}
}

if len(groups) == 0 && len(gs.groups) > 0 {
fmt.Println("The following files and directories were filtered out by strictness flag, resulting in no file matches.")
for _, group := range gs.groups {
fmt.Println(group.GetAllFiles())
}
fmt.Println("Please change the inclusion and exclusion options if an important file or directory was missed.")
}

gs.groups = groups
}

Expand Down

0 comments on commit 89ffc5f

Please sign in to comment.