testutils

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: BSD-3-Clause, MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package testutils provides some internal helpers for tests.

Index

Constants

This section is empty.

Variables

View Source
var MkdirAll = testutils.MkdirAll

MkdirAll is a wrapper around os.MkdirAll.

View Source
var Symlink = testutils.Symlink

Symlink is a wrapper around os.Symlink.

View Source
var WriteFile = testutils.WriteFile

WriteFile is a wrapper around os.WriteFile.

Functions

func CreateInTree

func CreateInTree(t TestingT, root, spec string)

CreateInTree creates a given inode inside the root directory.

Format:

dir <name> <?uid:gid:mode>
file <name> <?content> <?uid:gid:mode>
symlink <name> <target>
char <name> <major> <minor> <?uid:gid:mode>
block <name> <major> <minor> <?uid:gid:mode>
fifo <name> <?uid:gid:mode>
sock <name> <?uid:gid:mode>

func CreateTree

func CreateTree(t TestingT, specs ...string) string

CreateTree creates a rootfs tree using spec entries (as documented in CreateInTree). The returned path is the path to the root of the new tree.

func RequireRenameExchange

func RequireRenameExchange(t TestingT)

RequireRenameExchange skips the current test if renameat2(2) is not supported on the running system.

func RequireRoot

func RequireRoot(t TestingT)

RequireRoot skips the current test if we are not root.

func WithWithoutOpenat2

func WithWithoutOpenat2(doAuto bool, tRunFn TRunFunc, doFn TDoFunc)

WithWithoutOpenat2 runs a given test with and without openat2 (by forcefully disabling its usage).

Types

type TDoFunc

type TDoFunc func(ti TestingT)

TDoFunc is effectively a func(t *testing.T) function but using the TestingT interface to allow us to write testutils with non-test code. The argument is virtually guaranteed to be a *testing.T instance so you can just do a type assertion in the body of the closure.

type TRunFunc

type TRunFunc func(name string, doFn TDoFunc)

TRunFunc is a wrapper around t.Run but done with an interface that can be used in non-testing code. To use this, you should just define a wrapper function like this:

func tRunWrapper(t *testing.T) testutils.TRunFunc {
	return func(name string, doFn testutils.TDoFunc) {
		t.Run(name, func(t *testing.T) {
			doFn(t)
		})
	}
 }

and then use it with WithWithoutOpenat2 like so:

testutils.WithWithoutOpenat2(true, tRunWrapper(t), func(ti testutils.TestingT) {
	t := ti.(*testing.T) //nolint:forcetypeassert // guaranteed to be true and in test code
	/* test code */
})

type TestingT

type TestingT = testutils.TestingT

TestingT is an interface wrapper around *testing.T.

Jump to

Keyboard shortcuts

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