@@ -2,6 +2,7 @@ package fsext
22
33import (
44 "os"
5+ "path/filepath"
56 "testing"
67
78 "github.com/stretchr/testify/require"
@@ -30,3 +31,98 @@ func TestCrushIgnore(t *testing.T) {
3031 require.False(t, dl.shouldIgnore("test1.txt", nil), ".txt files should not be ignored")
3132 require.True(t, dl.shouldIgnore("test3.tmp", nil), ".tmp files should be ignored by common patterns")
3233}
34+
35+func TestShouldExcludeFile(t *testing.T) {
36+ t.Parallel()
37+
38+ // Create a temporary directory structure for testing
39+ tempDir := t.TempDir()
40+
41+ // Create directories that should be ignored
42+ nodeModules := filepath.Join(tempDir, "node_modules")
43+ target := filepath.Join(tempDir, "target")
44+ customIgnored := filepath.Join(tempDir, "custom_ignored")
45+ normalDir := filepath.Join(tempDir, "src")
46+
47+ for _, dir := range []string{nodeModules, target, customIgnored, normalDir} {
48+ if err := os.MkdirAll(dir, 0o755); err != nil {
49+ t.Fatalf("Failed to create directory %s: %v", dir, err)
50+ }
51+ }
52+
53+ // Create .gitignore file
54+ gitignoreContent := "node_modules/\ntarget/\n"
55+ if err := os.WriteFile(filepath.Join(tempDir, ".gitignore"), []byte(gitignoreContent), 0o644); err != nil {
56+ t.Fatalf("Failed to create .gitignore: %v", err)
57+ }
58+
59+ // Create .crushignore file
60+ crushignoreContent := "custom_ignored/\n"
61+ if err := os.WriteFile(filepath.Join(tempDir, ".crushignore"), []byte(crushignoreContent), 0o644); err != nil {
62+ t.Fatalf("Failed to create .crushignore: %v", err)
63+ }
64+
65+ // Test that ignored directories are properly ignored
66+ require.True(t, ShouldExcludeFile(tempDir, nodeModules), "Expected node_modules to be ignored by .gitignore")
67+ require.True(t, ShouldExcludeFile(tempDir, target), "Expected target to be ignored by .gitignore")
68+ require.True(t, ShouldExcludeFile(tempDir, customIgnored), "Expected custom_ignored to be ignored by .crushignore")
69+
70+ // Test that normal directories are not ignored
71+ require.False(t, ShouldExcludeFile(tempDir, normalDir), "Expected src directory to not be ignored")
72+
73+ // Test that the workspace root itself is not ignored
74+ require.False(t, ShouldExcludeFile(tempDir, tempDir), "Expected workspace root to not be ignored")
75+}
76+
77+func TestShouldExcludeFileHierarchical(t *testing.T) {
78+ t.Parallel()
79+
80+ // Create a nested directory structure for testing hierarchical ignore
81+ tempDir := t.TempDir()
82+
83+ // Create nested directories
84+ subDir := filepath.Join(tempDir, "subdir")
85+ nestedNormal := filepath.Join(subDir, "normal_nested")
86+
87+ for _, dir := range []string{subDir, nestedNormal} {
88+ if err := os.MkdirAll(dir, 0o755); err != nil {
89+ t.Fatalf("Failed to create directory %s: %v", dir, err)
90+ }
91+ }
92+
93+ // Create .crushignore in subdir that ignores normal_nested
94+ subCrushignore := "normal_nested/\n"
95+ if err := os.WriteFile(filepath.Join(subDir, ".crushignore"), []byte(subCrushignore), 0o644); err != nil {
96+ t.Fatalf("Failed to create subdir .crushignore: %v", err)
97+ }
98+
99+ // Test hierarchical ignore behavior - this should work because the .crushignore is in the parent directory
100+ require.True(t, ShouldExcludeFile(tempDir, nestedNormal), "Expected normal_nested to be ignored by subdir .crushignore")
101+ require.False(t, ShouldExcludeFile(tempDir, subDir), "Expected subdir itself to not be ignored")
102+}
103+
104+func TestShouldExcludeFileCommonPatterns(t *testing.T) {
105+ t.Parallel()
106+
107+ tempDir := t.TempDir()
108+
109+ // Create directories that should be ignored by common patterns
110+ commonIgnored := []string{
111+ filepath.Join(tempDir, ".git"),
112+ filepath.Join(tempDir, "node_modules"),
113+ filepath.Join(tempDir, "__pycache__"),
114+ filepath.Join(tempDir, "target"),
115+ filepath.Join(tempDir, ".vscode"),
116+ }
117+
118+ for _, dir := range commonIgnored {
119+ if err := os.MkdirAll(dir, 0o755); err != nil {
120+ t.Fatalf("Failed to create directory %s: %v", dir, err)
121+ }
122+ }
123+
124+ // Test that common patterns are ignored even without explicit ignore files
125+ for _, dir := range commonIgnored {
126+ require.True(t, ShouldExcludeFile(tempDir, dir), "Expected %s to be ignored by common patterns", filepath.Base(dir))
127+ }
128+}