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

log cases where no manifest or lock files were found. Provides detail… #246

Merged
merged 2 commits into from
Jul 10, 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: 1 addition & 1 deletion build/docker/debian.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ RUN dotnet --version
RUN apt update -y && \
apt install -t unstable lsb-release apt-transport-https ca-certificates software-properties-common -y && \
curl -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg && \
sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' && \
sh -c 'echo "deb https://packages.sury.org/php/ bookworm main" > /etc/apt/sources.list.d/php.list' && \
apt -y clean && rm -rf /var/lib/apt/lists/*

RUN apt -y update && apt -y install \
Expand Down
85 changes: 72 additions & 13 deletions internal/file/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,47 @@ 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
err := filepath.Walk(
options.RootPath,
func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
var excluded = Excluded(options.Exclusions, options.Inclusions, path)

formats, err := finder.GetSupportedFormats()
if err != nil {
return groups, err
}
if len(options.RootPath) == 0 {
options.RootPath = filepath.Base("")
}
if !fileInfo.IsDir() && !excluded {
for _, format := range formats {
if groups.Match(format, path, options.LockFileOnly) {

// Traverse files to find dependency file groups
err = filepath.Walk(
break
}
}
}

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() && !Excluded(options.Exclusions, options.Inclusions, path) {
if !fileInfo.IsDir() {
for _, format := range formats {
if groups.Match(format, path, options.LockFileOnly) {
if excludedGroups.Match(format, path, options.LockFileOnly) {
excludedFiles = append(excludedFiles, path)

break
}
Expand All @@ -109,6 +128,46 @@ func (finder *Finder) GetGroups(options DebrickedOptions) (Groups, error) {
},
)

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)

return groups, err
Expand Down
81 changes: 81 additions & 0 deletions internal/file/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package file
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"sort"
Expand Down Expand Up @@ -185,6 +187,85 @@ 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

_, err := function(options)
if err != nil {
fmt.Printf("Error: %v\n", err)

return ""
}

write.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, err = io.Copy(&buf, read)
if err != nil {
fmt.Printf("Error: %v\n", err)

return ""
}

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
Loading