MCPcopy Create free account
hub / github.com/github/gh-aw / TestSpec_PublicAPI_FindGitRootFrom

Function TestSpec_PublicAPI_FindGitRootFrom

pkg/gitutil/spec_test.go:300–334  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

298// looking for a .git directory or worktree marker file (a `.git` file starting
299// with `gitdir:`).
300func 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.

Callers

nothing calls this directly

Calls 4

FindGitRootFunction · 0.92
FindGitRootFromFunction · 0.92
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected