github.com/grahambrereton-form3/tilt@v0.10.18/internal/hud/fake_hud.go (about) 1 package hud 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/windmilleng/tilt/internal/store" 9 "github.com/windmilleng/tilt/pkg/logger" 10 "github.com/windmilleng/tilt/pkg/model" 11 12 "github.com/windmilleng/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) SetNarrationMessage(ctx context.Context, msg string) error { 43 return nil 44 } 45 46 func (h *FakeHud) Refresh(ctx context.Context) {} 47 48 func (h *FakeHud) OnChange(ctx context.Context, st store.RStore) { 49 state := st.RLockState() 50 view := store.StateToView(state) 51 st.RUnlockState() 52 53 err := h.Update(view, h.viewState) 54 if err != nil { 55 logger.Get(ctx).Infof("Error updating HUD: %v", err) 56 } 57 } 58 59 func (h *FakeHud) Close() { 60 h.Closed = true 61 close(h.closeCh) 62 } 63 64 func (h *FakeHud) Update(v view.View, vs view.ViewState) error { 65 h.LastView = v 66 h.updates <- v 67 return nil 68 } 69 70 func (h *FakeHud) WaitUntilResource(t testing.TB, ctx context.Context, msg string, name model.ManifestName, isDone func(view.Resource) bool) { 71 h.WaitUntil(t, ctx, msg, func(view view.View) bool { 72 res, ok := view.Resource(name) 73 if !ok { 74 return false 75 } 76 return isDone(res) 77 }) 78 } 79 80 func (h *FakeHud) WaitUntil(t testing.TB, ctx context.Context, msg string, isDone func(view.View) bool) { 81 ctx, cancel := context.WithTimeout(ctx, time.Second) 82 defer cancel() 83 84 for { 85 select { 86 case <-ctx.Done(): 87 t.Fatalf("Timed out waiting for: %s", msg) 88 case view := <-h.updates: 89 done := isDone(view) 90 if done { 91 return 92 } 93 } 94 } 95 }