Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a18573c9f8 | |||
| eacc92ce4b |
@@ -82,7 +82,7 @@ func TestGlobExpansion(t *testing.T) {
|
||||
for _, pattern := range tc.patterns {
|
||||
patternMap[pattern] = struct{}{}
|
||||
}
|
||||
files, err := utils.ExpandGLobs(patternMap)
|
||||
files, err := utils.ExpandGlobs(patternMap)
|
||||
if err != nil {
|
||||
t.Fatalf("ExpandGLobs failed: %v", err)
|
||||
}
|
||||
|
||||
2
main.go
2
main.go
@@ -243,7 +243,7 @@ func runModifier(args []string, cmd *cobra.Command) {
|
||||
// Resolve all the files for all the globs
|
||||
mainLogger.Info("Found %d unique file patterns", len(globs))
|
||||
mainLogger.Debug("Expanding glob patterns to files")
|
||||
files, err := utils.ExpandGLobs(globs)
|
||||
files, err := utils.ExpandGlobs(globs)
|
||||
if err != nil {
|
||||
mainLogger.Error("Failed to expand file patterns: %v", err)
|
||||
return
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -62,6 +62,7 @@ func (c *ModifyCommand) Validate() error {
|
||||
|
||||
// Ehh.. Not much better... Guess this wasn't the big deal
|
||||
var matchesMemoTable map[string]bool = make(map[string]bool)
|
||||
var globMemoTable map[string][]string = make(map[string][]string)
|
||||
|
||||
func Matches(path string, glob string) (bool, error) {
|
||||
matchesLogger := modifyCommandLogger.WithPrefix("Matches").WithField("path", path).WithField("glob", glob)
|
||||
@@ -208,7 +209,7 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
||||
return globs
|
||||
}
|
||||
|
||||
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
||||
func ExpandGlobs(patterns map[string]struct{}) ([]string, error) {
|
||||
expandGlobsLogger := modifyCommandLogger.WithPrefix("ExpandGLobs")
|
||||
expandGlobsLogger.Debug("Expanding glob patterns to actual files")
|
||||
expandGlobsLogger.Trace("Input patterns for expansion: %v", patterns)
|
||||
@@ -225,10 +226,16 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
||||
for pattern := range patterns {
|
||||
expandGlobsLogger.Debug("Processing glob pattern: %q", pattern)
|
||||
static, pattern := SplitPattern(pattern)
|
||||
matches, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||
if err != nil {
|
||||
expandGlobsLogger.Warning("Error expanding glob %q in %q: %v", pattern, static, err)
|
||||
continue
|
||||
key := static + "|" + pattern
|
||||
matches, ok := globMemoTable[key]
|
||||
if !ok {
|
||||
var err error
|
||||
matches, err = doublestar.Glob(os.DirFS(static), pattern)
|
||||
if err != nil {
|
||||
expandGlobsLogger.Warning("Error expanding glob %q in %q: %v", pattern, static, err)
|
||||
continue
|
||||
}
|
||||
globMemoTable[key] = matches
|
||||
}
|
||||
expandGlobsLogger.Debug("Found %d matches for pattern %q", len(matches), pattern)
|
||||
expandGlobsLogger.Trace("Raw matches for pattern %q: %v", pattern, matches)
|
||||
|
||||
@@ -705,6 +705,58 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) {
|
||||
// }
|
||||
// }
|
||||
|
||||
func TestExpandGlobsMemoization(t *testing.T) {
|
||||
tmpDir, err := os.MkdirTemp("", "expand-globs-memo-test")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
err = os.WriteFile(filepath.Join(tmpDir, "test1.go"), []byte("test"), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
err = os.WriteFile(filepath.Join(tmpDir, "test2.go"), []byte("test"), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
origDir, _ := os.Getwd()
|
||||
os.Chdir(tmpDir)
|
||||
defer os.Chdir(origDir)
|
||||
|
||||
cwd, _ := os.Getwd()
|
||||
resolvedCwd := ResolvePath(cwd)
|
||||
pattern1 := resolvedCwd + "/*.go"
|
||||
patterns := map[string]struct{}{pattern1: {}}
|
||||
|
||||
globMemoTable = make(map[string][]string)
|
||||
|
||||
files1, err := ExpandGlobs(patterns)
|
||||
if err != nil {
|
||||
t.Fatalf("ExpandGlobs failed: %v", err)
|
||||
}
|
||||
if len(files1) != 2 {
|
||||
t.Fatalf("Expected 2 files, got %d", len(files1))
|
||||
}
|
||||
|
||||
if len(globMemoTable) != 1 {
|
||||
t.Fatalf("Expected 1 entry in memo table, got %d", len(globMemoTable))
|
||||
}
|
||||
|
||||
files2, err := ExpandGlobs(patterns)
|
||||
if err != nil {
|
||||
t.Fatalf("ExpandGlobs failed: %v", err)
|
||||
}
|
||||
if len(files2) != 2 {
|
||||
t.Fatalf("Expected 2 files, got %d", len(files2))
|
||||
}
|
||||
|
||||
if len(globMemoTable) != 1 {
|
||||
t.Fatalf("Expected memo table to still have 1 entry, got %d", len(globMemoTable))
|
||||
}
|
||||
}
|
||||
|
||||
// LoadCommandsFromCookFile returns an error for a malformed YAML file
|
||||
// func TestLoadCommandsFromCookFilesMalformedYAML(t *testing.T) {
|
||||
// // Setup test directory with mock YAML files
|
||||
|
||||
Reference in New Issue
Block a user