repopath

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package repopath finds the repository root and reads files by their root-relative paths, so a test names a fixture such as "api/access.openapi.yaml" the same way from any package directory. It also holds the walk rule that every repository walk shares.

Role

platform module, test support layer (platform/testkit). It imports only the standard library, so a test in any package can use it without an import cycle.

Inputs

  • The working directory of the test, which go test sets to the package directory; the nearest go.mod at or above it marks the root.
  • Slash-separated paths relative to that root, named by tests.

Outputs

  • Absolute paths and file contents. A failed lookup fails the test through testing.TB instead of returning an error to every caller.
  • The sorted root-relative directories that hold Go files, for the layout tests.

Invariants

  • Every walk skips directories whose name starts with ".", testdata, vendor, node_modules and any directory below the root with its own go.mod. Untracked worktree copies under dot-directories such as .worktrees/ each hold a go.mod, so a walk without this rule would see every package many times over.

References

Status

Implemented P9d.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoModule = errors.New("no go.mod at or above the directory")

ErrNoModule reports that no directory at or above the starting directory holds a go.mod file.

Functions

func FindRoot

func FindRoot(dir string) (string, error)

FindRoot returns the nearest directory at or above dir, dir included, that holds a go.mod file. A relative dir is first made absolute against the working directory, so the search can climb above it. The error wraps ErrNoModule when the search reaches the file system root without a match.

func GoDirs

func GoDirs(root string) ([]string, error)

GoDirs returns the directories under root that hold .go files directly, as sorted, slash-separated paths relative to root ("." for root itself), walking with SkipDir. A directory of _test.go files alone counts, since it is still a package directory.

func Path

func Path(tb testing.TB, rel string) string

Path returns the absolute path of rel, a slash-separated path relative to the repository root such as "api/access.openapi.yaml". It fails the test when rel is empty, absolute or has "." or ".." elements, so a fixture path always names one place inside the repository.

func Read

func Read(tb testing.TB, rel string) []byte

Read returns the contents of the file at rel, a root-relative path as Path takes it. It fails the test when the file cannot be read.

func Root

func Root(tb testing.TB) string

Root returns the repository root: FindRoot of the working directory, which go test sets to the directory of the package under test. It fails the test when there is no root.

func SkipDir

func SkipDir(root, path string, d fs.DirEntry) bool

SkipDir is the walk rule the repository-wide walks in tests share. It reports whether a walk from root should skip the directory at path: one whose name starts with ".", testdata, vendor, node_modules, or any directory below root that holds its own go.mod. It never skips root itself or an entry that is not a directory. Use it from a filepath.WalkDir callback by returning filepath.SkipDir when it reports true.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL