go.undefinedlabs.com/scopeagent@v0.4.2/instrumentation/testing/tb.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"runtime"
     7  
     8  	"github.com/opentracing/opentracing-go/log"
     9  
    10  	"go.undefinedlabs.com/scopeagent/errors"
    11  	"go.undefinedlabs.com/scopeagent/instrumentation"
    12  	"go.undefinedlabs.com/scopeagent/reflection"
    13  	"go.undefinedlabs.com/scopeagent/tags"
    14  )
    15  
    16  // ***************************
    17  // TB interface implementation
    18  func (test *Test) private() {}
    19  
    20  func (test *Test) Error(args ...interface{}) {
    21  	test.t.Helper()
    22  	if test.span != nil {
    23  		test.span.LogFields(
    24  			log.String(tags.EventType, tags.LogEvent),
    25  			log.String(tags.EventMessage, fmt.Sprint(args...)),
    26  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
    27  			log.String(tags.LogEventLevel, tags.LogLevel_ERROR),
    28  			log.String("log.internal_level", "Error"),
    29  			log.String("log.logger", "testing"),
    30  		)
    31  	}
    32  	test.t.Error(args...)
    33  }
    34  
    35  func (test *Test) Errorf(format string, args ...interface{}) {
    36  	test.t.Helper()
    37  	if test.span != nil {
    38  		test.span.LogFields(
    39  			log.String(tags.EventType, tags.LogEvent),
    40  			log.String(tags.EventMessage, fmt.Sprintf(format, args...)),
    41  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
    42  			log.String(tags.LogEventLevel, tags.LogLevel_ERROR),
    43  			log.String("log.internal_level", "Error"),
    44  			log.String("log.logger", "testing"),
    45  		)
    46  	}
    47  	test.t.Errorf(format, args...)
    48  }
    49  
    50  func (test *Test) Fail() {
    51  	test.t.Helper()
    52  	test.t.Fail()
    53  }
    54  
    55  func (test *Test) FailNow() {
    56  	test.t.Helper()
    57  	test.t.FailNow()
    58  }
    59  
    60  func (test *Test) Failed() bool {
    61  	test.t.Helper()
    62  	return test.t.Failed()
    63  }
    64  
    65  func (test *Test) Fatal(args ...interface{}) {
    66  	test.t.Helper()
    67  	if test.span != nil {
    68  		test.span.LogFields(
    69  			log.String(tags.EventType, tags.EventTestFailure),
    70  			log.String(tags.EventMessage, fmt.Sprint(args...)),
    71  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
    72  			log.String("log.internal_level", "Fatal"),
    73  			log.String("log.logger", "testing"),
    74  		)
    75  	}
    76  	test.t.Fatal(args...)
    77  }
    78  
    79  func (test *Test) Fatalf(format string, args ...interface{}) {
    80  	test.t.Helper()
    81  	if test.span != nil {
    82  		test.span.LogFields(
    83  			log.String(tags.EventType, tags.EventTestFailure),
    84  			log.String(tags.EventMessage, fmt.Sprintf(format, args...)),
    85  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
    86  			log.String("log.internal_level", "Fatal"),
    87  			log.String("log.logger", "testing"),
    88  		)
    89  	}
    90  	test.t.Fatalf(format, args...)
    91  }
    92  
    93  func (test *Test) Log(args ...interface{}) {
    94  	test.t.Helper()
    95  	if test.span != nil {
    96  		test.span.LogFields(
    97  			log.String(tags.EventType, tags.LogEvent),
    98  			log.String(tags.EventMessage, fmt.Sprint(args...)),
    99  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
   100  			log.String(tags.LogEventLevel, tags.LogLevel_INFO),
   101  			log.String("log.internal_level", "Log"),
   102  			log.String("log.logger", "testing"),
   103  		)
   104  	}
   105  	test.t.Log(args...)
   106  }
   107  
   108  func (test *Test) Logf(format string, args ...interface{}) {
   109  	test.t.Helper()
   110  	if test.span != nil {
   111  		test.span.LogFields(
   112  			log.String(tags.EventType, tags.LogEvent),
   113  			log.String(tags.EventMessage, fmt.Sprintf(format, args...)),
   114  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
   115  			log.String(tags.LogEventLevel, tags.LogLevel_INFO),
   116  			log.String("log.internal_level", "Log"),
   117  			log.String("log.logger", "testing"),
   118  		)
   119  	}
   120  	test.t.Logf(format, args...)
   121  }
   122  
   123  func (test *Test) Name() string {
   124  	return test.t.Name()
   125  }
   126  
   127  func (test *Test) Skip(args ...interface{}) {
   128  	test.t.Helper()
   129  	if test.span != nil {
   130  		test.span.LogFields(
   131  			log.String(tags.EventType, tags.EventTestSkip),
   132  			log.String(tags.EventMessage, fmt.Sprint(args...)),
   133  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
   134  			log.String("log.internal_level", "Skip"),
   135  			log.String("log.logger", "testing"),
   136  		)
   137  	}
   138  	test.t.Skip(args...)
   139  }
   140  
   141  func (test *Test) SkipNow() {
   142  	test.t.Helper()
   143  	test.t.SkipNow()
   144  }
   145  
   146  func (test *Test) Skipf(format string, args ...interface{}) {
   147  	test.t.Helper()
   148  	if test.span != nil {
   149  		test.span.LogFields(
   150  			log.String(tags.EventType, tags.EventTestSkip),
   151  			log.String(tags.EventMessage, fmt.Sprintf(format, args...)),
   152  			log.String(tags.EventSource, getSourceFileAndNumber(1)),
   153  			log.String("log.internal_level", "Skip"),
   154  			log.String("log.logger", "testing"),
   155  		)
   156  	}
   157  	test.t.Skipf(format, args...)
   158  }
   159  
   160  func (test *Test) Skipped() bool {
   161  	return test.t.Skipped()
   162  }
   163  
   164  // Deprecated: use `testing.T.Helper` instead
   165  func (test *Test) Helper() {
   166  	pc, _, _, _ := runtime.Caller(1)
   167  	reflection.AddToHelpersMap(test.t, []string{
   168  		runtime.FuncForPC(pc).Name(),
   169  	})
   170  	test.t.Helper()
   171  }
   172  
   173  // Log panic data with stacktrace
   174  func (test *Test) LogPanic(recoverData interface{}, skipFrames int) {
   175  	errors.LogPanic(test.ctx, recoverData, skipFrames+1)
   176  }
   177  
   178  func getSourceFileAndNumber(skip int) string {
   179  	var source string
   180  	if _, file, line, ok := instrumentation.GetCallerInsideSourceRoot(1 + skip); ok == true {
   181  		file = filepath.Clean(file)
   182  		source = fmt.Sprintf("%s:%d", file, line)
   183  	}
   184  	return source
   185  }