summaryrefslogtreecommitdiff
path: root/internal/flags/flags_test.go
blob: b4d47d2e56c40808b76fc6b8f55fa1cf14d466f0 (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
package flags

import (
	"flag"
	"io"
	"os"
	"sync"
	"testing"
	"time"
)

func parseForTest(t *testing.T, args ...string) Flags {
	t.Helper()

	oldCommandLine := flag.CommandLine
	oldArgs := os.Args
	oldSingleton := singleton
	oldOnce := once
	oldPID := pidFilter.Load()
	oldTID := tidFilter.Load()
	oldTUIExport := tuiExportEnable.Load()

	fs := flag.NewFlagSet("ior-test", flag.ContinueOnError)
	fs.SetOutput(io.Discard)
	flag.CommandLine = fs
	os.Args = append([]string{"ior"}, args...)

	singleton = Flags{TUIExportEnable: true}
	once = sync.Once{}
	pidFilter.Store(-1)
	tidFilter.Store(-1)
	tuiExportEnable.Store(true)

	parse()
	cfg := singleton

	t.Cleanup(func() {
		flag.CommandLine = oldCommandLine
		os.Args = oldArgs
		singleton = oldSingleton
		once = oldOnce
		pidFilter.Store(oldPID)
		tidFilter.Store(oldTID)
		tuiExportEnable.Store(oldTUIExport)
	})

	return cfg
}

func TestParseLiveFlagsAndInterval(t *testing.T) {
	cfg := parseForTest(t, "-live", "-live-interval", "200ms", "-pid", "1234")

	if !cfg.LiveFlamegraph {
		t.Fatalf("expected -live to enable live mode")
	}
	if cfg.LiveInterval != 200*time.Millisecond {
		t.Fatalf("live interval = %v, want %v", cfg.LiveInterval, 200*time.Millisecond)
	}
	if cfg.PidFilter != 1234 {
		t.Fatalf("pid filter = %d, want 1234", cfg.PidFilter)
	}
	if got := int(pidFilter.Load()); got != 1234 {
		t.Fatalf("global pid filter = %d, want 1234", got)
	}
}

func TestParseLiveDefaults(t *testing.T) {
	cfg := parseForTest(t)

	if cfg.LiveFlamegraph {
		t.Fatalf("expected live mode disabled by default")
	}
	if cfg.LiveInterval != time.Second {
		t.Fatalf("default live interval = %v, want %v", cfg.LiveInterval, time.Second)
	}
}