forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli_test.go
59 lines (46 loc) · 1.19 KB
/
cli_test.go
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
package cli
import (
"bytes"
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func expectFileContent(t *testing.T, file, got string) {
data, err := os.ReadFile(file)
// Ignore windows line endings
data = bytes.ReplaceAll(data, []byte("\r\n"), []byte("\n"))
r := require.New(t)
r.NoError(err)
r.Equal(got, string(data))
}
func buildTestContext(t *testing.T) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
t.Cleanup(cancel)
return ctx
}
func TestTracing(t *testing.T) {
olderr := os.Stderr
oldtracing := isTracingOn
defer func() {
os.Stderr = olderr
isTracingOn = oldtracing
}()
file, err := os.CreateTemp(os.TempDir(), "cli*")
assert.NoError(t, err)
os.Stderr = file
// Note we cant really set the env since the isTracingOn
// is read at module startup so any changes mid code
// wont take effect
isTracingOn = false
tracef("something")
isTracingOn = true
tracef("foothing")
assert.NoError(t, file.Close())
b, err := os.ReadFile(file.Name())
assert.NoError(t, err)
assert.Contains(t, string(b), "foothing")
assert.NotContains(t, string(b), "something")
}