Test the commitWorktreeChanges function
(t *testing.T)
| 130 | |
| 131 | // Test the commitWorktreeChanges function |
| 132 | func TestCommitWorktreeChanges(t *testing.T) { |
| 133 | ctx := context.Background() |
| 134 | dir := t.TempDir() |
| 135 | |
| 136 | // Initialize git repo |
| 137 | _, err := RunGitCommand(ctx, dir, "init") |
| 138 | require.NoError(t, err) |
| 139 | |
| 140 | // Set git config |
| 141 | _, err = RunGitCommand(ctx, dir, "config", "user.email", "test@example.com") |
| 142 | require.NoError(t, err) |
| 143 | _, err = RunGitCommand(ctx, dir, "config", "user.name", "Test User") |
| 144 | require.NoError(t, err) |
| 145 | |
| 146 | repo := &Repository{ |
| 147 | lockManager: NewRepositoryLockManager(dir), |
| 148 | } |
| 149 | |
| 150 | t.Run("empty_directory_handling", func(t *testing.T) { |
| 151 | // Create empty directories (git doesn't track these) |
| 152 | createDir(t, dir, "empty1") |
| 153 | createDir(t, dir, "empty2/nested") |
| 154 | |
| 155 | // This verifies that commitWorktreeChanges handles empty directories gracefully |
| 156 | // It should return nil (success) when there's nothing to commit |
| 157 | err := repo.commitWorktreeChanges(ctx, dir, "Empty dirs") |
| 158 | assert.NoError(t, err, "commitWorktreeChanges should handle empty dirs gracefully") |
| 159 | }) |
| 160 | |
| 161 | t.Run("commits_changes", func(t *testing.T) { |
| 162 | // Create a file to commit |
| 163 | writeFile(t, dir, "test.txt", "hello world") |
| 164 | |
| 165 | err := repo.commitWorktreeChanges(ctx, dir, "Testing commit functionality") |
| 166 | require.NoError(t, err) |
| 167 | |
| 168 | // Verify commit was created |
| 169 | log, err := RunGitCommand(ctx, dir, "log", "--oneline") |
| 170 | require.NoError(t, err) |
| 171 | assert.Contains(t, log, "Testing commit functionality") |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | // Test helper functions |
| 176 | func writeFile(t *testing.T, dir, name, content string) { |
nothing calls this directly
no test coverage detected