github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/testingtproxy/testing_t_proxy.go (about) 1 package testingtproxy 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 "github.com/onsi/ginkgo/internal" 9 "github.com/onsi/ginkgo/types" 10 ) 11 12 type failFunc func(message string, callerSkip ...int) 13 type skipFunc func(message string, callerSkip ...int) 14 type cleanupFunc func(args ...interface{}) 15 type reportFunc func() types.SpecReport 16 17 func New(writer io.Writer, fail failFunc, skip skipFunc, cleanup cleanupFunc, report reportFunc, offset int) *ginkgoTestingTProxy { 18 return &ginkgoTestingTProxy{ 19 fail: fail, 20 offset: offset, 21 writer: writer, 22 skip: skip, 23 cleanup: cleanup, 24 report: report, 25 } 26 } 27 28 type ginkgoTestingTProxy struct { 29 fail failFunc 30 skip skipFunc 31 cleanup cleanupFunc 32 report reportFunc 33 offset int 34 writer io.Writer 35 } 36 37 func (t *ginkgoTestingTProxy) Cleanup(f func()) { 38 t.cleanup(f, internal.Offset(1)) 39 } 40 41 func (t *ginkgoTestingTProxy) Setenv(key, value string) { 42 originalValue, exists := os.LookupEnv(key) 43 if exists { 44 t.cleanup(os.Setenv, key, originalValue, internal.Offset(1)) 45 } else { 46 t.cleanup(os.Unsetenv, key, internal.Offset(1)) 47 } 48 49 err := os.Setenv(key, value) 50 if err != nil { 51 t.fail(fmt.Sprintf("Failed to set environment variable: %v", err), 1) 52 } 53 } 54 55 func (t *ginkgoTestingTProxy) Error(args ...interface{}) { 56 t.fail(fmt.Sprintln(args...), t.offset) 57 } 58 59 func (t *ginkgoTestingTProxy) Errorf(format string, args ...interface{}) { 60 t.fail(fmt.Sprintf(format, args...), t.offset) 61 } 62 63 func (t *ginkgoTestingTProxy) Fail() { 64 t.fail("failed", t.offset) 65 } 66 67 func (t *ginkgoTestingTProxy) FailNow() { 68 t.fail("failed", t.offset) 69 } 70 71 func (t *ginkgoTestingTProxy) Failed() bool { 72 return t.report().Failed() 73 } 74 75 func (t *ginkgoTestingTProxy) Fatal(args ...interface{}) { 76 t.fail(fmt.Sprintln(args...), t.offset) 77 } 78 79 func (t *ginkgoTestingTProxy) Fatalf(format string, args ...interface{}) { 80 t.fail(fmt.Sprintf(format, args...), t.offset) 81 } 82 83 func (t *ginkgoTestingTProxy) Helper() { 84 // No-op 85 } 86 87 func (t *ginkgoTestingTProxy) Log(args ...interface{}) { 88 fmt.Fprintln(t.writer, args...) 89 } 90 91 func (t *ginkgoTestingTProxy) Logf(format string, args ...interface{}) { 92 t.Log(fmt.Sprintf(format, args...)) 93 } 94 95 func (t *ginkgoTestingTProxy) Name() string { 96 return t.report().FullText() 97 } 98 99 func (t *ginkgoTestingTProxy) Parallel() { 100 // No-op 101 } 102 103 func (t *ginkgoTestingTProxy) Skip(args ...interface{}) { 104 t.skip(fmt.Sprintln(args...), t.offset) 105 } 106 107 func (t *ginkgoTestingTProxy) SkipNow() { 108 t.skip("skip", t.offset) 109 } 110 111 func (t *ginkgoTestingTProxy) Skipf(format string, args ...interface{}) { 112 t.skip(fmt.Sprintf(format, args...), t.offset) 113 } 114 115 func (t *ginkgoTestingTProxy) Skipped() bool { 116 return t.report().State.Is(types.SpecStateSkipped) 117 } 118 119 func (t *ginkgoTestingTProxy) TempDir() string { 120 tmpDir, err := os.MkdirTemp("", "ginkgo") 121 if err != nil { 122 t.fail(fmt.Sprintf("Failed to create temporary directory: %v", err), 1) 123 return "" 124 } 125 t.cleanup(os.RemoveAll, tmpDir) 126 127 return tmpDir 128 }