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.
This commit is contained in:
kolaente 2026-04-03 20:22:44 +02:00 committed by kolaente
parent 85a350749b
commit 6299bea794
3 changed files with 22 additions and 4 deletions

View File

@ -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 = "."

View File

@ -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"
}

View File

@ -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, "<task-123@vikunja>", threadID)
expectedDomain := "vikunja"
if hostname, err := os.Hostname(); err == nil && hostname != "" {
expectedDomain = hostname
}
assert.Equal(t, "<task-123@"+expectedDomain+">", 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, "<task-333@vikunja>", threadID)
expectedDomain := "vikunja"
if hostname, err := os.Hostname(); err == nil && hostname != "" {
expectedDomain = hostname
}
assert.Equal(t, "<task-333@"+expectedDomain+">", threadID)
})
t.Run("URL with path", func(t *testing.T) {