File: eventtest.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (64 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package eventtest supports logging events to a test.
// You can use NewContext to create a context that knows how to deliver
// telemetry events back to the test.
// You must use this context or a derived one anywhere you want telemetry to be
// correctly routed back to the test it was constructed with.
// Any events delivered to a background context will be dropped.
//
// Importing this package will cause it to register a new global telemetry
// exporter that understands the special contexts returned by NewContext.
// This means you should not import this package if you are not going to call
// NewContext.
package eventtest

import (
	"bytes"
	"context"
	"sync"
	"testing"

	"golang.org/x/tools/internal/event"
	"golang.org/x/tools/internal/event/core"
	"golang.org/x/tools/internal/event/export"
	"golang.org/x/tools/internal/event/label"
)

func init() {
	e := &testExporter{buffer: &bytes.Buffer{}}
	e.logger = export.LogWriter(e.buffer, false)

	event.SetExporter(export.Spans(e.processEvent))
}

type testingKeyType int

const testingKey = testingKeyType(0)

// NewContext returns a context you should use for the active test.
func NewContext(ctx context.Context, t testing.TB) context.Context {
	return context.WithValue(ctx, testingKey, t)
}

type testExporter struct {
	mu     sync.Mutex
	buffer *bytes.Buffer
	logger event.Exporter
}

func (w *testExporter) processEvent(ctx context.Context, ev core.Event, tm label.Map) context.Context {
	w.mu.Lock()
	defer w.mu.Unlock()
	// build our log message in buffer
	result := w.logger(ctx, ev, tm)
	v := ctx.Value(testingKey)
	// get the testing.TB
	if w.buffer.Len() > 0 && v != nil {
		v.(testing.TB).Log(w.buffer)
	}
	w.buffer.Truncate(0)
	return result
}