Skip to content
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
10 changes: 10 additions & 0 deletions pkg/watch/dockerignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"

"github.com/compose-spec/compose-go/v2/types"
Expand Down Expand Up @@ -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 p != "" && p[0] != '!' // Only keep exclusion patterns
})
}

pm, err := patternmatcher.New(absPatterns(absRoot, patterns))
if err != nil {
return nil, err
Expand Down
108 changes: 108 additions & 0 deletions pkg/watch/dockerignore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2020 Docker Compose CLI authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the license header to this new file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, pushed fix


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])
}
}
}
})
}
}