test

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MPL-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package test provides testing routines reused throughout terramate code base.

Index

Constants

View Source
const (
	// Username for the test commits.
	Username = "terramate tests"

	// Email for the test commits.
	Email = "terramate@mineiros.io"

	// DefBranch is the default repository branch.
	DefBranch = "main"
)

Variables

This section is empty.

Functions

func AppendFile

func AppendFile(t testing.TB, dir string, filename string, content string)

AppendFile appends content to a filename inside dir directory. If file exists, appends on the end of it by adding a newline, if file doesn't exists it will be created.

func AssertChmod added in v0.4.3

func AssertChmod(t testing.TB, fname string, mode fs.FileMode)

AssertChmod is a portable version of the os.AssertChmod.

func AssertConfigEquals

func AssertConfigEquals(t *testing.T, got, want []config.Assert)

AssertConfigEquals asserts that two config.Assert are equal.

func AssertDiff

func AssertDiff(t *testing.T, got, want interface{}, msg ...interface{})

AssertDiff will compare the two values and fail if they are not the same providing a comprehensive textual diff of the differences between them. If provided msg must be a string + any formatting parameters. The msg will be added if the assertion fails.

func AssertEqualPaths

func AssertEqualPaths(t *testing.T, got, want project.Path, fmtargs ...any)

AssertEqualPaths checks if two paths are equal.

func AssertEqualPos

func AssertEqualPos(t *testing.T, got, want info.Pos, fmtargs ...any)

AssertEqualPos checks if two ast.Pos are equal.

func AssertEqualRanges

func AssertEqualRanges(t *testing.T, got, want info.Range, fmtargs ...any)

AssertEqualRanges checks if two ranges are equal. If the wanted range is zero value of the type no check will be performed since this communicates that the caller is not interested on validating the range.

func AssertEqualSets

func AssertEqualSets[T comparable](t *testing.T, got, want []T)

AssertEqualSets checks if two sets contains the same elements independent of order (handles slices as sets).

func AssertFileContentEquals

func AssertFileContentEquals(t *testing.T, fname string, want string)

AssertFileContentEquals asserts that file fname has the content of want. It assumes the file content is a unicode string.

func AssertFileEquals

func AssertFileEquals(t *testing.T, filepath1, filepath2 string)

AssertFileEquals asserts that the two given files are the same. It assumes they are text files and shows a diff in case they are not the same.

func AssertGenCodeEquals

func AssertGenCodeEquals(t *testing.T, got string, want string)

AssertGenCodeEquals checks if got gen code equals want. Since got is generated by Terramate it will be stripped of its Terramate header (if present) before comparing with want.

func AssertStackImports

func AssertStackImports(t *testing.T, rootdir string, stackHostPath string, want []string)

AssertStackImports checks that the given stack has all the wanted import definitions. The wanted imports is a slice of the sources that are imported on each block.

func AssertStacks added in v0.4.4

func AssertStacks(t testing.TB, got, want config.Stack)

AssertStacks asserts that s1 and s2 are equal.

func AssertTerramateConfig

func AssertTerramateConfig(t *testing.T, got, want hcl.Config)

AssertTerramateConfig checks if two given Terramate configs are equal.

func AssertTreeEquals

func AssertTreeEquals(t *testing.T, dir1, dir2 string)

AssertTreeEquals asserts that the two given directories are the same. This means they must have the same files and also same subdirs with same files inside recursively.

func CanonPath

func CanonPath(t testing.TB, path string) string

CanonPath returns a canonical absolute path for the given path. Fails the test if any error is found.

func Chmod

func Chmod(fname string, mode fs.FileMode) error

Chmod changes file permissions.

func DoesNotExist added in v0.4.3

func DoesNotExist(t testing.TB, dir, fname string)

DoesNotExist calls os.Stat and asserts that the entry does not exist

func EmptyRepo

func EmptyRepo(t testing.TB, bare bool) string

EmptyRepo creates and initializes a git repository and checks for errors. If bare is provided, the repository is for revisions (ie: for pushs)

func FixupRangeOnAsserts

func FixupRangeOnAsserts(dir string, asserts []config.Assert)

FixupRangeOnAsserts fixes the range on all the given asserts. It assumes the asserts where created with relative paths and will join the relative path with the given dir to provide a final absolute path.

func Getwd

func Getwd(t testing.TB) string

Getwd gets the current working dir of the process

func IsDir added in v0.4.3

func IsDir(t testing.TB, dir, fname string)

IsDir calls os.Stat and asserts that the entry is a directory

