...

Source file src/gotest.tools/v3/internal/cleanup/cleanup.go

Documentation: gotest.tools/v3/internal/cleanup

     1  /*
     2  Package cleanup handles migration to and support for the Go 1.14+
     3  testing.TB.Cleanup() function.
     4  */
     5  package cleanup
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  )
    11  
    12  type cleanupT interface {
    13  	Cleanup(f func())
    14  }
    15  
    16  // implemented by gotest.tools/x/subtest.TestContext
    17  type addCleanupT interface {
    18  	AddCleanup(f func())
    19  }
    20  
    21  type logT interface {
    22  	Log(...interface{})
    23  }
    24  
    25  type helperT interface {
    26  	Helper()
    27  }
    28  
    29  var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true"
    30  
    31  // Cleanup registers f as a cleanup function on t if any mechanisms are available.
    32  //
    33  // Skips registering f if TEST_NOCLEANUP is set to true.
    34  func Cleanup(t logT, f func()) {
    35  	if ht, ok := t.(helperT); ok {
    36  		ht.Helper()
    37  	}
    38  	if noCleanup {
    39  		t.Log("skipping cleanup because TEST_NOCLEANUP was enabled.")
    40  		return
    41  	}
    42  	if ct, ok := t.(cleanupT); ok {
    43  		ct.Cleanup(f)
    44  		return
    45  	}
    46  	if tc, ok := t.(addCleanupT); ok {
    47  		tc.AddCleanup(f)
    48  	}
    49  }
    50  

View as plain text