From 220ae47d5cfd32dc58a0aefbe2e818df570654b8 Mon Sep 17 00:00:00 2001 From: Elijah Duffy Date: Wed, 4 Jun 2025 14:24:19 -0700 Subject: [PATCH] add Module.Lifecycle getter method --- module.go | 6 ++++++ module_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/module.go b/module.go index 53cd8f1..a6e2323 100644 --- a/module.go +++ b/module.go @@ -53,6 +53,12 @@ func NewModule(name string, opts ModuleOpts) *Module { } } +// Lifecycle returns the lifecycle this module belongs to. If the module is not +// part of a lifecycle, it will return nil. +func (s *Module) Lifecycle() *Lifecycle { + return s.lifecycle +} + // Logger returns the logger for the module. Uses the lifecycle's logger unless // a specific logger has been set during module load. func (s *Module) Logger() *slog.Logger { diff --git a/module_test.go b/module_test.go index 860bbbc..ea4675a 100644 --- a/module_test.go +++ b/module_test.go @@ -64,3 +64,16 @@ func TestModule(t *testing.T) { mod.loaded = true assert.NotPanics(func() { mod.RequireLoaded() }, "expected RequireLoaded to not panic when module is loaded") } + +func TestModule_Lifecycle(t *testing.T) { + assert := assert.New(t) + + // Create a module and check its lifecycle + mod := NewModule("testModule", ModuleOpts{}) + assert.Nil(mod.Lifecycle(), "expected Lifecycle to return nil when module is not part of a lifecycle") + + // Create a lifecycle and set it to the module + lifecycle := &Lifecycle{} + mod.lifecycle = lifecycle + assert.Equal(lifecycle, mod.Lifecycle(), "expected Lifecycle to return the correct lifecycle") +}