func IsFile added in v0.4.3

func IsFile(t testing.TB, dir, fname string)

IsFile calls os.Stat and asserts that the entry is a file

func LookPath

func LookPath(t *testing.T, file string) string

LookPath is identical to exec.LookPath except it will fail the test if the lookup fails

func Mkdir

func Mkdir(t testing.TB, base string, name string) string

Mkdir creates a directory inside base.

func MkdirAll

func MkdirAll(t testing.TB, path string)

MkdirAll creates a temporary directory with default test permission bits.

func MkdirAll2 added in v0.4.1

func MkdirAll2(t testing.TB, path string, perm fs.FileMode)

MkdirAll2 creates a temporary directory with provided permissions.

func NewExpr

func NewExpr(t *testing.T, expr string) hhcl.Expression

NewExpr parses the given string and returns a hcl.Expression.

func NewGitWrapper

func NewGitWrapper(t testing.TB, wd string, env []string) *git.Git

NewGitWrapper tests the creation of a git wrapper and returns it if success. The env is the list of environment variables to be passed to git but if nil is provided then the complete os.Environ() is used.

func NewRepo

func NewRepo(t testing.TB) string

NewRepo creates and initializes a repository for terramate use cases. It initializes two repositories, one for working and other bare for the "remote". It sets up the working repository with a "origin" remote pointing to the local "bare" repository and push a initial main commit onto origin/main. The working git repository is returned and the other is automatically cleaned up when the test function finishes.

func NonExistingDir

func NonExistingDir(t testing.TB) string

NonExistingDir returns a non-existing directory.

func ParseSource

func ParseSource(t *testing.T, source string) tf.Source

ParseSource calls tf.ParseSource failing the test if it fails.

func ParseTerramateConfig

func ParseTerramateConfig(t *testing.T, dir string) hcl.Config

ParseTerramateConfig parses the Terramate configuration found on the given dir, returning the parsed configuration.

func PrependToPath added in v0.4.3

func PrependToPath(env []string, dir string) ([]string, bool)

PrependToPath prepend a directory to the OS PATH variable in a portable way. It returns the new env slice and a boolean telling if the env was updated or not.

func ReadDir

func ReadDir(t testing.TB, dir string) []os.DirEntry

ReadDir calls os.Readir asserting the success of the operation.

func ReadFile

func ReadFile(t testing.TB, dir, fname string) []byte

ReadFile reads the content of fname from dir directory.

func RelPath

func RelPath(t testing.TB, basepath, targetpath string) string

RelPath does the same as filepath.Rel but failing the test if an error is found.

func RemoveAll

func RemoveAll(t testing.TB, path string)

RemoveAll removes the directory and any of its children files and directories.

func RemoveFile

func RemoveFile(t testing.TB, dir, fname string)

RemoveFile removes the file fname from dir directory. If the files doesn't exists, it succeeds.

func Symlink(t testing.TB, oldname, newname string)

Symlink calls os.Symlink failing the test if there is an error.

func TempDir

func TempDir(t testing.TB) string

TempDir creates a temporary directory.

func WriteFile

func WriteFile(t testing.TB, dir string, filename string, content string) string

WriteFile writes content to a filename inside dir directory. If dir is empty string then the file is created inside a temporary directory.

func WriteRootConfig

func WriteRootConfig(t testing.TB, rootdir string)

WriteRootConfig writes a basic terramate root config.

Types

This section is empty.

Directories

Path Synopsis
Package cloud provides testing helpers for the TMC cloud.
Package cloud provides testing helpers for the TMC cloud.
Package errors provides useful assert functions for handling errors on tests
Package errors provides useful assert functions for handling errors on tests
Package hclutils provides test utils related to hcl.
Package hclutils provides test utils related to hcl.
info
Package info provides functions useful to create types like [info.Range]
Package info provides functions useful to create types like [info.Range]
Package hclwrite aims to provide some facilities making it easier/safer to generate HCL code for testing purposes.
Package hclwrite aims to provide some facilities making it easier/safer to generate HCL code for testing purposes.
hclutils
Package hclutils provides useful functions to build HCL documents.
Package hclutils provides useful functions to build HCL documents.
Package ls provides test utilities used when testing the Terramate Language Server.
Package ls provides test utilities used when testing the Terramate Language Server.
Package sandbox provides an easy way to setup isolated terramate projects that can be used on testing, acting like sandboxes.
Package sandbox provides an easy way to setup isolated terramate projects that can be used on testing, acting like sandboxes.

Jump to

Keyboard shortcuts

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