-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
75 lines (64 loc) · 1.8 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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func createTempConfig(t *testing.T, content string) (string, func()) {
tmpDir, err := ioutil.TempDir("", "config-test")
if err != nil {
t.Fatal("Failed to create temp dir for config test:", err)
}
tmpFile := filepath.Join(tmpDir, "testuser.yml")
err = ioutil.WriteFile(tmpFile, []byte(content), 0644)
if err != nil {
t.Fatal("Failed to create temp config file:", err)
}
return tmpFile, func() { os.RemoveAll(tmpDir) }
}
func TestConfigLoad(t *testing.T) {
// Test case: valid YAML content
validYamlContent := `
sources:
- url: https://example.com/keys
`
tmpFile, cleanup := createTempConfig(t, validYamlContent)
defer cleanup()
config := &Config{}
tmpFilePath, err := filepath.Abs(tmpFile)
if err != nil {
t.Errorf("Failed to get the absolute path of tmpFile: %v", err)
}
config.LoadConfigByPath(tmpFilePath)
expectedSources := []Source{
{URL: "https://example.com/keys"},
}
if len(config.Sources) != len(expectedSources) {
t.Errorf("Expected %d sources, got %d", len(expectedSources), len(config.Sources))
} else {
for i, expected := range expectedSources {
if config.Sources[i] != expected {
t.Errorf("Expected source %d to be %v, got %v", i, expected, config.Sources[i])
}
}
}
// Test case: invalid YAML content using a dict of sources instead of a
// list
invalidYamlContent := `
---
sources:
url: https://example.com/keys
`
tmpFile, cleanup = createTempConfig(t, invalidYamlContent)
defer cleanup()
tmpFilePath, err = filepath.Abs(tmpFile)
if err != nil {
t.Errorf("Failed to get the absolute path of tmpFile: %v", err)
}
config = &Config{}
config.LoadConfigByPath(tmpFilePath)
if len(config.Sources) != 0 {
t.Errorf("Expected 0 sources, got %d: %v", len(config.Sources), config.Sources[0])
}
}