summaryrefslogtreecommitdiff
path: root/testcapture/capture_test.go
blob: 9e53b71a831f6e8b6ad0924e1d55f18d0626d6cf (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
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package testcapture_test

import (
	"context"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"

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

func TestCaptureArtifactDir(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		// test that writing a regular file works
		fooPath := filepath.Join(t.ArtifactDir(), "foo.txt")
		err := os.WriteFile(fooPath, []byte("Hello World."), 0666)
		if err != nil {
			t.Fatal(err)
		}

		// test that nesting regular files into directories works
		// (and also, implicitly, that multiple calls to ArtifactDir() return the same path)
		barPath := filepath.Join(t.ArtifactDir(), "bar/a/b/c/d/data.json")
		err = os.MkdirAll(filepath.Dir(barPath), 0777)
		if err != nil {
			t.Fatal(err)
		}
		err = os.WriteFile(barPath, []byte(`{"bar":42}`), 0666)
		if err != nil {
			t.Fatal(err)
		}

		// test that empty directories are ignored by the capture
		err = os.MkdirAll(filepath.Join(t.ArtifactDir(), "unused"), 0777)
		if err != nil {
			t.Fatal(err)
		}
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFinished,
		Artifacts: map[string]string{
			"foo.txt":               "Hello World.",
			"bar/a/b/c/d/data.json": `{"bar":42}`,
		},
	})
}

func TestCaptureAttr(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		t.Attr("foo", "bar")
		t.Attr("foo", "baz")
		t.Attr("hello", "world")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFinished,
		Attrs: map[string]string{
			"foo":   "baz",
			"hello": "world",
		},
	})
}

func TestCaptureChdir(t *testing.T) {
	var cwdInCapture string
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		// ArtifactDir() is used as a target for Chdir()...
		dir := t.ArtifactDir()
		t.Chdir(dir)
		// ...because we have an easy way to check if we are actually in that dir
		err := os.WriteFile("foo.txt", []byte("foo"), 0666)
		if err != nil {
			t.Error(err)
		}

		// smuggle the cwd out of the capture for the cleanup test below
		cwdInCapture, err = os.Getwd()
		if err != nil {
			t.Error(err)
		}

		// t.Chdir() should have set $PWD to an absolute path
		assert.Equal(t, os.Getenv("PWD"), cwdInCapture)
		assert.Equal(t, filepath.IsAbs(cwdInCapture), true)
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome:   testcapture.OutcomeFinished,
		Artifacts: map[string]string{"foo.txt": "foo"},
	})

	// check that we reset the working directory at the end of the test
	cwdAfterCapture, err := os.Getwd()
	if err != nil {
		t.Error(err)
	}
	if cwdAfterCapture == cwdInCapture {
		t.Error("cwd should have been reset, but still is", cwdInCapture)
	}
}

func TestCaptureCleanup(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		// test that cleanups run in reverse order of registration, and only after the test itself is done
		t.Log("starting up")
		t.Cleanup(func() { t.Log("first cleanup") })
		t.Cleanup(func() { t.Log("second cleanup") })
		t.Cleanup(func() { t.Log("third cleanup") })
		t.Log("shutting down")

		// test that cleanups run even after a panic
		panic("kaboom")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomePanicked,
		Messages: []testcapture.Message{
			testcapture.Log("starting up"),
			testcapture.Log("shutting down"),
			testcapture.Log("third cleanup"),
			testcapture.Log("second cleanup"),
			testcapture.Log("first cleanup"),
		},
		Panic: "kaboom",
	})
}

func TestCaptureContext(t *testing.T) {
	ctx := context.WithValue(t.Context(), "foo", "bar") //nolint:staticcheck // we do not care about type collision risks for this simple test
	result := testcapture.Capture(ctx, t.Name(), func(t assert.TestingTB) {
		// test that the context is live within the test
		err := t.Context().Err()
		if err != nil {
			t.Error(err)
		}

		// test that the context is expired at cleanup time
		t.Cleanup(func() {
			err := t.Context().Err()
			if err == nil {
				t.Error("still alive!?")
			}
		})

		// test that the context is derived from the one passed to Capture()
		value := ctx.Value("foo")
		if value != "bar" {
			t.Error("did not see the value")
		}
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFinished,
	})
}

func TestCaptureError(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		simpleError := errors.New("bar")
		t.Error("foo: ", simpleError)
		// check that the test keeps going, and that t.Log() does not reset the Outcome
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFailed,
		Messages: []testcapture.Message{
			testcapture.Log("foo: bar"),
			testcapture.Log("still going"),
		},
	})
}

func TestCaptureErrorf(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		t.Errorf("foo = %d", 42)
		// check that the test keeps going, and that t.Log() does not reset the Outcome
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFailed,
		Messages: []testcapture.Message{
			testcapture.Log("foo = 42"),
			testcapture.Log("still going"),
		},
	})
}

