tu

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package tu contains basic generic test utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnySlice

func AnySlice(slice any) []any

AnySlice converts a typed slice (such as []string) to []any. If slice is already of type []any, it is returned unmodified. Otherwise a new []any is constructed. If slice is nil, nil is returned. The function panics if slice is not a slice.

Note that this function uses reflection, and may panic. It is only to be used by test code.

REVISIT: This function predates generics. It can probably be removed, or at a minimum, moved to pkg loz.

func Chdir

func Chdir(tb testing.TB, dir string) (absDir string)

Chdir changes the working directory to dir, or if dir is empty, to a temp dir. On test conclusion, the original working dir is restored, and the temp dir deleted (if applicable). The absolute path of the changed working dir is returned.

func DiffOpenFileCount

func DiffOpenFileCount(tb testing.TB, log bool)

DiffOpenFileCount is a debugging function that compares the open file count at the start of the test with the count at the end of the test (via t.Cleanup). This function is skipped on Windows.

func DirCopy

func DirCopy(tb testing.TB, sourceDir string) (tmpDir string)

DirCopy copies the contents of sourceDir to a temp dir.

func MustAbsFilepath

func MustAbsFilepath(elems ...string) string

MustAbsFilepath invokes filepath.Join on elems, and then filepath.Abs on the result. It panics on error.

func MustStat

func MustStat(tb testing.TB, fp string) os.FileInfo

MustStat invokes os.Stat on fp, and fails t on error.

func Name

func Name(args ...any) string

Name is a convenience function for building a test name to pass to t.Run.

