2024-01-15 04:45:34 +03:00
|
|
|
// Package tu contains basic generic test utilities.
|
|
|
|
package tu
|
2022-12-17 05:09:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-19 08:28:09 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
2024-01-15 04:45:34 +03:00
|
|
|
"os/exec"
|
2023-06-13 19:06:18 +03:00
|
|
|
"path/filepath"
|
2022-12-17 05:09:49 +03:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-04-19 08:28:09 +03:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2022-12-18 07:31:06 +03:00
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/otiai10/copy"
|
2023-07-27 07:19:11 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-07-27 07:19:11 +03:00
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/ioz"
|
2022-12-18 07:31:06 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2022-12-17 05:09:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 StructFieldValue(fieldName string, strct any) any {
|
|
|
|
if strct == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// zv is the zero value of reflect.Value, which can be returned by FieldByName
|
|
|
|
zv := reflect.Value{}
|
|
|
|
|
|
|
|
e := reflect.Indirect(reflect.ValueOf(strct))
|
|
|
|
if e.Kind() != reflect.Struct {
|
|
|
|
panic(fmt.Sprintf("strct expected to be struct but was %s", e.Kind()))
|
|
|
|
}
|
|
|
|
|
|
|
|
f := e.FieldByName(fieldName)
|
2022-12-18 09:07:38 +03:00
|
|
|
if f == zv { //nolint:govet
|
|
|
|
// According to govet:
|
|
|
|
//
|
|
|
|
// reflectvaluecompare: avoid using == with reflect.Value
|
|
|
|
//
|
|
|
|
// Maybe we should be using f.IsZero instead?
|
|
|
|
|
2022-12-17 05:09:49 +03:00
|
|
|
panic(fmt.Sprintf("struct (%T) does not have field {%s}", strct, fieldName))
|
|
|
|
}
|
|
|
|
fieldValue := f.Interface()
|
|
|
|
return fieldValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 SliceFieldValues(fieldName string, slice any) []any {
|
|
|
|
if slice == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := reflect.ValueOf(slice)
|
|
|
|
if s.Kind() != reflect.Slice {
|
|
|
|
panic(fmt.Sprintf("arg slice expected to be a slice, but was {%T}", slice))
|
|
|
|
}
|
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
iSlice := AnySlice(slice)
|
2022-12-17 05:09:49 +03:00
|
|
|
retVals := make([]any, len(iSlice))
|
|
|
|
|
|
|
|
for i := range iSlice {
|
|
|
|
retVals[i] = StructFieldValue(fieldName, iSlice[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVals
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 SliceFieldKeyValues(keyFieldName, valFieldName string, slice any) map[any]any {
|
|
|
|
if slice == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := reflect.ValueOf(slice)
|
|
|
|
if s.Kind() != reflect.Slice {
|
|
|
|
panic(fmt.Sprintf("arg slice expected to be a slice, but was {%T}", slice))
|
|
|
|
}
|
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
iSlice := AnySlice(slice)
|
2022-12-17 05:09:49 +03:00
|
|
|
m := make(map[any]any, len(iSlice))
|
|
|
|
|
|
|
|
for i := range iSlice {
|
|
|
|
key := StructFieldValue(keyFieldName, iSlice[i])
|
|
|
|
val := StructFieldValue(valFieldName, iSlice[i])
|
|
|
|
|
|
|
|
m[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
// 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
|
2022-12-17 05:09:49 +03:00
|
|
|
// 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.
|
2024-01-15 04:45:34 +03:00
|
|
|
//
|
|
|
|
// REVISIT: This function predates generics. It can probably be
|
|
|
|
// removed, or at a minimum, moved to pkg loz.
|
2023-08-16 18:09:50 +03:00
|
|
|
func AnySlice(slice any) []any {
|
2022-12-17 05:09:49 +03:00
|
|
|
if slice == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's already an []interface{}, then just return
|
|
|
|
if iSlice, ok := slice.([]any); ok {
|
|
|
|
return iSlice
|
|
|
|
}
|
|
|
|
|
|
|
|
s := reflect.ValueOf(slice)
|
|
|
|
if s.Kind() != reflect.Slice {
|
|
|
|
panic(fmt.Sprintf("arg slice expected to be a slice, but was {%T}", slice))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the distinction between nil and empty slice input
|
|
|
|
if s.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]any, s.Len())
|
|
|
|
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
ret[i] = s.Index(i).Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 Name(args ...any) string {
|
|
|
|
var parts []string
|
|
|
|
var s string
|
|
|
|
for _, a := range args {
|
2023-04-09 17:44:27 +03:00
|
|
|
v := stringz.Val(a)
|
|
|
|
s = fmt.Sprintf("%v", v)
|
2022-12-17 05:09:49 +03:00
|
|
|
if s == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-18 09:07:38 +03:00
|
|
|
s = strings.ReplaceAll(s, "/", "_")
|
2023-01-01 06:17:44 +03:00
|
|
|
s = strings.ReplaceAll(s, ":", "_")
|
|
|
|
s = strings.ReplaceAll(s, `\`, "_")
|
2024-01-15 04:45:34 +03:00
|
|
|
s = stringz.SanitizeFilename(s)
|
|
|
|
s = stringz.EllipsifyASCII(s, 28) // we don't want it to be too long
|
2022-12-17 05:09:49 +03:00
|
|
|
parts = append(parts, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.Join(parts, "_")
|
|
|
|
if s == "" {
|
|
|
|
return "empty"
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssertCompareFunc matches several of the testify/require funcs.
|
|
|
|
// It can be used to choose assertion comparison funcs in test cases.
|
|
|
|
type AssertCompareFunc func(require.TestingT, any, any, ...any)
|
|
|
|
|
|
|
|
// Verify that a sample of the require funcs match AssertCompareFunc.
|
|
|
|
var (
|
|
|
|
_ AssertCompareFunc = require.Equal
|
|
|
|
_ AssertCompareFunc = require.GreaterOrEqual
|
|
|
|
_ AssertCompareFunc = require.Greater
|
|
|
|
)
|
2023-04-19 08:28:09 +03:00
|
|
|
|
|
|
|
// DirCopy copies the contents of sourceDir to a temp dir.
|
|
|
|
// If keep is false, temp dir will be cleaned up on test exit.
|
2024-01-27 16:43:17 +03:00
|
|
|
func DirCopy(tb testing.TB, sourceDir string, keep bool) (tmpDir string) {
|
|
|
|
tb.Helper()
|
2023-04-19 08:28:09 +03:00
|
|
|
var err error
|
|
|
|
if keep {
|
2024-01-27 16:43:17 +03:00
|
|
|
tmpDir, err = os.MkdirTemp("", sanitizeTestName(tb.Name())+"_*")
|
|
|
|
require.NoError(tb, err)
|
2023-04-19 08:28:09 +03:00
|
|
|
} else {
|
2024-01-27 16:43:17 +03:00
|
|
|
tmpDir = tb.TempDir()
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = copy.Copy(sourceDir, tmpDir)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
|
|
|
tb.Logf("Copied %s -> %s", sourceDir, tmpDir)
|
2023-04-19 08:28:09 +03:00
|
|
|
return tmpDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanitizeTestName sanitizes a test name. This impl is copied
|
|
|
|
// from testing.T.TempDir.
|
|
|
|
func sanitizeTestName(name string) string {
|
|
|
|
// Drop unusual characters (such as path separators or
|
|
|
|
// characters interacting with globs) from the directory name to
|
|
|
|
// avoid surprising os.MkdirTemp behavior.
|
|
|
|
mapper := func(r rune) rune {
|
|
|
|
if r < utf8.RuneSelf {
|
|
|
|
const allowed = "!#$%&()+,-.=@^_{}~ "
|
|
|
|
if '0' <= r && r <= '9' ||
|
|
|
|
'a' <= r && r <= 'z' ||
|
|
|
|
'A' <= r && r <= 'Z' {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
if strings.ContainsRune(allowed, r) {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
} else if unicode.IsLetter(r) || unicode.IsNumber(r) {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
pattern := strings.Map(mapper, name)
|
|
|
|
return pattern
|
|
|
|
}
|
|
|
|
|
2024-01-27 16:43:17 +03:00
|
|
|
// Writer returns an io.Writer whose Write method invokes tb.Log.
|
2023-04-19 08:28:09 +03:00
|
|
|
// A newline is prepended to the log output.
|
2024-01-27 16:43:17 +03:00
|
|
|
func Writer(tb testing.TB) io.Writer { //nolint:thelper
|
|
|
|
return &tWriter{tb}
|
2023-04-19 08:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.Writer = (*tWriter)(nil)
|
|
|
|
|
|
|
|
type tWriter struct {
|
|
|
|
t testing.TB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tWriter) Write(p []byte) (n int, err error) {
|
|
|
|
if len(p) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
t.t.Helper()
|
2023-04-19 08:28:09 +03:00
|
|
|
t.t.Log("\n" + string(p))
|
|
|
|
return len(p), nil
|
|
|
|
}
|
2023-06-13 19:06:18 +03:00
|
|
|
|
|
|
|
// Chdir changes the working directory to dir, or if dir is empty,
|
2024-01-15 04:45:34 +03:00
|
|
|
// to a temp dir. On test conclusion, the original working dir is restored,
|
2023-06-13 19:06:18 +03:00
|
|
|
// and the temp dir deleted (if applicable). The absolute path
|
|
|
|
// of the changed working dir is returned.
|
2024-01-27 16:43:17 +03:00
|
|
|
func Chdir(tb testing.TB, dir string) (absDir string) {
|
|
|
|
tb.Helper()
|
2023-06-13 19:06:18 +03:00
|
|
|
origDir, err := os.Getwd()
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2023-06-13 19:06:18 +03:00
|
|
|
|
|
|
|
if filepath.IsAbs(dir) {
|
|
|
|
absDir = dir
|
|
|
|
} else {
|
|
|
|
absDir, err = filepath.Abs(dir)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2023-06-13 19:06:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if dir == "" {
|
2024-01-27 16:43:17 +03:00
|
|
|
tmpDir := tb.TempDir()
|
|
|
|
tb.Cleanup(func() {
|
2023-06-13 19:06:18 +03:00
|
|
|
_ = os.Remove(tmpDir)
|
|
|
|
})
|
|
|
|
dir = tmpDir
|
|
|
|
}
|
|
|
|
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, os.Chdir(dir))
|
|
|
|
tb.Cleanup(func() {
|
2023-06-13 19:06:18 +03:00
|
|
|
_ = os.Chdir(origDir)
|
|
|
|
})
|
|
|
|
|
|
|
|
return absDir
|
|
|
|
}
|
|
|
|
|
2023-07-27 07:19:11 +03:00
|
|
|
// 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.
|
2024-01-27 16:43:17 +03:00
|
|
|
func WriteTemp(tb testing.TB, pattern string, b []byte, cleanup bool) (fpath string) {
|
|
|
|
tb.Helper()
|
2023-07-27 07:19:11 +03:00
|
|
|
f, err := os.CreateTemp("", pattern)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2023-07-27 07:19:11 +03:00
|
|
|
|
|
|
|
written, err := f.Write(b)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2023-07-27 07:19:11 +03:00
|
|
|
fpath = f.Name()
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, f.Close())
|
2023-07-27 07:19:11 +03:00
|
|
|
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Logf("Wrote %d bytes to: %s", written, fpath)
|
2023-07-27 07:19:11 +03:00
|
|
|
|
|
|
|
if cleanup {
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Cleanup(func() {
|
|
|
|
assert.NoError(tb, os.Remove(fpath))
|
2023-07-27 07:19:11 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return fpath
|
|
|
|
}
|
2023-08-21 19:05:17 +03:00
|
|
|
|
|
|
|
// MustAbsFilepath invokes filepath.Join on elems, and then filepath.Abs
|
|
|
|
// on the result. It panics on error.
|
|
|
|
func MustAbsFilepath(elems ...string) string {
|
|
|
|
fp := filepath.Join(elems...)
|
|
|
|
s, err := filepath.Abs(fp)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2024-01-15 04:45:34 +03:00
|
|
|
|
2024-01-27 01:18:38 +03:00
|
|
|
// MustStat invokes os.Stat on fp, and fails t on error.
|
2024-01-27 16:43:17 +03:00
|
|
|
func MustStat(tb testing.TB, fp string) os.FileInfo {
|
|
|
|
tb.Helper()
|
2024-01-27 01:18:38 +03:00
|
|
|
fi, err := os.Stat(fp)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2024-01-27 01:18:38 +03:00
|
|
|
return fi
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
// TempDir is the standard means for obtaining a temp dir for tests.
|
|
|
|
// If arg clean is true, the temp dir is created via t.TempDir, and
|
|
|
|
// thus is deleted on test cleanup.
|
2024-01-27 16:43:17 +03:00
|
|
|
func TempDir(tb testing.TB, clean bool) string {
|
|
|
|
tb.Helper()
|
2024-01-15 04:45:34 +03:00
|
|
|
if clean {
|
2024-01-27 16:43:17 +03:00
|
|
|
return filepath.Join(tb.TempDir(), "sq-test", "tmp")
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fp := filepath.Join(os.TempDir(), "sq-test", stringz.Uniq8(), "tmp")
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, ioz.RequireDir(fp))
|
2024-01-15 04:45:34 +03:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
// TempFile returns the path to a temp file with the given name, in a unique
|
|
|
|
// temp dir. The file is not created. If arg clean is true, the parent temp
|
|
|
|
// dir is created via t.TempDir, and thus is deleted on test cleanup.
|
2024-01-27 16:43:17 +03:00
|
|
|
func TempFile(tb testing.TB, name string, clean bool) string {
|
|
|
|
tb.Helper()
|
|
|
|
fp := filepath.Join(TempDir(tb, clean), name)
|
2024-01-15 04:45:34 +03:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheDir is the standard means for obtaining a cache dir for tests.
|
|
|
|
// If arg clean is true, the cache dir is created via t.TempDir, and
|
|
|
|
// thus is deleted on test cleanup.
|
2024-01-27 16:43:17 +03:00
|
|
|
func CacheDir(tb testing.TB, clean bool) string {
|
|
|
|
tb.Helper()
|
2024-01-15 04:45:34 +03:00
|
|
|
if clean {
|
2024-01-27 16:43:17 +03:00
|
|
|
return filepath.Join(tb.TempDir(), "sq-test", "cache")
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fp := filepath.Join(os.TempDir(), "sq-test", stringz.Uniq8(), "cache")
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, ioz.RequireDir(fp))
|
2024-01-15 04:45:34 +03:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFileToString invokes ioz.ReadFileToString, failing t if
|
|
|
|
// an error occurs.
|
2024-01-27 16:43:17 +03:00
|
|
|
func ReadFileToString(tb testing.TB, name string) string {
|
|
|
|
tb.Helper()
|
2024-01-15 04:45:34 +03:00
|
|
|
s, err := ioz.ReadFileToString(name)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2024-01-15 04:45:34 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadToString reads all bytes from r and returns them as a string.
|
|
|
|
// If r is an io.Closer, it is closed.
|
2024-01-27 16:43:17 +03:00
|
|
|
func ReadToString(tb testing.TB, r io.Reader) string {
|
|
|
|
tb.Helper()
|
2024-01-15 04:45:34 +03:00
|
|
|
b, err := io.ReadAll(r)
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2024-01-15 04:45:34 +03:00
|
|
|
if r, ok := r.(io.Closer); ok {
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, r.Close())
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFileCount is a debugging function that returns the count
|
|
|
|
// of open file handles for the current process via shelling out
|
2024-01-27 01:18:38 +03:00
|
|
|
// 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.
|
2024-01-27 16:43:17 +03:00
|
|
|
func OpenFileCount(tb testing.TB, log bool, label string) int {
|
|
|
|
tb.Helper()
|
|
|
|
count, out := doOpenFileCount(tb)
|
2024-01-27 01:18:38 +03:00
|
|
|
var msg string
|
|
|
|
|
|
|
|
if label != "" {
|
|
|
|
msg = "\n\n" + strings.Repeat("=", 40) + "\n" +
|
|
|
|
label + "\n" + strings.Repeat("=", 40) + "\n\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
msg += fmt.Sprintf("Open files for pid [%d]: %d", os.Getpid(), count)
|
2024-01-15 04:45:34 +03:00
|
|
|
if log {
|
|
|
|
msg += "\n\n" + out
|
|
|
|
}
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Log(msg)
|
2024-01-15 04:45:34 +03:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2024-01-27 16:43:17 +03:00
|
|
|
func doOpenFileCount(tb testing.TB) (count int, out string) {
|
|
|
|
tb.Helper()
|
|
|
|
SkipWindows(tb, "OpenFileCount not implemented on Windows")
|
2024-01-15 04:45:34 +03:00
|
|
|
|
|
|
|
c := fmt.Sprintf("lsof -p %v", os.Getpid())
|
|
|
|
b, err := exec.Command("/bin/sh", "-c", c).Output()
|
2024-01-27 16:43:17 +03:00
|
|
|
require.NoError(tb, err)
|
2024-01-15 04:45:34 +03:00
|
|
|
lines := strings.Split(string(b), "\n")
|
|
|
|
count = len(lines) - 1
|
|
|
|
return count, string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2024-01-27 16:43:17 +03:00
|
|
|
func DiffOpenFileCount(tb testing.TB, log bool) {
|
|
|
|
tb.Helper()
|
|
|
|
openingCount, openingOut := doOpenFileCount(tb)
|
2024-01-15 04:45:34 +03:00
|
|
|
if log {
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Logf("START: Open files for pid [%d]: %d\n\n%s", os.Getpid(), openingCount, openingOut)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Cleanup(func() {
|
|
|
|
closingCount, closingOut := doOpenFileCount(tb)
|
2024-01-15 04:45:34 +03:00
|
|
|
if log {
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Logf("END: Open files for pid [%d]: %d\n\n%s", os.Getpid(), closingCount, closingOut)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
if openingCount != closingCount {
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Logf("Open file count changed from %d to %d", openingCount, closingCount)
|
2024-01-15 04:45:34 +03:00
|
|
|
} else {
|
2024-01-27 16:43:17 +03:00
|
|
|
tb.Logf("Open file count unchanged: %d", openingCount)
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseProxy sets HTTP_PROXY and HTTPS_PROXY to localhost:9001.
|
2024-01-27 16:43:17 +03:00
|
|
|
func UseProxy(tb testing.TB) {
|
|
|
|
tb.Helper()
|
|
|
|
tb.Setenv("HTTP_PROXY", "http://localhost:9001")
|
|
|
|
tb.Setenv("HTTPS_PROXY", "http://localhost:9001")
|
2024-01-15 04:45:34 +03:00
|
|
|
}
|