Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/devcontainer/setup/lifecyclehooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ func validWaitForPhase(phase LifecyclePhase) bool {
return phase == PhaseInitializeCommand || slices.Contains(phaseOrder, phase)
}

// phaseHasCommands reports whether any phaseHook for the given phase
// carries actual work (either a runFunc or non-empty commands list).
func phaseHasCommands(all []phaseHook, phase LifecyclePhase) bool {
for _, ph := range all {
if ph.phase != phase {
continue
}
if ph.runFunc != nil || len(ph.params.commands) > 0 {
return true
}
}
return false
}

// resolveWaitFor normalises the raw waitFor string from the config,
// falling back to the spec default for empty or invalid values.
func resolveWaitFor(raw string) LifecyclePhase {
Expand Down Expand Up @@ -222,6 +236,14 @@ func RunPreAttachHooks(

waitFor := resolveWaitFor(setupInfo.MergedConfig.WaitFor)
waitFor = promoteDotfilesWaitFor(waitFor, dotfiles)

if !phaseHasCommands(all, waitFor) {
log.Debugf(
"waitFor phase %q has no commands configured; the split point is a no-op",
waitFor,
)
}

deferred, err := runWithWaitFor(all, waitFor)
return DeferredHooks{hooks: deferred}, err
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/devcontainer/setup/lifecyclehooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"time"

"github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/devsy-org/devsy/pkg/log"
"github.com/devsy-org/devsy/pkg/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zapcore"
)

type LifecycleHookTestSuite struct {
Expand Down Expand Up @@ -596,6 +598,51 @@ func (s *LifecycleHookTestSuite) TestPromoteDotfilesWaitForNoDotfiles() {
assert.Equal(t, DefaultWaitFor, result)
}

func (s *LifecycleHookTestSuite) TestPhaseHasCommandsTrue() {
all := []phaseHook{
{
phase: PhaseOnCreate,
params: hookRunParams{commands: []types.LifecycleHook{{"": {"echo", "hi"}}}},
},
}
assert.True(s.T(), phaseHasCommands(all, PhaseOnCreate))
}

func (s *LifecycleHookTestSuite) TestPhaseHasCommandsFalseEmpty() {
all := makeTestPhaseHooks()
assert.False(s.T(), phaseHasCommands(all, PhaseOnCreate))
}

func (s *LifecycleHookTestSuite) TestPhaseHasCommandsTrueRunFunc() {
all := []phaseHook{
{
phase: PhasePostCreate,
runFunc: func() error { return nil },
},
}
assert.True(s.T(), phaseHasCommands(all, PhasePostCreate))
}

func (s *LifecycleHookTestSuite) TestWaitForEmptyPhaseLogsWarning() {
t := s.T()
logs := log.InitTestObserved(t, zapcore.DebugLevel)

all := makeTestPhaseHooks()
phase := PhaseUpdateContent

if !phaseHasCommands(all, phase) {
log.Debugf(
"waitFor phase %q has no commands configured; the split point is a no-op",
phase,
)
}

entries := logs.All()
assert.NotEmpty(t, entries, "expected at least one log entry")
assert.Contains(t, entries[0].Message,
`waitFor phase "updateContentCommand" has no commands configured`)
}

func TestLifecycleHookTestSuite(t *testing.T) {
suite.Run(t, new(LifecycleHookTestSuite))
}
15 changes: 15 additions & 0 deletions pkg/log/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package log
import (
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"go.uber.org/zap/zaptest/observer"
)

// InitTest replaces the package-level logger with a test logger.
Expand All @@ -15,3 +18,15 @@ func InitTest(t testing.TB) {
sugar = logger.Sugar()
t.Cleanup(func() { sugar = prev })
}

// InitTestObserved replaces the package-level logger with an observable
// logger at the given level. The returned ObservedLogs can be used to
// assert that specific log messages were emitted.
func InitTestObserved(t testing.TB, level zapcore.Level) *observer.ObservedLogs {
t.Helper()
prev := sugar
core, logs := observer.New(level)
sugar = zap.New(core).Sugar()
t.Cleanup(func() { sugar = prev })
return logs
}
Loading