|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGitIgnore(t *testing.T) { |
| 13 | + t.Run("without gitignore files", func(t *testing.T) { |
| 14 | + gitIgnore := NewGitIgnore(t.TempDir()) |
| 15 | + assertFileNotSkipped(t, &gitIgnore, "some/random/file.txt") |
| 16 | + }) |
| 17 | + |
| 18 | + t.Run("with local gitignore", func(t *testing.T) { |
| 19 | + tmpRepoDir := t.TempDir() |
| 20 | + writeFile(t, filepath.Join(tmpRepoDir, ".gitignore"), "ignore/this/file.txt") |
| 21 | + |
| 22 | + gitIgnore := NewGitIgnore(tmpRepoDir) |
| 23 | + assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt") |
| 24 | + assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt") |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("with global gitignore", func(t *testing.T) { |
| 28 | + backup := setupGlobalGitIgnore(t, "ignore/this/file.txt") |
| 29 | + defer func() { |
| 30 | + if backup.originalPath == "" { |
| 31 | + unsetGlobalGitIgnoreConfig(t) |
| 32 | + } |
| 33 | + backup.Restore(t) |
| 34 | + }() |
| 35 | + |
| 36 | + gitIgnore := NewGitIgnore(t.TempDir()) |
| 37 | + assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt") |
| 38 | + assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt") |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("with git info exclude", func(t *testing.T) { |
| 42 | + tmpRepoDir := createEmptyRepository(t) |
| 43 | + backup := setupGitInfoExclude(t, tmpRepoDir, "ignore/this/file.txt") |
| 44 | + defer backup.Restore(t) |
| 45 | + |
| 46 | + gitIgnore := NewGitIgnore(tmpRepoDir) |
| 47 | + assertFileSkipped(t, &gitIgnore, "ignore/this/file.txt") |
| 48 | + assertFileNotSkipped(t, &gitIgnore, "some/other/file.txt") |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func assertFileSkipped(t *testing.T, gitIgnore *GitIgnore, path string) { |
| 53 | + skip, err := gitIgnore.SkipFile(path) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.True(t, skip) |
| 56 | +} |
| 57 | + |
| 58 | +func assertFileNotSkipped(t *testing.T, gitIgnore *GitIgnore, path string) { |
| 59 | + skip, err := gitIgnore.SkipFile(path) |
| 60 | + assert.NoError(t, err) |
| 61 | + assert.False(t, skip) |
| 62 | +} |
| 63 | + |
| 64 | +type FileBackup struct { |
| 65 | + originalPath string |
| 66 | + backupPath string |
| 67 | +} |
| 68 | + |
| 69 | +func (b *FileBackup) Restore(t *testing.T) { |
| 70 | + if b.originalPath != "" { |
| 71 | + moveFile(t, b.backupPath, b.originalPath) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func setupGlobalGitIgnore(t *testing.T, content string) *FileBackup { |
| 76 | + globalGitIgnorePath := getGlobalGitIgnorePath() |
| 77 | + backupPath := filepath.Join(t.TempDir(), ".gitignore_global") |
| 78 | + |
| 79 | + if globalGitIgnorePath == "" { |
| 80 | + writeFile(t, backupPath, content) |
| 81 | + setGlobalGitIgnoreConfig(t, backupPath) |
| 82 | + return &FileBackup{originalPath: "", backupPath: backupPath} |
| 83 | + } |
| 84 | + |
| 85 | + moveFile(t, globalGitIgnorePath, backupPath) |
| 86 | + writeFile(t, globalGitIgnorePath, content) |
| 87 | + return &FileBackup{originalPath: globalGitIgnorePath, backupPath: backupPath} |
| 88 | +} |
| 89 | + |
| 90 | +func setupGitInfoExclude(t *testing.T, baseDir string, content string) *FileBackup { |
| 91 | + gitInfoExcludePath := filepath.Join(baseDir, ".git", "info", "exclude") |
| 92 | + _, err := os.Stat(gitInfoExcludePath) |
| 93 | + assert.NoError(t, err) |
| 94 | + |
| 95 | + backupPath := filepath.Join(t.TempDir(), ".git_info_exclude_backup") |
| 96 | + moveFile(t, gitInfoExcludePath, backupPath) |
| 97 | + writeFile(t, gitInfoExcludePath, content) |
| 98 | + return &FileBackup{originalPath: gitInfoExcludePath, backupPath: backupPath} |
| 99 | +} |
| 100 | + |
| 101 | +func setGlobalGitIgnoreConfig(t *testing.T, path string) { |
| 102 | + _, err := exec.Command("git", "config", "--global", "core.excludesfile", path).Output() |
| 103 | + assert.NoError(t, err) |
| 104 | +} |
| 105 | + |
| 106 | +func unsetGlobalGitIgnoreConfig(t *testing.T) { |
| 107 | + _, err := exec.Command("git", "config", "--global", "--unset", "core.excludesfile").Output() |
| 108 | + assert.NoError(t, err) |
| 109 | +} |
| 110 | + |
| 111 | +func moveFile(t *testing.T, srcPath string, dstPath string) { |
| 112 | + err := os.Rename(srcPath, dstPath) |
| 113 | + assert.NoError(t, err) |
| 114 | +} |
| 115 | + |
| 116 | +func writeFile(t *testing.T, path string, content string) { |
| 117 | + err := os.WriteFile(path, []byte(content), 0644) |
| 118 | + assert.NoError(t, err) |
| 119 | +} |
0 commit comments