t.Run(testh.Name("my_test", 1), func(t *testing.T) {

The most common usage is with test names that are file paths.

testh.Name("path/to/file") --> "path_to_file"

Any element of arg that prints to empty string is skipped.

func OpenFileCount

func OpenFileCount(tb testing.TB, log bool, label string) int

OpenFileCount is a debugging function that returns the count of open file handles for the current process via shelling out to lsof. On the master branch, OpenFileCount probably should never be called; it's a debugging function for use during development.

If arg log is true, the output of lsof is logged.

This function is skipped on Windows.

func ReadFileToString

func ReadFileToString(tb testing.TB, name string) string

ReadFileToString invokes ioz.ReadFileToString, failing t if an error occurs.

func ReadToString

func ReadToString(tb testing.TB, r io.Reader) string

ReadToString reads all bytes from r and returns them as a string. If r is an io.Closer, it is closed.

func RequireNoTake

func RequireNoTake[C any](tb testing.TB, c <-chan C, msgAndArgs ...any)

RequireNoTake fails if a value is taken from c.

func RequireTake

func RequireTake[C any](tb testing.TB, c <-chan C, msgAndArgs ...any)

RequireTake fails if a value is not taken from c.

func SkipIf

func SkipIf(tb testing.TB, cond bool, format string, args ...any)

SkipIf skips tb if cond is true. If msgAndArgs is non-empty, its first element must be a string, which can be a format string if there are additional elements.

Examples:

tu.SkipIf(t, a == b)
tu.SkipIf(t, a == b, "skipping because a == b")
tu.SkipIf(t, a == b, "skipping because a is %v and b is %v", a, b)

func SkipIssue

func SkipIssue(tb testing.TB, issue GHIssue)

SkipIssue skips tb due to the specified GitHub issue.

func SkipIssueWindows

func SkipIssueWindows(tb testing.TB, issue GHIssue)

SkipIssueWindows skips tb on windows due to the specified GitHub issue.

func SkipShort

func SkipShort(tb testing.TB, skip bool)

SkipShort skips tb if testing.Short and arg skip are both true.

func SkipWindows

func SkipWindows(tb testing.TB, format string, args ...any)

SkipWindows skips tb if running on Windows.

func SkipWindowsIf

func SkipWindowsIf(tb testing.TB, cond bool, format string, args ...any)

SkipWindowsIf skips tb if running on Windows and b is true.

func SliceFieldKeyValues

func SliceFieldKeyValues(keyFieldName, valFieldName string, slice any) map[any]any

SliceFieldKeyValues is similar to SliceFieldValues, but instead of returning a slice of field values, it returns a map containing two field values, a "key" and a "value". For example:

persons := []*person{
  {Name: "Alice", Age: 42},
  {Name: "Bob", Age: 27},
}

m := SliceFieldKeyValues("Name", "Age", persons)
// map[Alice:42 Bob:27]

Note that this function uses reflection, and may panic. It is only to be used by test code.

See also: StructFieldValue, SliceFieldValues.

func SliceFieldValues

func SliceFieldValues(fieldName string, slice any) []any

SliceFieldValues takes a slice of structs, and returns a slice containing the value of fieldName for each element of slice.

Note that slice can be []interface{}, or a typed slice (e.g. []*Person). If slice is nil, nil is returned. If slice has len zero, an empty slice is returned. The function panics if slice is not a slice, or if any element of slice is not a struct (excepting nil elements).

Note that this function uses reflection, and may panic. It is only to be used by test code.

See also: StructFieldValue, SliceFieldKeyValues.

func StructFieldValue

func StructFieldValue(fieldName string, strct any) any

StructFieldValue extracts the value of fieldName from arg strct. If strct is nil, nil is returned. The function will panic if strct is not a struct (or pointer to struct), or if the struct does not have fieldName. The returned value may be nil if the field is a pointer and is nil.

Note that this function uses reflection, and may panic. It is only to be used by test code.

See also: SliceFieldValues, SliceFieldKeyValues.

func TempDir

func TempDir(tb testing.TB, subs ...string) string

TempDir is the standard means for obtaining a temp dir for tests. A new, unique temp dir is returned on each call. If arg subs is non-empty, that sub-directory structure is created within the parent temp dir. The returned value is an absolute path of the form:

# tu.TempDir(t):
/var/folders/68/qthw...0gn/T/sq/test/testh/tu/TestTempDir/69226/2_1706579687990637_f8f226d0

# tu.TempDir(t, "foo", "bar"):
/var/folders/68/qthw...0gn/T/sq/test/testh/tu/TestTempDir/69226/3_1706579687990706_efb99710/foo/bar

The returned dir is a subdir of os.TempDir(), and includes the package path and test name (sanitized), as well as the pid, created dir count, timestamp, and random value. We use this structure to make it easier to identify the calling test when debugging. The dir is created with perms 0777.

The caller is responsible for removing the dir if desired - it is NOT automatically deleted via t.Cleanup.

func TempFile

func TempFile(tb testing.TB, name string) string

TempFile returns the path to a temp file with the given name, in a unique temp dir. The file is not created.

func UseProxy

func UseProxy(tb testing.TB)

UseProxy sets HTTP_PROXY and HTTPS_PROXY to localhost:9001.

func WriteTemp

func WriteTemp(tb testing.TB, pattern string, b []byte, cleanup bool) (fpath string)

WriteTemp writes b to a temporary file. The pattern arg is used to generate the file name, per os.CreateTemp. If cleanup is true, the file is deleted on test cleanup.

func Writer

func Writer(tb testing.TB) io.Writer

Writer returns an io.Writer whose Write method invokes tb.Log. A newline is prepended to the log output.

Types

type AssertCompareFunc

type AssertCompareFunc func(require.TestingT, any, any, ...any)

AssertCompareFunc matches several of the testify/require funcs. It can be used to choose assertion comparison funcs in test cases.

type GHIssue

type GHIssue uint

GHIssue is a GitHub issue number. It is used with SkipIssue and SkipIssueWindows.

const (
	GH355SQLiteDecimalWin   GHIssue = 355 // https://github.com/neilotoole/sq/issues/355
	GH371ExcelSlowWin       GHIssue = 371 // https://github.com/neilotoole/sq/issues/371
	GH372ShellCompletionWin GHIssue = 372 // https://github.com/neilotoole/sq/issues/372
)

func (GHIssue) String

func (g GHIssue) String() string

String returns the URL of the GitHub issue.

Jump to

Keyboard shortcuts

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