-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcommon_flags_test.go
More file actions
68 lines (55 loc) · 1.53 KB
/
common_flags_test.go
File metadata and controls
68 lines (55 loc) · 1.53 KB
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
package goflags
import (
"os"
"os/signal"
"runtime"
"testing"
"time"
)
func TestAddCommonFlags(t *testing.T) {
flagSet := NewFlagSet()
commonFlags := flagSet.AddCommonFlags()
if commonFlags == nil {
t.Fatal("AddCommonFlags returned nil")
}
if commonFlags.MaxTime != 0 {
t.Errorf("Expected default MaxTime to be 0, got %v", commonFlags.MaxTime)
}
err := flagSet.Parse("-max-time", "1h30m")
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expectedDuration := 90 * time.Minute
if commonFlags.MaxTime != expectedDuration {
t.Errorf("Expected MaxTime to be %v, got %v", expectedDuration, commonFlags.MaxTime)
}
}
func TestAddCommonFlagsShortFlag(t *testing.T) {
flagSet := NewFlagSet()
commonFlags := flagSet.AddCommonFlags()
err := flagSet.Parse("-mt", "45m")
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
expectedDuration := 45 * time.Minute
if commonFlags.MaxTime != expectedDuration {
t.Errorf("Expected MaxTime to be %v, got %v", expectedDuration, commonFlags.MaxTime)
}
}
func TestMaxTimeInterrupt(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping on Windows: GenerateConsoleCtrlEvent sends to all console processes including test runner")
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
defer signal.Stop(sigChan)
flagSet := NewFlagSet()
flagSet.AddCommonFlags()
_ = flagSet.Parse("-mt", "100ms")
select {
case <-sigChan:
// Success - received interrupt
case <-time.After(500 * time.Millisecond):
t.Error("Expected interrupt signal within 500ms")
}
}