test: use standard logging methods from testing for the test helpers

Use the logging methods from testing.TB to make use of tb.Helper(). This
allows the tests to log the filename and line number in which the test
helper was called. Previously the test helper was logged which is rarely
useful.
This commit is contained in:
Michael Eischer 2023-05-05 23:26:13 +02:00
parent 658aa4c0f7
commit c3212ab6a6
2 changed files with 15 additions and 13 deletions

View File

@ -31,6 +31,7 @@ import (
) )
func parseIDsFromReader(t testing.TB, rd io.Reader) restic.IDs { func parseIDsFromReader(t testing.TB, rd io.Reader) restic.IDs {
t.Helper()
IDs := restic.IDs{} IDs := restic.IDs{}
sc := bufio.NewScanner(rd) sc := bufio.NewScanner(rd)
@ -148,6 +149,7 @@ func testRunRestoreAssumeFailure(snapshotID string, opts RestoreOptions, gopts G
} }
func testRunCheck(t testing.TB, gopts GlobalOptions) { func testRunCheck(t testing.TB, gopts GlobalOptions) {
t.Helper()
opts := CheckOptions{ opts := CheckOptions{
ReadData: true, ReadData: true,
CheckUnused: true, CheckUnused: true,

View File

@ -3,13 +3,11 @@ package test
import ( import (
"compress/bzip2" "compress/bzip2"
"compress/gzip" "compress/gzip"
"fmt"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime"
"testing" "testing"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -19,30 +17,28 @@ import (
// Assert fails the test if the condition is false. // Assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) { func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition { if !condition {
_, file, line, _ := runtime.Caller(1) tb.Fatalf("\033[31m"+msg+"\033[39m\n\n", v...)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
} }
} }
// OK fails the test if an err is not nil. // OK fails the test if an err is not nil.
func OK(tb testing.TB, err error) { func OK(tb testing.TB, err error) {
tb.Helper()
if err != nil { if err != nil {
_, file, line, _ := runtime.Caller(1) tb.Fatalf("\033[31munexpected error: %+v\033[39m\n\n", err)
fmt.Printf("\033[31m%s:%d: unexpected error: %+v\033[39m\n\n", filepath.Base(file), line, err)
tb.FailNow()
} }
} }
// OKs fails the test if any error from errs is not nil. // OKs fails the test if any error from errs is not nil.
func OKs(tb testing.TB, errs []error) { func OKs(tb testing.TB, errs []error) {
tb.Helper()
errFound := false errFound := false
for _, err := range errs { for _, err := range errs {
if err != nil { if err != nil {
errFound = true errFound = true
_, file, line, _ := runtime.Caller(1) tb.Logf("\033[31munexpected error: %+v\033[39m\n\n", err.Error())
fmt.Printf("\033[31m%s:%d: unexpected error: %+v\033[39m\n\n", filepath.Base(file), line, err.Error())
} }
} }
if errFound { if errFound {
@ -52,10 +48,9 @@ func OKs(tb testing.TB, errs []error) {
// Equals fails the test if exp is not equal to act. // Equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}) { func Equals(tb testing.TB, exp, act interface{}) {
tb.Helper()
if !reflect.DeepEqual(exp, act) { if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1) tb.Fatalf("\033[31m\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", exp, act)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
} }
} }
@ -92,6 +87,7 @@ func Random(seed, count int) []byte {
// SetupTarTestFixture extracts the tarFile to outputDir. // SetupTarTestFixture extracts the tarFile to outputDir.
func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) { func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
t.Helper()
input, err := os.Open(tarFile) input, err := os.Open(tarFile)
OK(t, err) OK(t, err)
defer func() { defer func() {
@ -130,6 +126,7 @@ func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
// Env creates a test environment and extracts the repository fixture. // Env creates a test environment and extracts the repository fixture.
// Returned is the repo path and a cleanup function. // Returned is the repo path and a cleanup function.
func Env(t testing.TB, repoFixture string) (repodir string, cleanup func()) { func Env(t testing.TB, repoFixture string) (repodir string, cleanup func()) {
t.Helper()
tempdir, err := os.MkdirTemp(TestTempDir, "restic-test-env-") tempdir, err := os.MkdirTemp(TestTempDir, "restic-test-env-")
OK(t, err) OK(t, err)
@ -159,6 +156,7 @@ func isFile(fi os.FileInfo) bool {
// This is mainly used for tests on Windows, which is unable to delete a file // This is mainly used for tests on Windows, which is unable to delete a file
// set read-only. // set read-only.
func ResetReadOnly(t testing.TB, dir string) { func ResetReadOnly(t testing.TB, dir string) {
t.Helper()
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
if fi == nil { if fi == nil {
return err return err
@ -183,6 +181,7 @@ func ResetReadOnly(t testing.TB, dir string) {
// RemoveAll recursively resets the read-only flag of all files and dirs and // RemoveAll recursively resets the read-only flag of all files and dirs and
// afterwards uses os.RemoveAll() to remove the path. // afterwards uses os.RemoveAll() to remove the path.
func RemoveAll(t testing.TB, path string) { func RemoveAll(t testing.TB, path string) {
t.Helper()
ResetReadOnly(t, path) ResetReadOnly(t, path)
err := os.RemoveAll(path) err := os.RemoveAll(path)
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
@ -194,6 +193,7 @@ func RemoveAll(t testing.TB, path string) {
// TempDir returns a temporary directory that is removed by t.Cleanup, // TempDir returns a temporary directory that is removed by t.Cleanup,
// except if TestCleanupTempDirs is set to false. // except if TestCleanupTempDirs is set to false.
func TempDir(t testing.TB) string { func TempDir(t testing.TB) string {
t.Helper()
tempdir, err := os.MkdirTemp(TestTempDir, "restic-test-") tempdir, err := os.MkdirTemp(TestTempDir, "restic-test-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)