github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/hud/fake_hud.go (about) 1 package hud 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/tilt-dev/tilt/internal/store" 9 "github.com/tilt-dev/tilt/pkg/logger" 10 "github.com/tilt-dev/tilt/pkg/model" 11 12 "github.com/tilt-dev/tilt/internal/hud/view" 13 ) 14 15 var _ HeadsUpDisplay = (*FakeHud)(nil) 16 17 type FakeHud struct { 18 LastView view.View 19 viewState view.ViewState 20 updates chan view.View 21 Canceled bool 22 Closed bool 23 closeCh chan interface{} 24 } 25 26 func NewFakeHud() *FakeHud { 27 return &FakeHud{ 28 updates: make(chan view.View, 10), 29 closeCh: make(chan interface{}), 30 } 31 } 32 33 func (h *FakeHud) Run(ctx context.Context, dispatch func(action store.Action), refreshInterval time.Duration) error { 34 select { 35 case <-ctx.Done(): 36 case <-h.closeCh: 37 } 38 h.Canceled = true 39 return ctx.Err() 40 } 41 42 func (h *FakeHud) OnChange(ctx context.Context, st store.RStore, _ store.ChangeSummary) error { 43 state := st.RLockState() 44 view := StateToTerminalView(state, st.StateMutex()) 45 st.RUnlockState() 46 47 err := h.update(view, h.viewState) 48 if err != nil { 49 logger.Get(ctx).Infof("Error updating HUD: %v", err) 50 } 51 52 return nil 53 } 54 55 func (h *FakeHud) update(v view.View, vs view.ViewState) error { 56 h.LastView = v 57 h.updates <- v 58 return nil 59 } 60 61 func (h *FakeHud) WaitUntilResource(t testing.TB, ctx context.Context, msg string, name model.ManifestName, isDone func(view.Resource) bool) { 62 h.WaitUntil(t, ctx, msg, func(view view.View) bool { 63 res, ok := view.Resource(name) 64 if !ok { 65 return false 66 } 67 return isDone(res) 68 }) 69 } 70 71 func (h *FakeHud) WaitUntil(t testing.TB, ctx context.Context, msg string, isDone func(view.View) bool) { 72 t.Helper() 73 ctx, cancel := context.WithTimeout(ctx, time.Second) 74 defer cancel() 75 76 for { 77 select { 78 case <-ctx.Done(): 79 t.Fatalf("Timed out waiting for: %s", msg) 80 case view := <-h.updates: 81 done := isDone(view) 82 if done { 83 return 84 } 85 } 86 } 87 }