Documentation ¶
Overview ¶
Package tu contains basic generic test utilities.
Index ¶
- func AnySlice(slice any) []any
- func Chdir(tb testing.TB, dir string) (absDir string)
- func DiffOpenFileCount(tb testing.TB, log bool)
- func DirCopy(tb testing.TB, sourceDir string) (tmpDir string)
- func MustAbsFilepath(elems ...string) string
- func MustStat(tb testing.TB, fp string) os.FileInfo
- func Name(args ...any) string
- func OpenFileCount(tb testing.TB, log bool, label string) int
- func ReadFileToString(tb testing.TB, name string) string
- func ReadToString(tb testing.TB, r io.Reader) string
- func RequireNoTake[C any](tb testing.TB, c <-chan C, msgAndArgs ...any)
- func RequireTake[C any](tb testing.TB, c <-chan C, msgAndArgs ...any)
- func SkipIf(tb testing.TB, cond bool, format string, args ...any)
- func SkipIssue(tb testing.TB, issue GHIssue)
- func SkipIssueWindows(tb testing.TB, issue GHIssue)
- func SkipShort(tb testing.TB, skip bool)
- func SkipWindows(tb testing.TB, format string, args ...any)
- func SkipWindowsIf(tb testing.TB, cond bool, format string, args ...any)
- func SliceFieldKeyValues(keyFieldName, valFieldName string, slice any) map[any]any
- func SliceFieldValues(fieldName string, slice any) []any
- func StructFieldValue(fieldName string, strct any) any
- func TempDir(tb testing.TB, subs ...string) string
- func TempFile(tb testing.TB, name string) string
- func UseProxy(tb testing.TB)
- func WriteTemp(tb testing.TB, pattern string, b []byte, cleanup bool) (fpath string)
- func Writer(tb testing.TB) io.Writer
- type AssertCompareFunc
- type GHIssue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnySlice ¶
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 ¶
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 ¶
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 MustAbsFilepath ¶
MustAbsFilepath invokes filepath.Join on elems, and then filepath.Abs on the result. It panics on error.
func Name ¶
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 ¶
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 ¶
ReadFileToString invokes ioz.ReadFileToString, failing t if an error occurs.
func ReadToString ¶
ReadToString reads all bytes from r and returns them as a string. If r is an io.Closer, it is closed.
func RequireNoTake ¶
RequireNoTake fails if a value is taken from c.
func RequireTake ¶
RequireTake fails if a value is not taken from c.
func SkipIf ¶
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 SkipIssueWindows ¶
SkipIssueWindows skips tb on windows due to the specified GitHub issue.
func SkipWindows ¶
SkipWindows skips tb if running on Windows.
func SkipWindowsIf ¶
SkipWindowsIf skips tb if running on Windows and b is true.
func SliceFieldKeyValues ¶
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 ¶
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 ¶
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 ¶
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 ¶
TempFile returns the path to a temp file with the given name, in a unique temp dir. The file is not created.
Types ¶
type AssertCompareFunc ¶
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 )