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 warning message if compressed files were skipped during scan. #247

Merged
merged 1 commit into from
Jul 11, 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
57 changes: 42 additions & 15 deletions internal/file/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ func (finder *Finder) GetConfigPath(rootPath string, exclusions []string, inclus
return configPath
}

func isCompressed(filename string) bool {
compressionExtensions := map[string]struct{}{
".gz": {},
".zip": {},
".tar": {},
".rar": {},
".bz2": {},
".xz": {},
".7z": {},
}
myExt := filepath.Ext(filename)
_, compressed := compressionExtensions[myExt]

return compressed
}

func (finder *Finder) GetIncludedGroups(formats []*CompiledFormat, options DebrickedOptions) (Groups, error) {
// NOTE: inefficient because it walks into excluded directories
var groups Groups
Expand Down Expand Up @@ -114,7 +130,9 @@ func (finder *Finder) GetExcludedGroups(formats []*CompiledFormat, options Debri

return err
}
if !fileInfo.IsDir() {
if isCompressed(path) {
excludedFiles = append(excludedFiles, path)
} else if !fileInfo.IsDir() {
for _, format := range formats {
if excludedGroups.Match(format, path, options.LockFileOnly) {
excludedFiles = append(excludedFiles, path)
Expand All @@ -131,10 +149,29 @@ func (finder *Finder) GetExcludedGroups(formats []*CompiledFormat, options Debri
return excludedGroups, excludedFiles, err
}

func reportExclusions(excludedFiles []string) {
if len(excludedFiles) > 0 {
containsCompressedFile := false
fmt.Println("The following files were excluded, resulting in no dependency files found.")
for _, file := range excludedFiles {
if !containsCompressedFile && isCompressed(file) {
containsCompressedFile = true
}
fmt.Println(file)
}
if containsCompressedFile {
fmt.Println("Compressed file found, but contained files cannot be scanned. Decompress to scan content.")
}
} else {
fmt.Println("No dependency file matches found with current configuration.")
}
fmt.Println("Change the inclusion and exclusion options if a file or directory was missed.")

}

// 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 {
Expand All @@ -147,21 +184,11 @@ func (finder *Finder) GetGroups(options DebrickedOptions) (Groups, error) {

// Traverse files to find dependency file groups
groups, err = finder.GetIncludedGroups(formats, options)
noGroupsFound = len(groups.groups) == 0
if noGroupsFound {
if len(groups.groups) == 0 {
// No dependencies found. (should rarely happen)
// Traverse again to see if dependency files were excluded.
// Traverse again to see if dependency or zip 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.")
}
reportExclusions(excludedFiles)
if excludedErr != nil {

return groups, err
Expand Down
25 changes: 13 additions & 12 deletions internal/file/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,26 +218,31 @@ func TestGetGroupsAllExcluded(t *testing.T) {
options := DebrickedOptions{
RootPath: "testdata/misc",
Exclusions: []string{"**/**"},
Inclusions: []string{},
Inclusions: []string{"**/**.zip"},
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)
}
expectedEnd := "Change the inclusion and exclusion options if a file or directory was missed."
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)
expectedCompressionWarning := "Compressed file found, but contained files cannot be scanned. Decompress to scan content."
if !strings.Contains(actualOutput, expectedCompressionWarning) {
t.Errorf("Expected %q but got %q", expectedCompressionWarning, actualOutput)
}
expectedExcludedDependencyFile := "requirements.txt"
if !strings.Contains(actualOutput, expectedExcludedDependencyFile) {
t.Errorf("Expected %q but got %q", expectedExcludedDependencyFile, actualOutput)
}
expectedSkippedCompressedFile := "zipped.zip"
if !strings.Contains(actualOutput, expectedSkippedCompressedFile) {
t.Errorf("Expected %q but got %q", expectedExcludedDependencyFile, actualOutput)
}
}

Expand All @@ -255,15 +260,11 @@ func TestGetGroupsAllExcludedByStrictness(t *testing.T) {

// 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) {
Expand Down
1 change: 0 additions & 1 deletion internal/file/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func (gs *Groups) FilterGroupsByStrictness(strictness int) {
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
Empty file.
Loading