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

fix(file_filter): do not try and pass invalid ignore files #159

Merged
merged 2 commits into from
May 13, 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
3 changes: 2 additions & 1 deletion pkg/utils/file_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func (fw *FileFilter) GetRules(ruleFiles []string) ([]string, error) {
// iterate filesToFilter channel and find ignore filesToFilter
var ignoreFiles = make([]string, 0)
for file := range files {
fileName := filepath.Base(file)
for _, ruleFile := range ruleFiles {
if strings.Contains(file, ruleFile) {
if fileName == ruleFile {
j-luong marked this conversation as resolved.
Show resolved Hide resolved
ignoreFiles = append(ignoreFiles, file)
}
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/utils/file_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,33 @@ func TestFileFilter_GetRules(t *testing.T) {

assert.ElementsMatch(t, expectedRules, actualRules)
})

t.Run("only gets ignore rules from valid files", func(t *testing.T) {
// adds ignore file to filesystem
ignoreFile := filepath.Join(tempDir, ".gitignore")
createFileInPath(t, ignoreFile, []byte("test1.ts\n"))

// adds a similarly named ignore file to filesystem
almostAnIgnoreFile := filepath.Join(tempDir, "almost.gitignore.go")
createFileInPath(t, almostAnIgnoreFile, []byte("package main\n import \"fmt\"\n func main() { fmt.Println(\"hello world\") }"))

// create fileFilter
ruleFiles := []string{".gitignore"}
fileFilter := NewFileFilter(tempDir, &log.Logger)
actualRules, err := fileFilter.GetRules(ruleFiles)
assert.NoError(t, err)

// create expected rules
expectedRules := append(
[]string{
fmt.Sprintf("%s/**/test1.ts/**", tempDir), // apply ignore in subDirs
fmt.Sprintf("%s/**/test1.ts", tempDir), // apply ignore in curDir
},
fileFilter.defaultRules...,
)

assert.ElementsMatch(t, expectedRules, actualRules)
})
}

func TestFileFilter_GetFilteredFiles(t *testing.T) {
Expand Down