-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
89 lines (77 loc) · 2.15 KB
/
config_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
package main
import (
"os"
"testing"
)
func TestLoadConfig(t *testing.T) {
// Create a temporary config file
tempFile, err := os.CreateTemp("", "test_config")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tempFile.Name())
// Write test config
testConfig := `USERNAME=testuser
PASSWORD=testpass
LISTEN_HOST=127.0.0.1
LISTEN_PORT=8080
`
if _, err := tempFile.Write([]byte(testConfig)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
tempFile.Close()
// Set the configPath to our temp file
oldConfigPath := configPath
configPath = tempFile.Name()
defer func() { configPath = oldConfigPath }()
// Test loadConfig
config := loadConfig()
if config.Username != "testuser" {
t.Errorf("Expected Username 'testuser', got '%s'", config.Username)
}
if config.Password != "testpass" {
t.Errorf("Expected Password 'testpass', got '%s'", config.Password)
}
if config.ListenHost != "127.0.0.1" {
t.Errorf("Expected ListenHost '127.0.0.1', got '%s'", config.ListenHost)
}
if config.ListenPort != "8080" {
t.Errorf("Expected ListenPort '8080', got '%s'", config.ListenPort)
}
}
func TestSaveConfig(t *testing.T) {
// Create a temporary file for the config
tempFile, err := os.CreateTemp("", "test_config")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tempFile.Name())
tempFile.Close()
// Set the configPath to our temp file
oldConfigPath := configPath
configPath = tempFile.Name()
defer func() { configPath = oldConfigPath }()
// Test saveConfig
testConfig := Config{
Username: "testuser",
Password: "testpass",
ListenHost: "127.0.0.1",
ListenPort: "8080",
}
if err := saveConfig(testConfig); err != nil {
t.Fatalf("Failed to save config: %v", err)
}
// Read the saved config
data, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("Failed to read saved config: %v", err)
}
expectedContent := `USERNAME=testuser
PASSWORD=testpass
LISTEN_HOST=127.0.0.1
LISTEN_PORT=8080
`
if string(data) != expectedContent {
t.Errorf("Saved config does not match expected content.\nExpected:\n%s\nGot:\n%s", expectedContent, string(data))
}
}