diff --git a/internal/fs/helpers.go b/internal/fs/helpers.go index 1789f2bca..7235834ae 100644 --- a/internal/fs/helpers.go +++ b/internal/fs/helpers.go @@ -3,6 +3,8 @@ package fs import ( "os" "testing" + + "github.com/restic/restic/internal/test" ) // IsRegularFile returns true if fi belongs to a normal file. If fi is nil, @@ -17,6 +19,8 @@ func IsRegularFile(fi os.FileInfo) bool { // TestChdir changes the current directory to dest, the function back returns to the previous directory. func TestChdir(t testing.TB, dest string) (back func()) { + test.Helper(t).Helper() + prev, err := os.Getwd() if err != nil { t.Fatal(err) @@ -29,6 +33,7 @@ func TestChdir(t testing.TB, dest string) (back func()) { } return func() { + test.Helper(t).Helper() t.Logf("chdir back to %v", prev) err = os.Chdir(prev) if err != nil { diff --git a/internal/repository/testing.go b/internal/repository/testing.go index a49072335..739aa4d62 100644 --- a/internal/repository/testing.go +++ b/internal/repository/testing.go @@ -42,6 +42,7 @@ const testChunkerPol = chunker.Pol(0x3DA3358B4DC173) // password. If be is nil, an in-memory backend is used. A constant polynomial // is used for the chunker and low-security test parameters. func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r restic.Repository, cleanup func()) { + test.Helper(t).Helper() TestUseLowSecurityKDFParameters(t) restic.TestDisableCheckPolynomial(t) @@ -70,6 +71,7 @@ func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r restic.Reposi // a non-existing directory, a local backend is created there and this is used // instead. The directory is not removed, but left there for inspection. func TestRepository(t testing.TB) (r restic.Repository, cleanup func()) { + test.Helper(t).Helper() dir := os.Getenv("RESTIC_TEST_REPO") if dir != "" { _, err := os.Stat(dir) diff --git a/internal/test/helper.go b/internal/test/helper.go new file mode 100644 index 000000000..f0fc1f61b --- /dev/null +++ b/internal/test/helper.go @@ -0,0 +1,15 @@ +// +build go1.9 + +package test + +import "testing" + +// Helperer marks the current function as a test helper. +type Helperer interface { + Helper() +} + +// Helper returns a function that marks the current function as a helper function. +func Helper(t testing.TB) Helperer { + return t +} diff --git a/internal/test/helper_go18.go b/internal/test/helper_go18.go new file mode 100644 index 000000000..d4f8b8de6 --- /dev/null +++ b/internal/test/helper_go18.go @@ -0,0 +1,19 @@ +// +build !go1.9 + +package test + +import "testing" + +// Helperer marks the current function as a test helper. +type Helperer interface { + Helper() +} + +type fakeHelper struct{} + +func (fakeHelper) Helper() {} + +// Helper returns a function that marks the current function as a helper function. +func Helper(t testing.TB) Helperer { + return fakeHelper{} +}