aboutsummaryrefslogtreecommitdiff
path: root/testcapture/capture.go
blob: 3ac4f52aa10e986663e9aa2bdaab2c3dba7bfbf7 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

// Package testcapture contains [Capture], a function that executes test code in a way that captures error messages and side effects without failing the overall test.
//
// The main intended use case is testing test assertions where calls to e.g. t.Error() are an expected part of a successful test run.
package testcapture

import (
	"context"
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"
	"runtime"
	"slices"
	"sync"
	"sync/atomic"

	"go.xyrillian.de/gg/assert"
)

// Result is returned by func [Capture].
type Result struct {
	// Outcome describes how the test ended.
	Outcome Outcome
	// Panic contains a payload recovered from a panic(), if Outcome is [OutcomePanicked].
	Panic any
	// Messages contains log lines captured from t.Log() calls, or functions calling t.Log(), such as t.Error() and t.Fatal();
	// as well as data captured from t.Output().Write() calls.
	Messages []Message
	// Attrs contains attributes captured in t.Attr() calls.
	Attrs map[string]string
	// Artifacts holds the contents of any regular files that were created below t.ArtifactDir(), keyed with the path relative to t.ArtifactDir().
	Artifacts map[string]string
}

// Outcome is an enum.
// It appears in type [Result].
type Outcome string

const (
	// OutcomeFinished describes a [Capture] that ended with the test running to completion.
	OutcomeFinished Outcome = "finished"
	// OutcomeFailed describes a [Capture] that ended early because of a t.FailNow() call.
	OutcomeFailed Outcome = "failed"
	// OutcomeSkipped describes a [Capture] that ended early because of a t.SkipNow() call.
	OutcomeSkipped Outcome = "skipped"
	// OutcomePanicked describes a [Capture] that ended early because of a panic() call.
	OutcomePanicked Outcome = "panicked"
)

// Message is a piece of log output captured by func [Capture].
// It appears in type [Result].
//   - Each call to t.Log(), t.Logf() or their derived functions results in one Message instance of type [Log].
//   - Writing into t.Output() between two calls to t.Log(), t.Logf() etc. results in a single Message instance of type [Output], even if Write() is called multiple times.
type Message struct {
	Message string
	Type    MessageType
}

// Log is a shorthand for constructing [Message] objects of type [MessageTypeLog].
func Log[T interface{ ~string }](message T) Message {
	return Message{string(message), MessageTypeLog}
}

// Output is a shorthand for constructing [Message] objects of type [MessageTypeOutput].
func Output[T interface{ ~string | ~[]byte }](message T) Message {
	return Message{string(message), MessageTypeOutput}
}

// MessageType is an enum.
// It appears in type [Message].
type MessageType string

const (
	// MessageTypeLog describes [Message] instances created by calls to t.Log(), t.Logf(), or functions calling them, such as t.Error() and t.Fatal().
	MessageTypeLog MessageType = "log"
	// MessageTypeOutput describes [Message] instances created by calls to t.Output().Write().
	MessageTypeOutput MessageType = "output"
)

// Capture executes a test function with a stub implementation of [assert.TestingTB] that captures all calls to it.
// It is intended for unit-testing test assertions.
//
// The name argument is what will be reported in t.Name() within the test.
func Capture(ctx context.Context, name string, test func(assert.TestingTB)) Result {
	r := Result{
		Outcome: OutcomeFinished, // can be overridden by Fail() or SkipNow()
	}
	executeCapture(ctx, name, &r, test)
	return r
}

// capturer is the implementation of [assert.TestingTB] used by func [Capture].
type capturer struct {
	context  context.Context
	cleanups []func()
	name     string
	result   *Result
	state    struct {
		ArtifactDir string
	}

	cleanupsMutex sync.Mutex   // lock for access to the `cleanups` field
	resultMutex   sync.RWMutex // lock for access to the `result` field
	stateMutex    sync.Mutex   // lock for access to the `state` field
	nonlocalMutex sync.Mutex   // lock for non-local effects like Setenv() or filesystem operations
}