func TestCaptureFail(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		if !t.Failed() {
			t.Log("looking good so far")
		}
		t.Fail()
		if t.Failed() {
			t.Log("still going")
		}
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFailed,
		Messages: []testcapture.Message{
			testcapture.Log("looking good so far"),
			testcapture.Log("still going"),
		},
	})
}

func TestCaptureFailNow(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		if !t.Failed() {
			t.Log("looking good so far")
		}
		t.FailNow()
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFailed,
		Messages: []testcapture.Message{
			testcapture.Log("looking good so far"),
			// "still going" is not logged because FailNow() bails
		},
	})
}

func TestCaptureFatal(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		t.Log("looking good so far")
		t.Fatal("kaboom")
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFailed,
		Messages: []testcapture.Message{
			testcapture.Log("looking good so far"),
			testcapture.Log("kaboom"),
			// "still going" is not logged because Fatal() bails
		},
	})
}

func TestCaptureFatalf(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		t.Log("looking good so far")
		t.Fatalf("kaboom %d", 42)
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFailed,
		Messages: []testcapture.Message{
			testcapture.Log("looking good so far"),
			testcapture.Log("kaboom 42"),
			// "still going" is not logged because Fatalf() bails
		},
	})
}

func TestCaptureName(t *testing.T) {
	result := testcapture.Capture(t.Context(), "Harold", func(t assert.TestingTB) {
		panic(t.Name() + " died")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomePanicked,
		Panic:   "Harold died",
	})
}

func TestCaptureOutput(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		t.Log("hello 1")
		for range 10 {
			fmt.Fprintln(t.Output(), "a")
		}
		t.Log("hello 2")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFinished,
		Messages: []testcapture.Message{
			testcapture.Log("hello 1"),
			testcapture.Output(strings.Repeat("a\n", 10)),
			testcapture.Log("hello 2"),
		},
	})
}

func TestCapturePanic(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		t.Error("we are going to blow up")
		panic("kaboom")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomePanicked, // Panicked takes precedence over Failed
		Messages: []testcapture.Message{
			testcapture.Log("we are going to blow up"),
		},
		Panic: "kaboom",
	})
}

func TestCaptureSetenv(t *testing.T) {
	t.Setenv("GG_TEST_SETENV_SCOPE", "outer")

	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		// test t.Setenv() overriding an existing variable
		assert.Equal(t, os.Getenv("GG_TEST_SETENV_SCOPE"), "outer")
		t.Setenv("GG_TEST_SETENV_SCOPE", "inner")
		assert.Equal(t, os.Getenv("GG_TEST_SETENV_SCOPE"), "inner")

		// test t.Setenv() setting a fresh variable
		// (this variable should be completely removed after the end of the test)
		t.Setenv("GG_TEST_SETENV_PAYLOAD", "42")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFinished,
	})

	assert.Equal(t, os.Getenv("GG_TEST_SETENV_SCOPE"), "outer")
	_, ok := os.LookupEnv("GG_TEST_SETENV_PAYLOAD")
	assert.Equal(t, ok, false)
}

func TestCaptureSkip(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		if !t.Skipped() {
			t.Skip("this looks uninteresting")
		}
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeSkipped,
		Messages: []testcapture.Message{
			testcapture.Log("this looks uninteresting"),
			// "still going" is not logged because Skip() bails
		},
	})
}

func TestCaptureSkipf(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		if !t.Skipped() {
			t.Skipf("pretty sure the answer is %d", 42)
		}
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeSkipped,
		Messages: []testcapture.Message{
			testcapture.Log("pretty sure the answer is 42"),
			// "still going" is not logged because Skipf() bails
		},
	})
}

func TestCaptureSkipNow(t *testing.T) {
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		if !t.Skipped() {
			t.Log("this looks uninteresting")
		}
		t.SkipNow()
		t.Log("still going")
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeSkipped,
		Messages: []testcapture.Message{
			testcapture.Log("this looks uninteresting"),
			// "still going" is not logged because Skip() bails
		},
	})
}

func TestCaptureTempDir(t *testing.T) {
	var path string
	result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
		// fill a TempDir with some stuff
		path = t.TempDir()
		err := os.MkdirAll(filepath.Join(path, "emptydir"), 0777)
		if err != nil {
			t.Error(err)
		}
		err = os.WriteFile(filepath.Join(path, "data.json"), []byte(`{"username":"admin"}`), 0666)
		if err != nil {
			t.Error(err)
		}

		// check that each call to TempDir returns a new dir
		otherPath := t.TempDir()
		if otherPath == path {
			t.Error("should have returned a different TempDir")
		}
	})
	assert.Equal(t, result, testcapture.Result{
		Outcome: testcapture.OutcomeFinished,
	})

	// check that TempDir was cleaned up
	_, err := os.Stat(path)
	if err == nil {
		t.Error("TempDir was not cleaned up")
	} else if !os.IsNotExist(err) {
		t.Error(err)
	}
}