From 8b22a36d1c068f9baee68799b0c1f51ecb999e9d Mon Sep 17 00:00:00 2001 From: Bernhard Guillon Date: Mon, 6 Jul 2026 21:26:29 +0200 Subject: Add watcher tests, refactor watch() to accept context - Added watcher_test.go: 10 tests covering create, write, remove, rename, ignored files, debouncing, directory creation/removal, and manifest updates - Refactored watch() to accept context.Context for clean cancellation - Added signal handling (SIGINT/SIGTERM) for graceful shutdown in main.go - Fixed test expectations (version two = 11 bytes, not 10) --- watcher.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'watcher.go') diff --git a/watcher.go b/watcher.go index 7b823da..2b56bb4 100644 --- a/watcher.go +++ b/watcher.go @@ -1,6 +1,7 @@ package main import ( + "context" "log" "os" "path/filepath" @@ -12,7 +13,7 @@ import ( ignore "github.com/sabhiram/go-gitignore" ) -func watch(hostDir, dataDir string, manifest *Manifest, rules *ignore.GitIgnore, dryRun, verbose bool) error { +func watch(ctx context.Context, hostDir, dataDir string, manifest *Manifest, rules *ignore.GitIgnore, dryRun, verbose bool) error { watcher, err := fsnotify.NewWatcher() if err != nil { return err @@ -28,6 +29,9 @@ func watch(hostDir, dataDir string, manifest *Manifest, rules *ignore.GitIgnore, for { select { + case <-ctx.Done(): + return nil + case event, ok := <-watcher.Events: if !ok { return nil -- cgit v1.2.3