func executeCapture(ctx context.Context, name string, r *Result, test func(assert.TestingTB)) {
	ctx, cancel := context.WithCancel(ctx)
	t := capturer{
		context:  ctx,
		cleanups: nil,
		name:     name,
		result:   r,
	}
	defer func() {
		t.setOutcome(recover())
		cancel() // T.Context() demands that the context be canceled before any cleanup handlers
		for _, cleanup := range slices.Backward(t.cleanups) {
			cleanup()
		}
	}()
	test(&t)
}

func (t *capturer) setOutcome(panicPayload any) {
	t.resultMutex.Lock()
	defer t.resultMutex.Unlock()
	if panicPayload == nil {
		return
	} else if outcome, ok := panicPayload.(Outcome); ok {
		t.result.Outcome = outcome
	} else {
		t.result.Outcome = OutcomePanicked
		t.result.Panic = panicPayload
	}
}

func (t *capturer) pushOutput(buf []byte, msgType MessageType) {
	t.resultMutex.Lock()
	defer t.resultMutex.Unlock()

	// try to merge consecutive t.Output().Write() calls together
	if msgType == MessageTypeOutput && len(t.result.Messages) > 0 {
		idx := len(t.result.Messages) - 1
		msg := t.result.Messages[idx]
		if msg.Type == MessageTypeOutput {
			msg.Message = msg.Message + string(buf)
			t.result.Messages[idx] = msg
			return
		}
	}

	t.result.Messages = append(t.result.Messages, Message{
		Message: string(buf),
		Type:    msgType,
	})
}

var tempdirID atomic.Uint64

func pickTempdir() (string, error) {
	path := filepath.Join(os.TempDir(), fmt.Sprintf("gg-assert-capture-%d", tempdirID.Add(1)))
	return path, os.MkdirAll(path, 0777)
}

func collectArtifacts(dirPath string) (map[string]string, error) {
	dir, err := os.OpenRoot(dirPath)
	if err != nil {
		return nil, err
	}
	dirFS := dir.FS()

	result := make(map[string]string)
	err = fs.WalkDir(dirFS, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.Type().IsRegular() {
			buf, err := fs.ReadFile(dirFS, path)
			if err != nil {
				return err
			}
			result[path] = string(buf)
		}
		return nil
	})
	if err != nil {
		return nil, err
	}

	return result, os.RemoveAll(dirPath)
}

// ArtifactDir implements the [assert.TestingTB] interface.
func (t *capturer) ArtifactDir() string {
	t.stateMutex.Lock()
	defer t.stateMutex.Unlock()
	if t.state.ArtifactDir == "" {
		path, err := pickTempdir()
		if err != nil {
			t.Fatal("in t.ArtifactDir(): ", err)
		}
		t.state.ArtifactDir = path

		t.Cleanup(func() {
			artifacts, err := collectArtifacts(path)
			if err == nil {
				t.resultMutex.Lock()
				defer t.resultMutex.Unlock()
				t.result.Artifacts = artifacts
			} else {
				t.Error(err)
			}
		})
	}
	return t.state.ArtifactDir
}

// Attr implements the [assert.TestingTB] interface.
func (t *capturer) Attr(key, value string) {
	t.resultMutex.Lock()
	defer t.resultMutex.Unlock()
	if t.result.Attrs == nil {
		t.result.Attrs = make(map[string]string)
	}
	t.result.Attrs[key] = value
}

// Chdir implements the [assert.TestingTB] interface.
func (t *capturer) Chdir(dir string) {
	t.doChdir(dir)

	// the following is done outside of doChdir() because t.Setenv() also locks t.nonlocalMutex
	switch runtime.GOOS {
	case "windows", "plan9":
		// these platforms do not use the PWD variable
	default:
		dir, err := os.Getwd() // returns an absolute path even if `dir` is not one
		if err != nil {
			t.Fatal(err)
		}
		t.Setenv("PWD", dir)
	}
}

func (t *capturer) doChdir(dir string) {
	t.nonlocalMutex.Lock()
	defer t.nonlocalMutex.Unlock()

	oldDir, err := os.Open(".")
	if err != nil {
		t.Fatal(err)
	}
	err = os.Chdir(dir)
	if err != nil {
		t.Fatal(err)
	}

	t.Cleanup(func() {
		err := oldDir.Chdir()
		if err != nil {
			t.Error("could not reset cwd changed by t.Chdir(): ", err)
		}
	})
}

