-
Notifications
You must be signed in to change notification settings - Fork 303
/
context.go
52 lines (41 loc) · 1.71 KB
/
context.go
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
package testutils
import (
"context"
"fmt"
"io"
"os"
"testing"
"github.com/tilt-dev/wmclient/pkg/analytics"
tiltanalytics "github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/pkg/logger"
)
// CtxAndAnalyticsForTest returns a context.Context suitable for use in tests (i.e. with
// logger & analytics attached), and the analytics it contains.
func CtxAndAnalyticsForTest() (context.Context, *analytics.MemoryAnalytics, *tiltanalytics.TiltAnalytics) {
l := logger.NewLogger(logger.DebugLvl, os.Stdout)
ctx := logger.WithLogger(context.Background(), l)
opter := tiltanalytics.NewFakeOpter(analytics.OptIn)
ma, ta := tiltanalytics.NewMemoryTiltAnalyticsForTest(opter)
ctx = tiltanalytics.WithAnalytics(ctx, ta)
return ctx, ma, ta
}
func ForkedCtxAndAnalyticsWithOpterForTest(w io.Writer, o tiltanalytics.AnalyticsOpter) (context.Context, *analytics.MemoryAnalytics, *tiltanalytics.TiltAnalytics) {
l := logger.NewLogger(logger.DebugLvl, os.Stdout)
ctx := logger.WithLogger(context.Background(), l)
ctx = logger.CtxWithForkedOutput(ctx, w)
ma, ta := tiltanalytics.NewMemoryTiltAnalyticsForTest(o)
ctx = tiltanalytics.WithAnalytics(ctx, ta)
return ctx, ma, ta
}
// CtxForTest returns a context.Context suitable for use in tests (i.e. with
// logger attached), and with all output being copied to `w`
func ForkedCtxAndAnalyticsForTest(w io.Writer) (context.Context, *analytics.MemoryAnalytics, *tiltanalytics.TiltAnalytics) {
opter := tiltanalytics.NewFakeOpter(analytics.OptIn)
return ForkedCtxAndAnalyticsWithOpterForTest(w, opter)
}
func FailOnNonCanceledErr(t *testing.T, err error, message string) {
if err != nil && err != context.Canceled {
fmt.Printf("%s: %v\n", message, err)
t.Error(err)
}
}