(t *testing.T)
| 295 | } |
| 296 | |
| 297 | func TestFindGitRootFrom(t *testing.T) { |
| 298 | t.Run("returns git root from the repository root itself", func(t *testing.T) { |
| 299 | gitRoot, err := FindGitRoot() |
| 300 | require.NoError(t, err, "must be inside a git repository") |
| 301 | |
| 302 | root, err := FindGitRootFrom(gitRoot) |
| 303 | require.NoError(t, err, "FindGitRootFrom should succeed when starting from the git root") |
| 304 | assert.Equal(t, gitRoot, root, "FindGitRootFrom from git root should return git root") |
| 305 | }) |
| 306 | |
| 307 | t.Run("returns git root from a subdirectory", func(t *testing.T) { |
| 308 | gitRoot, err := FindGitRoot() |
| 309 | require.NoError(t, err, "must be inside a git repository") |
| 310 | |
| 311 | // Create a temporary subdirectory inside the repo to avoid depending on |
| 312 | // specific repo layout (e.g. pkg/ may not exist in all test environments). |
| 313 | subDir, mkdirErr := os.MkdirTemp(gitRoot, "test-subdir-*") |
| 314 | require.NoError(t, mkdirErr, "should create temp subdir inside git repo") |
| 315 | defer os.RemoveAll(subDir) |
| 316 | |
| 317 | root, err := FindGitRootFrom(subDir) |
| 318 | require.NoError(t, err, "FindGitRootFrom should succeed from a subdirectory") |
| 319 | assert.Equal(t, gitRoot, root, "FindGitRootFrom from subdirectory should return the git root") |
| 320 | }) |
| 321 | |
| 322 | t.Run("returns error when starting outside any git repository", func(t *testing.T) { |
| 323 | tmpDir := t.TempDir() |
| 324 | // Create a nested directory that is definitely not a git repo |
| 325 | nonRepoDir := filepath.Join(tmpDir, "not-a-git-repo", "subdir") |
| 326 | require.NoError(t, os.MkdirAll(nonRepoDir, 0755), "should create nested temp dir") |
| 327 | |
| 328 | _, err := FindGitRootFrom(nonRepoDir) |
| 329 | require.Error(t, err, "FindGitRootFrom should return error outside a git repository") |
| 330 | assert.Contains(t, err.Error(), "not in a git repository", "error should mention not in git repository") |
| 331 | }) |
| 332 | |
| 333 | t.Run("returns git root when .git is a worktree marker file", func(t *testing.T) { |
| 334 | // Simulate a git worktree: the repo root has a .git *file* (not dir) |
| 335 | // whose content begins with "gitdir: /some/path" |
| 336 | tmpDir := t.TempDir() |
| 337 | repoRoot := filepath.Join(tmpDir, "worktree-repo") |
| 338 | require.NoError(t, os.MkdirAll(repoRoot, 0755)) |
| 339 | |
| 340 | // Write a valid worktree .git file |
| 341 | gitFile := filepath.Join(repoRoot, ".git") |
| 342 | require.NoError(t, os.WriteFile(gitFile, []byte("gitdir: /tmp/real-repo/.git/worktrees/myworktree\n"), 0644)) |
| 343 | |
| 344 | // Start from the root itself |
| 345 | root, err := FindGitRootFrom(repoRoot) |
| 346 | require.NoError(t, err, "FindGitRootFrom should detect a worktree .git file") |
| 347 | assert.Equal(t, repoRoot, root) |
| 348 | |
| 349 | // Start from a subdirectory inside the worktree |
| 350 | subDir := filepath.Join(repoRoot, "pkg", "sub") |
| 351 | require.NoError(t, os.MkdirAll(subDir, 0755)) |
| 352 | root, err = FindGitRootFrom(subDir) |
| 353 | require.NoError(t, err, "FindGitRootFrom should detect worktree root from a subdirectory") |
| 354 | assert.Equal(t, repoRoot, root) |
nothing calls this directly
no test coverage detected