Skip to content

Commit

Permalink
Add find directories with files regex (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
tateexon authored Apr 15, 2024
1 parent 1007b45 commit 04efb9f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
22 changes: 22 additions & 0 deletions utils/osutil/osutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -176,3 +177,24 @@ func findFileInSubfolders(startDir, targetFileName string) (string, error) {

return filePath, nil
}

// FindDirectoriesContainingFile finds all directories containing a file matching the given regular expression
func FindDirectoriesContainingFile(dir string, r *regexp.Regexp) ([]string, error) {
foundDirs := []string{}
// Walk through all files in the directory and its sub-directories
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if r.MatchString(info.Name()) {
foundDirs = append(foundDirs, filepath.Dir(path))
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "Error walking the directory: %v\n", err)
return nil, err
}
return foundDirs, nil
}
80 changes: 80 additions & 0 deletions utils/osutil/osutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -172,3 +173,82 @@ func TestFindFile_stopFileNotFoundWithinLimit(t *testing.T) {
require.Error(t, err, "expected error calling FindFile")
require.Contains(t, err.Error(), ErrStopFileNotFoundWithinLimit, "got wrong error")
}

func TestFindDirectoriesContainingFile(t *testing.T) {
t.Run("file in root directory", func(t *testing.T) {
// Create a temporary directory using the os package
tempDir, err := os.MkdirTemp("", "rootcheck")
if err != nil {
require.NoError(t, err, "error creating temp directory")
}
defer os.RemoveAll(tempDir) // Clean up after yourself

exampleFile := "examplefile.txt"
_, err = os.CreateTemp(tempDir, exampleFile)
if err != nil {
require.NoError(t, err, "error creating temp file")
}

testRegex := regexp.MustCompile(".*examplefile.txt")
dirs, err := FindDirectoriesContainingFile(tempDir, testRegex)
require.NoError(t, err, "error calling FindDirectoriesContainingFile")
require.Equal(t, []string{tempDir}, dirs)
})

t.Run("file in root and sub directory", func(t *testing.T) {
// Create a temporary directory using the os package
tempDir, err := os.MkdirTemp("", "rootcheck")
if err != nil {
require.NoError(t, err, "error creating temp directory")
}
defer os.RemoveAll(tempDir) // Clean up after yourself

exampleFile := "examplefile.txt"
_, err = os.CreateTemp(tempDir, exampleFile)
if err != nil {
require.NoError(t, err, "error creating temp file")
}

subDir, err := os.MkdirTemp(tempDir, "subdir")
if err != nil {
require.NoError(t, err, "error creating temp directory")
}

exampleFile2 := "examplefile.txt"
_, err = os.CreateTemp(subDir, exampleFile2)
if err != nil {
require.NoError(t, err, "error creating temp file")
}

testRegex := regexp.MustCompile(".*examplefile.txt")
dirs, err := FindDirectoriesContainingFile(tempDir, testRegex)
require.NoError(t, err, "error calling FindDirectoriesContainingFile")
require.Equal(t, []string{tempDir, subDir}, dirs)
})

t.Run("file in sub directory", func(t *testing.T) {
// Create a temporary directory using the os package
tempDir, err := os.MkdirTemp("", "rootcheck")
if err != nil {
require.NoError(t, err, "error creating temp directory")
}
defer os.RemoveAll(tempDir) // Clean up after yourself

subDir, err := os.MkdirTemp(tempDir, "subdir")
if err != nil {
require.NoError(t, err, "error creating temp directory")
}

exampleFile2 := "examplefile.txt"
_, err = os.CreateTemp(subDir, exampleFile2)
if err != nil {
require.NoError(t, err, "error creating temp file")
}

testRegex := regexp.MustCompile(".*examplefile.txt")
dirs, err := FindDirectoriesContainingFile(tempDir, testRegex)
require.NoError(t, err, "error calling FindDirectoriesContainingFile")
require.Equal(t, []string{subDir}, dirs)
})

}

0 comments on commit 04efb9f

Please sign in to comment.