mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
config: Tidy default handling and comparisons in tests
This patch tidies how defaults are handled in the config, using a new logic to allow "overriding" one config (the default) with another (the user supplied). It also improves how the comparisons are done in the tests, using the more convenient "github.com/google/go-cmp/cmp" package, which also prints nice diffs on errors. This is in preparation for a future path where the override mechanism will be reused.
This commit is contained in:
@@ -8,6 +8,9 @@ import (
|
||||
|
||||
"blitiri.com.ar/go/chasquid/internal/testlib"
|
||||
"blitiri.com.ar/go/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
)
|
||||
|
||||
func mustCreateConfig(t *testing.T, contents string) (string, string) {
|
||||
@@ -30,26 +33,13 @@ func TestEmptyConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test the default values are set.
|
||||
|
||||
defaults := proto.Clone(defaultConfig).(*Config)
|
||||
hostname, _ := os.Hostname()
|
||||
if c.Hostname == "" || c.Hostname != hostname {
|
||||
t.Errorf("invalid hostname %q, should be: %q", c.Hostname, hostname)
|
||||
}
|
||||
defaults.Hostname = hostname
|
||||
|
||||
if c.MaxDataSizeMb != 50 {
|
||||
t.Errorf("max data size != 50: %d", c.MaxDataSizeMb)
|
||||
}
|
||||
|
||||
if len(c.SmtpAddress) != 1 || c.SmtpAddress[0] != "systemd" {
|
||||
t.Errorf("unexpected address default: %v", c.SmtpAddress)
|
||||
}
|
||||
|
||||
if len(c.SubmissionAddress) != 1 || c.SubmissionAddress[0] != "systemd" {
|
||||
t.Errorf("unexpected address default: %v", c.SubmissionAddress)
|
||||
}
|
||||
|
||||
if c.MonitoringAddress != "" {
|
||||
t.Errorf("monitoring address is set: %v", c.MonitoringAddress)
|
||||
diff := cmp.Diff(defaults, c, protocmp.Transform())
|
||||
if diff != "" {
|
||||
t.Errorf("Load() mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
testLogConfig(c)
|
||||
@@ -60,6 +50,8 @@ func TestFullConfig(t *testing.T) {
|
||||
hostname: "joust"
|
||||
smtp_address: ":1234"
|
||||
smtp_address: ":5678"
|
||||
submission_address: ":10001"
|
||||
submission_address: ":10002"
|
||||
monitoring_address: ":1111"
|
||||
max_data_size_mb: 26
|
||||
`
|
||||
@@ -67,26 +59,34 @@ func TestFullConfig(t *testing.T) {
|
||||
tmpDir, path := mustCreateConfig(t, confStr)
|
||||
defer testlib.RemoveIfOk(t, tmpDir)
|
||||
|
||||
expected := &Config{
|
||||
Hostname: "joust",
|
||||
MaxDataSizeMb: 26,
|
||||
|
||||
SmtpAddress: []string{":1234", ":5678"},
|
||||
SubmissionAddress: []string{":10001", ":10002"},
|
||||
SubmissionOverTlsAddress: []string{"systemd"},
|
||||
MonitoringAddress: ":1111",
|
||||
|
||||
MailDeliveryAgentBin: "maildrop",
|
||||
MailDeliveryAgentArgs: []string{"-f", "%from%", "-d", "%to_user%"},
|
||||
|
||||
DataDir: "/var/lib/chasquid",
|
||||
|
||||
SuffixSeparators: "+",
|
||||
DropCharacters: ".",
|
||||
|
||||
MailLogPath: "<syslog>",
|
||||
}
|
||||
|
||||
c, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("error loading non-existent config: %v", err)
|
||||
}
|
||||
|
||||
if c.Hostname != "joust" {
|
||||
t.Errorf("hostname %q != 'joust'", c.Hostname)
|
||||
}
|
||||
|
||||
if c.MaxDataSizeMb != 26 {
|
||||
t.Errorf("max data size != 26: %d", c.MaxDataSizeMb)
|
||||
}
|
||||
|
||||
if len(c.SmtpAddress) != 2 ||
|
||||
c.SmtpAddress[0] != ":1234" || c.SmtpAddress[1] != ":5678" {
|
||||
t.Errorf("different address: %v", c.SmtpAddress)
|
||||
}
|
||||
|
||||
if c.MonitoringAddress != ":1111" {
|
||||
t.Errorf("monitoring address %q != ':1111;", c.MonitoringAddress)
|
||||
diff := cmp.Diff(expected, c, protocmp.Transform())
|
||||
if diff != "" {
|
||||
t.Errorf("Load() mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
|
||||
testLogConfig(c)
|
||||
|
||||
Reference in New Issue
Block a user