| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package eventtest supports logging events to a test. |
| 6 | // You can use NewContext to create a context that knows how to deliver |
| 7 | // telemetry events back to the test. |
| 8 | // You must use this context or a derived one anywhere you want telemetry to be |
| 9 | // correctly routed back to the test it was constructed with. |
| 10 | // Any events delivered to a background context will be dropped. |
| 11 | // |
| 12 | // Importing this package will cause it to register a new global telemetry |
| 13 | // exporter that understands the special contexts returned by NewContext. |
| 14 | // This means you should not import this package if you are not going to call |
| 15 | // NewContext. |
| 16 | package eventtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/ |
| 17 | |
| 18 | import ( |
| 19 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 20 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 21 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 22 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 23 | |
| 24 | "golang.org/x/tools/internal/eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/" |
| 25 | "golang.org/x/tools/internal/event/corescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/core`/" |
| 26 | "golang.org/x/tools/internal/event/exportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export`/" |
| 27 | "golang.org/x/tools/internal/event/labelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/label`/" |
| 28 | ) |
| 29 | |
| 30 | func initscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/init().() { |
| 31 | e := &testExporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#{bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#buffer.: &bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#{}} |
| 32 | e.loggerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#logger. = exportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export`/.LogWriterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export`/LogWriter().(e.bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#buffer., false) |
| 33 | |
| 34 | eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/.SetExporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/SetExporter().(exportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export`/.Spansscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export`/Spans().(e.processEventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#processEvent().)) |
| 35 | } |
| 36 | |
| 37 | type testingKeyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testingKeyType# int |
| 38 | |
| 39 | const testingKeyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testingKey. = testingKeyTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testingKeyType#(0) |
| 40 | |
| 41 | // NewContext returns a context you should use for the active test. |
| 42 | func NewContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/NewContext().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context# { |
| 43 | return contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.0 context/WithValue().(ctx, testingKeyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testingKey., t) |
| 44 | } |
| 45 | |
| 46 | type testExporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter# struct { |
| 47 | muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#mu. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# |
| 48 | bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#buffer. *bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 49 | loggerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#logger. eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/.Exporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/Exporter# |
| 50 | } |
| 51 | |
| 52 | func (w *testExporterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#) processEventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#processEvent().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, ev corescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/core`/.Eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/core`/Event#, tm labelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/label`/.Mapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/label`/Map#) contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context# { |
| 53 | w.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 54 | defer w.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 55 | // build our log message in buffer |
| 56 | result := w.loggerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#logger.(ctx, ev, tm) |
| 57 | v := ctx.Valuescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Value().(testingKeyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testingKey.) |
| 58 | // get the testing.TB |
| 59 | if w.bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#buffer..Lenscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Len().() > 0 && v != nil { |
| 60 | v.(testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#).Logscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Log().(w.bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#buffer.) |
| 61 | } |
| 62 | w.bufferscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event/export/eventtest`/testExporter#buffer..Truncatescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Truncate().(0) |
| 63 | return result |
| 64 | } |