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)
| 366 | // Specification: Reads a file's content from the HEAD commit without touching |
| 367 | // the working tree; rejects paths that escape the repository. |
| 368 | func 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 | } |
nothing calls this directly
no test coverage detected