From 6299bea7946153d7b4fb8571a53e100d59ff406a Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 3 Apr 2026 20:22:44 +0200 Subject: [PATCH] fix(mail): guard log calls in GetMailDomain and fix hostname-dependent tests GetMailDomain called log.Warningf which panics when the logger is not initialized (e.g. in unit tests). Add log.IsInitialized() guard. Also fix TestGetThreadID tests that hardcoded "vikunja" as the expected fallback domain - on CI the os.Hostname() fallback produces a different value. Tests now dynamically compute the expected domain. --- pkg/log/logging.go | 5 +++++ pkg/mail/domain.go | 8 ++++++-- pkg/models/notifications_test.go | 13 +++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/log/logging.go b/pkg/log/logging.go index 111bfd029..0b4754189 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -27,6 +27,11 @@ import ( // logInstance is the instance of the logger which is used under the hood to log var logInstance *slog.Logger +// IsInitialized returns true if the logger has been initialized. +func IsInitialized() bool { + return logInstance != nil +} + // logpath is the path in which log files will be written. // This value is a mere fallback for other modules that could but shouldn't be used before calling ConfigureLogger var logPath = "." diff --git a/pkg/mail/domain.go b/pkg/mail/domain.go index 2bfc58c51..439e0dd7c 100644 --- a/pkg/mail/domain.go +++ b/pkg/mail/domain.go @@ -35,9 +35,13 @@ func GetMailDomain() string { } } if hostname, err := os.Hostname(); err == nil && hostname != "" { - log.Warningf("Could not determine mail domain from public URL, falling back to hostname %q", hostname) + if log.IsInitialized() { + log.Warningf("Could not determine mail domain from public URL, falling back to hostname %q", hostname) + } return hostname } - log.Warningf("Could not determine mail domain from public URL or hostname, falling back to %q", "vikunja") + if log.IsInitialized() { + log.Warningf("Could not determine mail domain from public URL or hostname, falling back to %q", "vikunja") + } return "vikunja" } diff --git a/pkg/models/notifications_test.go b/pkg/models/notifications_test.go index 526f39951..94c2aca83 100644 --- a/pkg/models/notifications_test.go +++ b/pkg/models/notifications_test.go @@ -17,6 +17,7 @@ package models import ( + "os" "testing" "code.vikunja.io/api/pkg/config" @@ -33,7 +34,11 @@ func TestGetThreadID(t *testing.T) { t.Run("default domain when no public URL", func(t *testing.T) { config.ServicePublicURL.Set("") threadID := getThreadID(123) - assert.Equal(t, "", threadID) + expectedDomain := "vikunja" + if hostname, err := os.Hostname(); err == nil && hostname != "" { + expectedDomain = hostname + } + assert.Equal(t, "", threadID) }) t.Run("simple domain without port", func(t *testing.T) { @@ -73,7 +78,11 @@ func TestGetThreadID(t *testing.T) { t.Run("invalid URL falls back to default", func(t *testing.T) { config.ServicePublicURL.Set("not a valid url") threadID := getThreadID(333) - assert.Equal(t, "", threadID) + expectedDomain := "vikunja" + if hostname, err := os.Hostname(); err == nil && hostname != "" { + expectedDomain = hostname + } + assert.Equal(t, "", threadID) }) t.Run("URL with path", func(t *testing.T) {