File: log_test.go

package info (click to toggle)
golang-github-bifurcation-mint 0.0~git20200214.93c820e-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 632 kB
  • sloc: makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,446 bytes parent folder | download
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
package mint

import (
	"fmt"
	"testing"
)

var logLine = ""

func testLogFunction(format string, v ...interface{}) {
	logLine = fmt.Sprintf(format, v...)
}

func TestLogging(t *testing.T) {
	originalLogFunction := logFunction
	originalLogAll := logAll
	originalLogSettings := logSettings

	logAll = false
	logSettings = map[string]bool{}
	env := []string{"MINT_LOG=*"}
	parseLogEnv(env)
	assertTrue(t, logAll, "Failed to parse wildcard log directive")
	assertTrue(t, len(logSettings) == 0, "Mistakenly set log settings")

	logAll = false
	logSettings = map[string]bool{}
	env = []string{"MINT_LOG=foo,bar"}
	parseLogEnv(env)
	assertTrue(t, !logAll, "Mistakenly set logAll")
	assertTrue(t, logSettings["foo"] && logSettings["bar"], "Failed to parse string log directive")

	logFunction = testLogFunction
	logAll = false
	logSettings = map[string]bool{"foo": true}

	// Test that we print matching lines
	logLine = ""
	logf("foo", "This is an integer: %d", 1)
	assertEquals(t, logLine, "[foo] This is an integer: 1")

	// Test that we ignore non-matching lines
	logLine = ""
	logf("bar", "This is an integer: %d", 1)
	assertEquals(t, logLine, "")

	// Test that logAll enables all
	logAll = true
	logLine = ""
	logf("bar", "This is an integer: %d", 1)
	assertEquals(t, logLine, "[bar] This is an integer: 1")

	// Restore original values for globals
	logFunction = originalLogFunction
	logAll = originalLogAll
	logSettings = originalLogSettings
}