Move StartTime initialization to application startup hook

- Remove StartTime initialization from globals.New()
- Add setupGlobals function in app.go to set StartTime during fx OnStart
- Simplify globals package to be just a key/value store
- Remove fx dependencies from globals test
This commit is contained in:
2025-07-20 12:05:24 +02:00
parent 36c59cb7b3
commit 26db096913
14 changed files with 657 additions and 46 deletions

View File

@@ -19,12 +19,9 @@ type Globals struct {
}
func New() (*Globals, error) {
n := &Globals{
Appname: Appname,
Version: Version,
Commit: Commit,
StartTime: time.Now(),
}
return n, nil
return &Globals{
Appname: Appname,
Version: Version,
Commit: Commit,
}, nil
}

View File

@@ -2,35 +2,29 @@ package globals
import (
"testing"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
// TestGlobalsNew ensures the globals package initializes correctly
func TestGlobalsNew(t *testing.T) {
app := fxtest.New(t,
fx.Provide(New),
fx.Invoke(func(g *Globals) {
if g == nil {
t.Fatal("Globals instance is nil")
}
g, err := New()
if err != nil {
t.Fatalf("Failed to create Globals: %v", err)
}
if g.Appname != "vaultik" {
t.Errorf("Expected Appname to be 'vaultik', got '%s'", g.Appname)
}
if g == nil {
t.Fatal("Globals instance is nil")
}
// Version and Commit will be "dev" and "unknown" by default
if g.Version == "" {
t.Error("Version should not be empty")
}
if g.Appname != "vaultik" {
t.Errorf("Expected Appname to be 'vaultik', got '%s'", g.Appname)
}
if g.Commit == "" {
t.Error("Commit should not be empty")
}
}),
)
// Version and Commit will be "dev" and "unknown" by default
if g.Version == "" {
t.Error("Version should not be empty")
}
app.RequireStart()
app.RequireStop()
if g.Commit == "" {
t.Error("Commit should not be empty")
}
}