Skip to content

Commit

Permalink
Add support for Go 1.14 testing Cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Dec 20, 2019
1 parent 8fc03ff commit b3c6267
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions fs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type helperT interface {
Helper()
}

type cleanupT interface {
Cleanup(f func())
}

// NewFile creates a new file in a temporary directory using prefix as part of
// the filename. The PathOps are applied to the before returning the File.
func NewFile(t assert.TestingT, prefix string, ops ...PathOp) *File {
Expand All @@ -50,6 +54,9 @@ func NewFile(t assert.TestingT, prefix string, ops ...PathOp) *File {
if tc, ok := t.(subtest.TestContext); ok {
tc.AddCleanup(file.Remove)
}
if ct, ok := t.(cleanupT); ok {
ct.Cleanup(file.Remove)
}
return file
}

Expand Down Expand Up @@ -90,6 +97,9 @@ func NewDir(t assert.TestingT, prefix string, ops ...PathOp) *Dir {
if tc, ok := t.(subtest.TestContext); ok {
tc.AddCleanup(dir.Remove)
}
if ct, ok := t.(cleanupT); ok {
ct.Cleanup(dir.Remove)
}
return dir
}

Expand Down

0 comments on commit b3c6267

Please sign in to comment.