File: context_test.go

package info (click to toggle)
docker.io 20.10.5%2Bdfsg1-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 60,044 kB
  • sloc: sh: 5,527; makefile: 616; ansic: 179; python: 162; asm: 7
file content (41 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download | duplicates (6)
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
package log

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestLoggerContext(t *testing.T) {
	ctx := context.Background()
	assert.Equal(t, GetLogger(ctx), L)      // should be same as L variable
	assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same.

	ctx = WithLogger(ctx, G(ctx).WithField("test", "one"))
	assert.Equal(t, GetLogger(ctx).Data["test"], "one")
	assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same.
}

func TestModuleContext(t *testing.T) {
	ctx := context.Background()
	assert.Equal(t, GetModulePath(ctx), "")

	ctx = WithModule(ctx, "a") // basic behavior
	assert.Equal(t, GetModulePath(ctx), "a")
	logger := GetLogger(ctx)
	assert.Equal(t, logger.Data["module"], "a")

	parent, ctx := ctx, WithModule(ctx, "a")
	assert.Equal(t, ctx, parent) // should be a no-op
	assert.Equal(t, GetModulePath(ctx), "a")
	assert.Equal(t, GetLogger(ctx).Data["module"], "a")

	ctx = WithModule(ctx, "b") // new module
	assert.Equal(t, GetModulePath(ctx), "a/b")
	assert.Equal(t, GetLogger(ctx).Data["module"], "a/b")

	ctx = WithModule(ctx, "c") // new module
	assert.Equal(t, GetModulePath(ctx), "a/b/c")
	assert.Equal(t, GetLogger(ctx).Data["module"], "a/b/c")
}