Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetEnv ¶
GetEnv returns the value of the environment variable named by the key. It returns an error if the variable is not set or empty.
Example ¶
// GetEnv returns the value of an environment variable, or an error
// if the variable is not set or empty.
addr, err := GetEnv("TEST_ES_ADDRESS")
if err != nil {
// handle missing env var (e.g. skip the test)
return
}
_ = addr
// Use addr to connect to the service under test.
func LoadEnv ¶
func LoadEnv() error
LoadEnv loads environment variables from .env.test at the project root. It is a no-op if .env.test does not exist; externally set environment variables still apply (CI-friendly). It returns an error only when the file exists but fails to parse, or when the project root cannot be found.
Example ¶
// LoadEnv loads environment variables from .env.test at the project
// root. It is a no-op if the file does not exist, so tests can rely
// on externally set env vars in CI.
if err := LoadEnv(); err != nil {
// handle error (e.g. skip the test)
return
}
// Env vars from .env.test are now available via os.Getenv or GetEnv.
func LoadEnvFromFile ¶
LoadEnvFromFile loads environment variables from the file at the given path, interpreted as relative to the project root. It is the underlying implementation of LoadEnv. It is a no-op if the file does not exist; externally set environment variables still apply (CI-friendly). It returns an error only when the file exists but fails to parse, or when the project root cannot be found.
func MustGetEnv ¶
MustGetEnv is like GetEnv but panics if the variable is not set or empty. This is convenient for package-level or TestMain setup where the env var is guaranteed to exist and a missing value indicates a configuration bug.
func ProjectRoot ¶
func ProjectRoot() string
ProjectRoot walks up from the current working directory to find the directory containing go.mod. It returns the absolute path, or "" if not found.
Example ¶
// ProjectRoot walks up from the working directory to find the directory // containing go.mod. It is useful for locating test fixtures relative // to the project root. root := ProjectRoot() _ = root // Use root to build paths to test fixtures, e.g.: // fixturePath := filepath.Join(root, "testdata", "fixture.json")
Types ¶
This section is empty.