fix(dockerignore): Add wildcard support to dockerignore.go

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
This commit is contained in:
Suleiman Dibirov 2024-10-27 08:24:17 +02:00 коммит произвёл Guillaume Lours
Родитель eeea049f17
Коммит 2ac081b4c4
2 изменённых файлов: 102 добавлений и 0 удалений

Просмотреть файл

@ -21,6 +21,7 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"
"github.com/compose-spec/compose-go/v2/types"
@ -128,6 +129,15 @@ func NewDockerPatternMatcher(repoRoot string, patterns []string) (*dockerPathMat
return nil, err
}
// Check if "*" is present in patterns
hasAllPattern := slices.Contains(patterns, "*")
if hasAllPattern {
// Remove all non-exclusion patterns (those that don't start with '!')
patterns = slices.DeleteFunc(patterns, func(p string) bool {
return len(p) > 0 && p[0] != '!' // Only keep exclusion patterns
})
}
pm, err := patternmatcher.New(absPatterns(absRoot, patterns))
if err != nil {
return nil, err

Просмотреть файл

@ -0,0 +1,92 @@
package watch
import (
"testing"
)
func TestNewDockerPatternMatcher(t *testing.T) {
tests := []struct {
name string
repoRoot string
patterns []string
expectedErr bool
expectedRoot string
expectedPat []string
}{
{
name: "Basic patterns without wildcard",
repoRoot: "/repo",
patterns: []string{"dir1/", "file.txt"},
expectedErr: false,
expectedRoot: "/repo",
expectedPat: []string{"/repo/dir1", "/repo/file.txt"},
},
{
name: "Patterns with exclusion",
repoRoot: "/repo",
patterns: []string{"dir1/", "!file.txt"},
expectedErr: false,
expectedRoot: "/repo",
expectedPat: []string{"/repo/dir1", "!/repo/file.txt"},
},
{
name: "Wildcard with exclusion",
repoRoot: "/repo",
patterns: []string{"*", "!file.txt"},
expectedErr: false,
expectedRoot: "/repo",
expectedPat: []string{"!/repo/file.txt"},
},
{
name: "No patterns",
repoRoot: "/repo",
patterns: []string{},
expectedErr: false,
expectedRoot: "/repo",
expectedPat: nil,
},
{
name: "Only exclusion pattern",
repoRoot: "/repo",
patterns: []string{"!file.txt"},
expectedErr: false,
expectedRoot: "/repo",
expectedPat: []string{"!/repo/file.txt"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the function with the test data
matcher, err := NewDockerPatternMatcher(tt.repoRoot, tt.patterns)
// Check if we expect an error
if (err != nil) != tt.expectedErr {
t.Fatalf("expected error: %v, got: %v", tt.expectedErr, err)
}
// If no error is expected, check the output
if !tt.expectedErr {
if matcher.repoRoot != tt.expectedRoot {
t.Errorf("expected root: %v, got: %v", tt.expectedRoot, matcher.repoRoot)
}
// Compare patterns
actualPatterns := matcher.matcher.Patterns()
if len(actualPatterns) != len(tt.expectedPat) {
t.Errorf("expected patterns length: %v, got: %v", len(tt.expectedPat), len(actualPatterns))
}
for i, expectedPat := range tt.expectedPat {
actualPatternStr := actualPatterns[i].String()
if actualPatterns[i].Exclusion() {
actualPatternStr = "!" + actualPatternStr
}
if actualPatternStr != expectedPat {
t.Errorf("expected pattern: %v, got: %v", expectedPat, actualPatterns[i])
}
}
}
})
}
}