@@ -2,6 +2,7 @@ package fsext
7 "github.com/stretchr/testify/require"
8 "github.com/stretchr/testify/require"
@@ -30,3 +31,98 @@ func TestCrushIgnore(t *testing.T) {
30 require.False(t, dl.shouldIgnore("test1.txt", nil), ".txt files should not be ignored")
31 require.False(t, dl.shouldIgnore("test1.txt", nil), ".txt files should not be ignored")
31 require.True(t, dl.shouldIgnore("test3.tmp", nil), ".tmp files should be ignored by common patterns")
32 require.True(t, dl.shouldIgnore("test3.tmp", nil), ".tmp files should be ignored by common patterns")
35+func TestShouldExcludeFile(t *testing.T) {
38+ // Create a temporary directory structure for testing
39+ tempDir := t.TempDir()
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")
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)
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)
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)
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")
70+ // Test that normal directories are not ignored
71+ require.False(t, ShouldExcludeFile(tempDir, normalDir), "Expected src directory to not be ignored")
73+ // Test that the workspace root itself is not ignored
74+ require.False(t, ShouldExcludeFile(tempDir, tempDir), "Expected workspace root to not be ignored")
77+func TestShouldExcludeFileHierarchical(t *testing.T) {
80+ // Create a nested directory structure for testing hierarchical ignore
81+ tempDir := t.TempDir()
83+ // Create nested directories
84+ subDir := filepath.Join(tempDir, "subdir")
85+ nestedNormal := filepath.Join(subDir, "normal_nested")
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)
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)
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")
104+func TestShouldExcludeFileCommonPatterns(t *testing.T) {
107+ tempDir := t.TempDir()
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"),
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)
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))