diff --git a/internal/pkg/platform/localserver.go b/internal/pkg/platform/localserver.go index 15468550..cbec8f92 100644 --- a/internal/pkg/platform/localserver.go +++ b/internal/pkg/platform/localserver.go @@ -399,9 +399,25 @@ func (r *LocalServer) WatchManifest(ctx context.Context, auth types.SlackAuth, a // Begin watching for manifest file changes go r.watchManifestEvents(ctx, w, isRemoteManifest, auth, app) + go closeWatcherOnDone(ctx, w) + return w.Start(time.Millisecond * 100) } +func closeWatcherOnDone(ctx context.Context, w *watcher.Watcher) { + <-ctx.Done() + w.Wait() + go w.Close() + for { + select { + case <-w.Event: + case <-w.Error: + case <-w.Closed: + return + } + } +} + // watchManifestEvents reinstalls the app on each manifest change until the context is canceled or the watcher closes func (r *LocalServer) watchManifestEvents(ctx context.Context, w *watcher.Watcher, isRemoteManifest bool, auth types.SlackAuth, app types.App) { for { diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index 8cb8536d..bdeedb7b 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -21,6 +21,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "sync" "testing" @@ -515,6 +516,7 @@ func Test_LocalServer_WatchManifest(t *testing.T) { manifestSource config.ManifestSource manifestErr error watch *hooks.WatchOpts + watchManifest bool expectedErr error expectedOutput []string }{ @@ -538,6 +540,11 @@ func Test_LocalServer_WatchManifest(t *testing.T) { watch: &hooks.WatchOpts{}, expectedOutput: []string{"Manifest source is remote", "Manifest watching is not enabled"}, }, + "watches manifest paths until the context ends": { + manifestSource: config.ManifestSourceRemote, + watchManifest: true, + expectedOutput: []string{"Watching manifest changes to file paths matching"}, + }, } { t.Run(name, func(t *testing.T) { ctx, cancel := context.WithCancel(slackcontext.MockContext(t.Context())) @@ -551,6 +558,14 @@ func Test_LocalServer_WatchManifest(t *testing.T) { clients := shared.NewClientFactory(clientsMock.MockClientFactory()) server := LocalServer{clients: clients} server.cliConfig.Config.Watch = tc.watch + if tc.watchManifest { + server.cliConfig.Config.Watch = &hooks.WatchOpts{ + Manifest: &hooks.ManifestWatchOpts{ + Paths: []string{t.TempDir()}, + FilterRegex: `manifest\.json$`, + }, + } + } errChan := make(chan error, 1) go func() { errChan <- server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) @@ -583,6 +598,32 @@ func Test_LocalServer_WatchManifest(t *testing.T) { } } +func Test_closeWatcherOnDone_drainsAnUnreadEvent(t *testing.T) { + dir := t.TempDir() + manifest := filepath.Join(dir, "manifest.json") + require.NoError(t, os.WriteFile(manifest, []byte("{}"), 0o600)) + w := watcher.New() + require.NoError(t, w.AddRecursive(dir)) + later := time.Now().Add(time.Hour) + require.NoError(t, os.Chtimes(manifest, later, later)) + started := make(chan error, 1) + go func() { started <- w.Start(time.Millisecond) }() + ctx, cancel := context.WithCancel(t.Context()) + cancel() + time.Sleep(50 * time.Millisecond) + done := make(chan struct{}) + go func() { + closeWatcherOnDone(ctx, w) + close(done) + }() + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "closeWatcherOnDone did not return with an unread event") + } + require.NoError(t, <-started) +} + func Test_LocalServer_watchManifestEvents(t *testing.T) { for name, tc := range map[string]struct { cancel bool