-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
147 lines (126 loc) · 3.84 KB
/
main_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"flag"
"os"
"testing"
"time"
"github.com/calpa/urusai/config"
)
// TestFlagParsing tests the command line flag parsing functionality
func TestFlagParsing(t *testing.T) {
// Save original command line arguments and flags
origArgs := os.Args
origFlagCommandLine := flag.CommandLine
// Restore the original state after the test
defer func() {
os.Args = origArgs
flag.CommandLine = origFlagCommandLine
}()
// Test cases
testCases := []struct {
name string
args []string
expectConfigFile string
expectLogLevel string
expectTimeout int
}{
{
name: "Default values",
args: []string{"urusai"},
expectConfigFile: "",
expectLogLevel: "info",
expectTimeout: 0,
},
{
name: "Custom config file",
args: []string{"urusai", "--config", "custom.json"},
expectConfigFile: "custom.json",
expectLogLevel: "info",
expectTimeout: 0,
},
{
name: "Custom log level",
args: []string{"urusai", "--log", "debug"},
expectConfigFile: "",
expectLogLevel: "debug",
expectTimeout: 0,
},
{
name: "Custom timeout",
args: []string{"urusai", "--timeout", "300"},
expectConfigFile: "",
expectLogLevel: "info",
expectTimeout: 300,
},
{
name: "All custom values",
args: []string{"urusai", "--config", "custom.json", "--log", "error", "--timeout", "600"},
expectConfigFile: "custom.json",
expectLogLevel: "error",
expectTimeout: 600,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Reset flag.CommandLine for each test case
flag.CommandLine = flag.NewFlagSet(tc.args[0], flag.ExitOnError)
// Set up command line arguments
os.Args = tc.args
// Declare the flags again (as in main.go)
var configFile string
var logLevel string
var timeout int
flag.StringVar(&configFile, "config", "", "path to config file")
flag.StringVar(&logLevel, "log", "info", "logging level (debug, info, warn, error)")
flag.IntVar(&timeout, "timeout", 0, "for how long the crawler should be running, in seconds (0 means no timeout)")
flag.Parse()
// Check if the flags were parsed correctly
if configFile != tc.expectConfigFile {
t.Errorf("Expected config file %q, got %q", tc.expectConfigFile, configFile)
}
if logLevel != tc.expectLogLevel {
t.Errorf("Expected log level %q, got %q", tc.expectLogLevel, logLevel)
}
if timeout != tc.expectTimeout {
t.Errorf("Expected timeout %d, got %d", tc.expectTimeout, timeout)
}
})
}
}
// TestLoadDefaultConfig tests that the default configuration can be loaded
func TestLoadDefaultConfig(t *testing.T) {
cfg, err := config.LoadDefaultConfig()
if err != nil {
t.Fatalf("Failed to load default config: %v", err)
}
// Verify that the default config has valid values
if cfg.MaxDepth <= 0 {
t.Errorf("Expected MaxDepth > 0, got %d", cfg.MaxDepth)
}
if len(cfg.RootURLs) == 0 {
t.Error("Expected RootURLs to have at least one URL")
}
}
// TestSetLogLevel tests the log level setting functionality
func TestSetLogLevel(t *testing.T) {
// Test with valid log levels
validLevels := []string{"debug", "info", "warn", "error"}
for _, level := range validLevels {
// This should not panic
setLogLevel(level)
}
// Test with invalid log level (should default to info)
setLogLevel("invalid")
}
// TestSignalHandling tests the signal handling setup
// Note: This is a basic test that just ensures the function doesn't crash
func TestSignalHandling(t *testing.T) {
sigChan := make(chan os.Signal, 1)
// Start a goroutine that will send a signal after a short delay
go func() {
time.Sleep(10 * time.Millisecond)
sigChan <- os.Interrupt
}()
// This should return almost immediately due to the signal
<-sigChan
}