-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig_test.go
115 lines (108 loc) · 3.07 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
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
package main
import (
"bytes"
"encoding/json"
"os"
"path"
"strings"
"testing"
"time"
"gopkg.in/yaml.v3"
)
func TestFlagOverride(t *testing.T) {
data := []struct {
Name string
Override string
ExpectBlockTime Duration
ExpectAutoDelete bool
ExpectVerbose bool
ExpectPort uint16
}{
{"NoFlags", "", 0, false, false, 0},
{"Blocktime", "-blocktime=8h", Duration(8 * time.Hour), false, false, 0},
{"Autodelete", "-autodelete", 0, true, false, 0},
{"Verbose", "-verbose", 0, false, true, 0},
{"Port", "-port=1234", 0, false, false, 1234},
{"AllFlags", "-blocktime=16h -autodelete -verbose -port=5678", Duration(16 * time.Hour), true, true, 5678}}
for _, d := range data {
_ = setFlags(strings.Split(d.Override, " ")...)
t.Run(d.Name, func(t *testing.T) {
var cfg Config
cfg.mergeFlags(uint16(*port), Duration(*blocktime), *autodelete, *verbose)
if cfg.Settings.BlockTime != d.ExpectBlockTime {
t.Errorf("settings.blocktime: expected %v, actual %v", d.ExpectBlockTime, cfg.Settings.BlockTime)
}
if cfg.Settings.Port != d.ExpectPort {
t.Errorf("settings.port: expected %v, actual %v", d.ExpectPort, cfg.Settings.Port)
}
if cfg.Settings.AutoDelete != d.ExpectAutoDelete {
t.Errorf("settings.autodelete: expected %v, actual %v", d.ExpectAutoDelete, cfg.Settings.AutoDelete)
}
if cfg.Settings.Verbose != d.ExpectVerbose {
t.Errorf("settings.verbose: expected %v, actual %v", d.ExpectVerbose, cfg.Settings.Verbose)
}
})
}
_ = setFlags()
}
func TestReadConfig(t *testing.T) {
files, err := os.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
if !file.Type().IsRegular() || !strings.HasSuffix(file.Name(), ".yml") {
continue
}
t.Run(strings.TrimSuffix(file.Name(), ".yml"), func(t *testing.T) {
var yml struct {
In string
Out string
Err []string
}
fname := path.Join("testdata", file.Name())
data, err := os.ReadFile(fname)
if err != nil {
t.Fatal(err)
}
if err = yaml.Unmarshal(data, &yml); err != nil {
t.Fatal(err)
}
if len(yml.Err) != 0 && len(yml.Out) != 0 {
t.Fatal("One cannot have both err: and out: set")
}
cfg, err := newConfigString(yml.In, 0, Duration(0*time.Hour), false, false)
if len(yml.Out) != 0 {
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(cfg); err != nil {
t.Fatal(err)
}
if buf.String() != yml.Out {
t.Errorf("%s: does not match expected output\n---\n%s---\n%s---\n", file.Name(), buf.String(), yml.Out)
}
} else {
if err == nil {
t.Fatalf("%s: expected error, got nil", file.Name())
}
var buf bytes.Buffer
buf.WriteString(err.Error())
found := false
for _, e := range yml.Err {
if buf.String() == e {
found = true
break
}
}
if !found {
t.Errorf("%s: does not match any of the expected errors\n---\n%s---\n%s---\n", file.Name(), buf.String(), yml.Err[0])
}
}
})
}
}