Debian Codegraph @

cli_test.go

raw symbols SCIP
1 package cli
2
3 import (
4 "bytes"
5 "context"
6 "os"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func expectFileContent(t *testing.T, file, got string) {
15 data, err := os.ReadFile(file)
16 // Ignore windows line endings
17 data = bytes.ReplaceAll(data, []byte("\r\n"), []byte("\n"))
18
19 r := require.New(t)
20 r.NoError(err)
21 r.Equal(got, string(data))
22 }
23
24 func buildTestContext(t *testing.T) context.Context {
25 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
26 t.Cleanup(cancel)
27
28 return ctx
29 }
30
31 func TestTracing(t *testing.T) {
32 olderr := os.Stderr
33 oldtracing := isTracingOn
34 defer func() {
35 os.Stderr = olderr
36 isTracingOn = oldtracing
37 }()
38
39 file, err := os.CreateTemp(os.TempDir(), "cli*")
40 assert.NoError(t, err)
41 os.Stderr = file
42
43 // Note we can't really set the env since the isTracingOn
44 // is read at module startup so any changes mid code
45 // won't take effect
46 isTracingOn = false
47 tracef("something")
48
49 isTracingOn = true
50 tracef("foothing")
51
52 assert.NoError(t, file.Close())
53
54 b, err := os.ReadFile(file.Name())
55 assert.NoError(t, err)
56
57 assert.Contains(t, string(b), "foothing")
58 assert.NotContains(t, string(b), "something")
59 }