// Cleanup implements the [assert.TestingTB] interface.
func (t *capturer) Cleanup(action func()) {
	t.cleanupsMutex.Lock()
	defer t.cleanupsMutex.Unlock()
	t.cleanups = append(t.cleanups, action)
}

// Context implements the [assert.TestingTB] interface.
func (t *capturer) Context() context.Context {
	return t.context
}

// Error implements the [assert.TestingTB] interface.
func (t *capturer) Error(args ...any) {
	t.Log(args...)
	t.Fail()
}

// Errorf implements the [assert.TestingTB] interface.
func (t *capturer) Errorf(format string, args ...any) {
	t.Logf(format, args...)
	t.Fail()
}

// Fail implements the [assert.TestingTB] interface.
func (t *capturer) Fail() {
	t.resultMutex.Lock()
	defer t.resultMutex.Unlock()
	t.result.Outcome = OutcomeFailed
}

// Failed implements the [assert.TestingTB] interface.
func (t *capturer) Failed() bool {
	t.resultMutex.RLock()
	defer t.resultMutex.RUnlock()
	return t.result.Outcome == OutcomeFailed
}

// FailNow implements the [assert.TestingTB] interface.
func (t *capturer) FailNow() {
	panic(OutcomeFailed)
}

// Fatal implements the [assert.TestingTB] interface.
func (t *capturer) Fatal(args ...any) {
	t.Log(args...)
	t.FailNow()
}

// Fatalf implements the [assert.TestingTB] interface.
func (t *capturer) Fatalf(format string, args ...any) {
	t.Logf(format, args...)
	t.FailNow()
}

// Helper implements the [assert.TestingTB] interface.
func (t *capturer) Helper() {
	// no-op because we do not collect file and line information at the moment
}

// Log implements the [assert.TestingTB] interface.
func (t *capturer) Log(args ...any) {
	t.pushOutput(fmt.Append(nil, args...), MessageTypeLog)
}

// Logf implements the [assert.TestingTB] interface.
func (t *capturer) Logf(format string, args ...any) {
	t.pushOutput(fmt.Appendf(nil, format, args...), MessageTypeLog)
}

// Name implements the [assert.TestingTB] interface.
func (t *capturer) Name() string {
	return t.name
}

// Output implements the [assert.TestingTB] interface.
func (t *capturer) Output() io.Writer {
	return outputCapturer{t}
}

type outputCapturer struct {
	t *capturer
}

// Write implements the [io.Writer] interface.
func (c outputCapturer) Write(buf []byte) (int, error) {
	c.t.pushOutput(buf, MessageTypeOutput)
	return len(buf), nil
}

// Setenv implements the [assert.TestingTB] interface.
func (t *capturer) Setenv(key, value string) {
	t.nonlocalMutex.Lock()
	defer t.nonlocalMutex.Unlock()

	oldValue, hasOldValue := os.LookupEnv(key)
	os.Setenv(key, value)

	t.Cleanup(func() {
		if hasOldValue {
			os.Setenv(key, oldValue)
		} else {
			os.Unsetenv(key)
		}
	})
}

// Skip implements the [assert.TestingTB] interface.
func (t *capturer) Skip(args ...any) {
	t.Log(args...)
	t.SkipNow()
}

// Skipf implements the [assert.TestingTB] interface.
func (t *capturer) Skipf(format string, args ...any) {
	t.Logf(format, args...)
	t.SkipNow()
}

// SkipNow implements the [assert.TestingTB] interface.
func (t *capturer) SkipNow() {
	panic(OutcomeSkipped)
}

// Skipped implements the [assert.TestingTB] interface.
func (t *capturer) Skipped() bool {
	t.resultMutex.RLock()
	defer t.resultMutex.RUnlock()
	return t.result.Outcome == OutcomeSkipped
}

// TempDir implements the [assert.TestingTB] interface.
func (t *capturer) TempDir() string {
	path, err := pickTempdir()
	if err != nil {
		t.Fatal("in t.TempDir(): ", err)
	}
	t.Cleanup(func() {
		err := os.RemoveAll(path)
		if err != nil {
			t.Error(err)
		}
	})
	return path
}