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