ignore_test.go 4.4 KB

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