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

Function TestSpec_PublicAPI_ReadFileFromHEAD

pkg/gitutil/spec_test.go:368–399  ·  view source on GitHub ↗

TestSpec_PublicAPI_ReadFileFromHEAD validates the documented behavior of ReadFileFromHEAD as described in the package README.md. Specification: Reads a file's content from the HEAD commit without touching the working tree; rejects paths that escape the repository.

(t *testing.T)

Source from the content-addressed store, hash-verified

366// Specification: Reads a file's content from the HEAD commit without touching
367// the working tree; rejects paths that escape the repository.
368func TestSpec_PublicAPI_ReadFileFromHEAD(t *testing.T) {
369 root, err := gitutil.FindGitRoot()
370 if err != nil {
371 t.Skip("not inside a git repository, skipping ReadFileFromHEAD tests")
372 }
373
374 t.Run("reads known file from HEAD without error", func(t *testing.T) {
375 content, err := gitutil.ReadFileFromHEAD(filepath.Join(root, "go.mod"), root)
376 require.NoError(t, err, "ReadFileFromHEAD should read go.mod without error")
377 assert.NotEmpty(t, content, "content of go.mod should not be empty")
378 })
379
380 t.Run("returns error for non-existent file", func(t *testing.T) {
381 _, err := gitutil.ReadFileFromHEAD("this-file-does-not-exist-xyzzy.txt", root)
382 assert.Error(t, err, "ReadFileFromHEAD should return error for non-existent file")
383 })
384
385 t.Run("rejects path with .. traversal", func(t *testing.T) {
386 // Specification: "The function rejects paths that escape the repository
387 // (i.e. paths containing .. after resolution)."
388 outsidePath := filepath.Join(root, "..", "outside.txt")
389 _, err := gitutil.ReadFileFromHEAD(outsidePath, root)
390 require.Error(t, err, "ReadFileFromHEAD should reject path-traversal attempts")
391 assert.Contains(t, err.Error(), "outside the git repository root")
392 })
393
394 t.Run("returns error when gitRoot is empty", func(t *testing.T) {
395 // Specification: gitRoot must be the repository root (from FindGitRoot)
396 _, err := gitutil.ReadFileFromHEAD("go.mod", "")
397 assert.Error(t, err, "ReadFileFromHEAD should return error when gitRoot is empty")
398 })
399}

Callers

nothing calls this directly

Calls 4

FindGitRootFunction · 0.92
ReadFileFromHEADFunction · 0.92
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected