-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from dnephin/cleanup
Add calls to testing.TB.Cleanup() when available.
- Loading branch information
Showing
6 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/*Package cleanup handles migration to and support for the Go 1.14+ | ||
testing.TB.Cleanup() function. | ||
*/ | ||
package cleanup | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"gotest.tools/v3/x/subtest" | ||
) | ||
|
||
type cleanupT interface { | ||
Cleanup(f func()) | ||
} | ||
|
||
type logT interface { | ||
Log(...interface{}) | ||
} | ||
|
||
type helperT interface { | ||
Helper() | ||
} | ||
|
||
var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true" | ||
|
||
// Cleanup registers f as a cleanup function on t if any mechanisms are available. | ||
// | ||
// Skips registering f if TEST_NOCLEANUP is set to true. | ||
func Cleanup(t logT, f func()) { | ||
if ht, ok := t.(helperT); ok { | ||
ht.Helper() | ||
} | ||
if noCleanup { | ||
t.Log("skipping cleanup because TEST_NOCLEANUP was enabled.") | ||
return | ||
} | ||
if ct, ok := t.(cleanupT); ok { | ||
ct.Cleanup(f) | ||
return | ||
} | ||
if tc, ok := t.(subtest.TestContext); ok { | ||
tc.AddCleanup(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters