TestSpec_PublicAPI_FindGitRootFrom validates the documented behavior of FindGitRootFrom as described in the package README.md. Specification: Like FindGitRoot but starts from startDir; traverses upward looking for a .git directory or worktree marker file (a `.git` file starting with `gitdir:`).
(t *testing.T)
| 298 | // looking for a .git directory or worktree marker file (a `.git` file starting |
| 299 | // with `gitdir:`). |
| 300 | func TestSpec_PublicAPI_FindGitRootFrom(t *testing.T) { |
| 301 | t.Run("returns absolute repository root when startDir is inside a repo", func(t *testing.T) { |
| 302 | // The current working directory of this test is inside the gh-aw |
| 303 | // repository, so any subdirectory inside it should resolve to the |
| 304 | // repository root. |
| 305 | repoRoot, err := gitutil.FindGitRoot() |
| 306 | require.NoError(t, err, "FindGitRoot should succeed inside the gh-aw repository") |
| 307 | |
| 308 | root, err := gitutil.FindGitRootFrom(repoRoot) |
| 309 | require.NoError(t, err, "FindGitRootFrom should succeed when startDir is inside a repository") |
| 310 | assert.NotEmpty(t, root, "FindGitRootFrom should return a non-empty path") |
| 311 | assert.True(t, filepath.IsAbs(root), |
| 312 | "FindGitRootFrom should return an absolute path, got %q", root) |
| 313 | }) |
| 314 | |
| 315 | t.Run("traverses upward from a subdirectory to locate the repository root", func(t *testing.T) { |
| 316 | repoRoot, err := gitutil.FindGitRoot() |
| 317 | require.NoError(t, err, "FindGitRoot should succeed inside the gh-aw repository") |
| 318 | |
| 319 | // Start from a subdirectory of the repo; FindGitRootFrom should walk |
| 320 | // upward and land on the same root. |
| 321 | fromSub, err := gitutil.FindGitRootFrom(filepath.Join(repoRoot, "pkg", "gitutil")) |
| 322 | require.NoError(t, err, "FindGitRootFrom should succeed from a subdirectory") |
| 323 | assert.Equal(t, repoRoot, fromSub, |
| 324 | "FindGitRootFrom from a subdirectory should return the same root as FindGitRoot") |
| 325 | }) |
| 326 | |
| 327 | t.Run("returns error when startDir is not inside a git repository", func(t *testing.T) { |
| 328 | // A directory created outside any git repository should produce an error. |
| 329 | isolated := t.TempDir() |
| 330 | _, err := gitutil.FindGitRootFrom(isolated) |
| 331 | assert.Error(t, err, |
| 332 | "FindGitRootFrom should return an error when startDir is not inside a git repository") |
| 333 | }) |
| 334 | } |
| 335 | |
| 336 | // TestSpec_Variables_ErrNotGitRepository validates the documented sentinel-error |
| 337 | // contract for ErrNotGitRepository. |
nothing calls this directly
no test coverage detected