diff --git a/internal/file/finder.go b/internal/file/finder.go index 09e73530..51788197 100644 --- a/internal/file/finder.go +++ b/internal/file/finder.go @@ -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 @@ -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) @@ -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 { @@ -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 diff --git a/internal/file/finder_test.go b/internal/file/finder_test.go index 465b00bf..8a32bba3 100644 --- a/internal/file/finder_test.go +++ b/internal/file/finder_test.go @@ -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) } } @@ -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) { diff --git a/internal/file/groups.go b/internal/file/groups.go index 0686e041..808544c7 100644 --- a/internal/file/groups.go +++ b/internal/file/groups.go @@ -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 diff --git a/internal/file/testdata/misc/zipped.zip b/internal/file/testdata/misc/zipped.zip new file mode 100644 index 00000000..e69de29b