chore: import official kitty sources from upstream
Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty, vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
commit
7e7d249099
1134 files changed
+433629
No files matched your search
@@ -0,0 +1,262 @@
|
||||
package watch
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sgtdi/fswatcher"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/cli"
|
||||
"github.com/kovidgoyal/kitty/tools/config"
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
// get_parent_dirs returns a deduplicated list of the immediate parent directory for each path.
|
||||
// Unlike get_unique_directories it does not filter out subdirectories, so every unique
|
||||
// parent is returned even when some are descendants of others. This is the correct
|
||||
// set of directories to pass to a non-recursive (top-level) file-system watcher.
|
||||
func get_parent_dirs(paths []string) []string {
|
||||
dirSet := utils.NewSet[string](len(paths))
|
||||
for _, p := range paths {
|
||||
dirSet.Add(filepath.Dir(p))
|
||||
}
|
||||
return dirSet.AsSlice()
|
||||
}
|
||||
|
||||
// returns the closest unique parent directories for a list of paths.
|
||||
// It excludes any directory that is a subdirectory of another directory already in the result set.
|
||||
func get_unique_directories(paths []string) []string {
|
||||
if len(paths) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 1. Extract parent directories and remove duplicates
|
||||
dirMap := utils.NewSet[string](len(paths))
|
||||
for _, p := range paths {
|
||||
dirMap.Add(filepath.Dir(p))
|
||||
}
|
||||
|
||||
// 2. Convert map to a sorted slice
|
||||
// Sorting ensures that shorter paths (potential parents) come before longer ones (potential children)
|
||||
uniqueDirs := dirMap.AsSlice()
|
||||
sort.Strings(uniqueDirs)
|
||||
|
||||
// 3. Filter out subdirectories
|
||||
var result []string
|
||||
for _, current := range uniqueDirs {
|
||||
isSubDir := false
|
||||
for _, parent := range result {
|
||||
// Check if 'current' is a subdirectory of 'parent'
|
||||
// Strings.HasPrefix is safe here because paths are sorted and Cleaned by filepath.Dir
|
||||
if current == parent || strings.HasPrefix(current, parent+string(filepath.Separator)) {
|
||||
isSubDir = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isSubDir {
|
||||
result = append(result, current)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// resolve_path resolves symlinks in the directory component of path so the result
|
||||
// matches what the OS-level file-system watcher (FSEvents on macOS, inotify on Linux)
|
||||
// reports. This is important on macOS where /tmp is a symlink to /private/tmp.
|
||||
// If the directory cannot be resolved (e.g. it doesn't exist yet) the path is
|
||||
// returned cleaned but unresolved.
|
||||
func resolve_path(path string) string {
|
||||
dir := filepath.Dir(path)
|
||||
if resolved, err := filepath.EvalSymlinks(dir); err == nil {
|
||||
return filepath.Join(resolved, filepath.Base(path))
|
||||
}
|
||||
return filepath.Clean(path)
|
||||
}
|
||||
|
||||
func safe_eval_symlinks(path string) string {
|
||||
if q, err := filepath.EvalSymlinks(path); err == nil {
|
||||
path = q
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func get_set_of_config_files(config_paths []string) *utils.Set[string] {
|
||||
cp := config.ConfigParser{
|
||||
AllIncludedFiles: utils.NewSet[string](), LineHandler: func(k, v string) error { return nil }}
|
||||
config_paths = utils.Filter(config_paths, func(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
})
|
||||
cp.ParseFiles(config_paths...)
|
||||
// Resolve symlinks in all paths collected by the parser (important on macOS
|
||||
// where /tmp -> /private/tmp causes mismatches with FSEvents-reported paths).
|
||||
result := utils.NewSet[string](cp.AllIncludedFiles.Len() + len(config_paths)*4)
|
||||
for _, p := range cp.AllIncludedFiles.AsSlice() {
|
||||
result.Add(safe_eval_symlinks(resolve_path(p)))
|
||||
}
|
||||
for _, path := range config_paths {
|
||||
path = resolve_path(path)
|
||||
dir := filepath.Dir(path)
|
||||
result.Add(safe_eval_symlinks(path))
|
||||
for _, q := range []string{"dark-theme.auto.conf", "light-theme.auto.conf", "no-preference-theme.auto.conf"} {
|
||||
result.Add(safe_eval_symlinks(resolve_path(filepath.Join(dir, q))))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// watch_for_config_changes watches the parent directories of every conf file (main configs,
|
||||
// includes, and auto color-scheme files) and calls action whenever one of those files changes.
|
||||
// Watching is non-recursive (top-level only): only the immediate parent directories are added.
|
||||
// When a conf file change is detected the full set of conf files is re-scanned so that newly
|
||||
// added or removed include directives are reflected in the watched-directory set.
|
||||
// It runs until ctx is cancelled.
|
||||
func watch_for_config_changes(ctx context.Context, action func() error, debounce_time time.Duration, config_paths []string) error {
|
||||
event_chan := make(chan fswatcher.WatchEvent)
|
||||
|
||||
all_paths := get_set_of_config_files(config_paths)
|
||||
|
||||
// desired_dirs is the full set of parent directories we want to watch
|
||||
// (one per conf file, including files that may not yet exist).
|
||||
desired_dirs := utils.NewSet[string]()
|
||||
for _, p := range all_paths.AsSlice() {
|
||||
desired_dirs.Add(filepath.Dir(p))
|
||||
}
|
||||
// Always watch the parent dirs of the original config paths so that we
|
||||
// detect file creation even when no config file exists yet (e.g. a fresh
|
||||
// empty config directory).
|
||||
for _, p := range config_paths {
|
||||
desired_dirs.Add(filepath.Dir(resolve_path(p)))
|
||||
}
|
||||
if desired_dirs.Len() == 0 {
|
||||
return fmt.Errorf("No directories to watch provided")
|
||||
}
|
||||
|
||||
// Create the watcher with top-level (non-recursive) depth.
|
||||
opts := []fswatcher.WatcherOpt{fswatcher.WithCooldown(debounce_time)}
|
||||
watched_dirs := utils.NewSet[string]()
|
||||
for _, dir := range desired_dirs.AsSlice() {
|
||||
if unix.Access(dir, unix.R_OK|unix.X_OK) == nil {
|
||||
opts = append(opts, fswatcher.WithPath(dir, fswatcher.WithDepth(fswatcher.WatchTopLevel)))
|
||||
watched_dirs.Add(dir)
|
||||
}
|
||||
}
|
||||
if watched_dirs.Len() == 0 {
|
||||
return fmt.Errorf("No directories to watch provided")
|
||||
}
|
||||
|
||||
w, err := fswatcher.New(opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go w.Watch(ctx)
|
||||
go func() {
|
||||
for event := range w.Events() {
|
||||
event_chan <- event
|
||||
}
|
||||
}()
|
||||
|
||||
// sync_watched_dirs reconciles watched_dirs with desired_dirs: any desired directory
|
||||
// that now exists is added to the watcher, and any watched directory that is no longer
|
||||
// desired is dropped.
|
||||
sync_watched_dirs := func() {
|
||||
desired_dirs.ForEach(func(d string) {
|
||||
if !watched_dirs.Has(d) && unix.Access(d, unix.R_OK|unix.X_OK) == nil {
|
||||
if err := w.AddPath(d, fswatcher.WithDepth(fswatcher.WatchTopLevel)); err == nil {
|
||||
watched_dirs.Add(d)
|
||||
}
|
||||
}
|
||||
})
|
||||
watched_dirs.ForEach(func(d string) {
|
||||
if !desired_dirs.Has(d) {
|
||||
_ = w.DropPath(d)
|
||||
watched_dirs.Discard(d)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case event := <-event_chan:
|
||||
// On every event try to activate any desired directories that may have been
|
||||
// created since the last check (e.g. a new include directory was mkdir'd).
|
||||
sync_watched_dirs()
|
||||
|
||||
new_all_paths := get_set_of_config_files(config_paths)
|
||||
if new_all_paths.Has(resolve_path(event.Path)) {
|
||||
// A conf file changed: rebuild desired_dirs from the new include set and
|
||||
// sync the watcher so new include directories are watched and stale ones dropped.
|
||||
new_desired := utils.NewSet[string]()
|
||||
for _, p := range new_all_paths.AsSlice() {
|
||||
new_desired.Add(filepath.Dir(p))
|
||||
}
|
||||
desired_dirs = new_desired
|
||||
sync_watched_dirs()
|
||||
|
||||
if err := action(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to signal kitty in event: %s with error: %s\n", event, err)
|
||||
}
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func watch_for_kitty_config_changes(action func() error, debounce_time time.Duration, config_paths []string) error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
}
|
||||
_ = scanner.Err()
|
||||
cancel()
|
||||
}()
|
||||
return watch_for_config_changes(ctx, action, debounce_time, config_paths)
|
||||
}
|
||||
|
||||
func signal_kitty_to_reload_config(kitty_pid int) error {
|
||||
return unix.Kill(kitty_pid, unix.SIGUSR1)
|
||||
}
|
||||
|
||||
func EntryPoint(root *cli.Command) {
|
||||
root.AddSubCommand(&cli.Command{
|
||||
Name: "__watch_conf__",
|
||||
Hidden: true,
|
||||
OnlyArgsAllowed: true,
|
||||
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
|
||||
if len(args) < 3 {
|
||||
return 1, fmt.Errorf("Usage: __watch_conf__ kitty_pid debounce_time_ms config_paths...")
|
||||
}
|
||||
kitty_pid, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
debounce_time_ms, err := strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
if debounce_time_ms < 0 {
|
||||
return 0, fmt.Errorf("debounce_time must be >= 0")
|
||||
}
|
||||
config_paths := utils.Map(resolve_path, args[2:])
|
||||
if err = watch_for_kitty_config_changes(
|
||||
func() error { return signal_kitty_to_reload_config(kitty_pid) },
|
||||
time.Millisecond*time.Duration(debounce_time_ms), config_paths); err != nil {
|
||||
return 1, err
|
||||
}
|
||||
return 0, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,486 @@
|
||||
// License: GPLv3 Copyright: 2025, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package watch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
// prime_watcher repeatedly writes to path until the watcher delivers an event and
|
||||
// the action fires, confirming the watcher goroutine is ready. A single write may
|
||||
// race the watcher's initialisation so retrying ensures at least one write lands
|
||||
// while the watcher is active. After success it waits one full debounce period
|
||||
// so the cooldown window is clear before returning.
|
||||
// Returns the current action count after settling.
|
||||
func prime_watcher(t *testing.T, path string, counter *atomic.Int32, debounce time.Duration) int32 {
|
||||
t.Helper()
|
||||
before := counter.Load()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
n := 0
|
||||
for time.Now().Before(deadline) {
|
||||
write_file(t, path, fmt.Sprintf("# prime %d\n", n))
|
||||
n++
|
||||
if wait_for_count(counter, before+1, debounce+100*time.Millisecond) > before {
|
||||
// Action fired — clear the debounce window before returning.
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
return counter.Load()
|
||||
}
|
||||
}
|
||||
t.Fatal("watcher failed to become ready: no action fired within 5 seconds of repeated prime writes")
|
||||
return 0
|
||||
}
|
||||
|
||||
func write_file(t *testing.T, path string, data string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(data), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetParentDirs(t *testing.T) {
|
||||
type tc struct {
|
||||
name string
|
||||
input []string
|
||||
expect []string
|
||||
}
|
||||
cases := []tc{
|
||||
{
|
||||
name: "nil input",
|
||||
input: nil,
|
||||
expect: nil,
|
||||
},
|
||||
{
|
||||
name: "single file",
|
||||
input: []string{"/a/b/file.conf"},
|
||||
expect: []string{"/a/b"},
|
||||
},
|
||||
{
|
||||
name: "files in same directory",
|
||||
input: []string{"/a/b/file1.conf", "/a/b/file2.conf"},
|
||||
expect: []string{"/a/b"},
|
||||
},
|
||||
{
|
||||
name: "sibling directories",
|
||||
input: []string{"/a/b/file.conf", "/a/c/file.conf"},
|
||||
expect: []string{"/a/b", "/a/c"},
|
||||
},
|
||||
{
|
||||
name: "parent and child directory both returned",
|
||||
// Unlike get_unique_directories, subdirectories are NOT filtered out.
|
||||
input: []string{"/a/file.conf", "/a/b/file.conf"},
|
||||
expect: []string{"/a", "/a/b"},
|
||||
},
|
||||
{
|
||||
name: "deeply nested all returned",
|
||||
input: []string{
|
||||
"/a/file.conf",
|
||||
"/a/b/file.conf",
|
||||
"/a/b/c/file.conf",
|
||||
},
|
||||
expect: []string{"/a", "/a/b", "/a/b/c"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := get_parent_dirs(tc.input)
|
||||
sort.Strings(result)
|
||||
sort.Strings(tc.expect)
|
||||
if tc.expect == nil {
|
||||
if len(result) != 0 {
|
||||
t.Fatalf("expected empty result, got %v", result)
|
||||
}
|
||||
return
|
||||
}
|
||||
if diff := cmp.Diff(tc.expect, result); diff != "" {
|
||||
t.Fatalf("get_parent_dirs mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUniqueDirectories(t *testing.T) {
|
||||
// Empty input
|
||||
if result := get_unique_directories(nil); result != nil {
|
||||
t.Fatalf("Expected nil for empty input, got %v", result)
|
||||
}
|
||||
|
||||
type tc struct {
|
||||
name string
|
||||
input []string
|
||||
expect []string
|
||||
}
|
||||
|
||||
cases := []tc{
|
||||
{
|
||||
name: "single file",
|
||||
input: []string{"/a/b/file.conf"},
|
||||
expect: []string{"/a/b"},
|
||||
},
|
||||
{
|
||||
name: "files in same directory",
|
||||
input: []string{"/a/b/file1.conf", "/a/b/file2.conf"},
|
||||
expect: []string{"/a/b"},
|
||||
},
|
||||
{
|
||||
name: "sibling directories",
|
||||
input: []string{"/a/b/file.conf", "/a/c/file.conf"},
|
||||
expect: []string{"/a/b", "/a/c"},
|
||||
},
|
||||
{
|
||||
name: "subdirectory is excluded",
|
||||
// /a/b is a subdirectory of /a, so only /a should be in results
|
||||
input: []string{"/a/file.conf", "/a/b/file.conf"},
|
||||
expect: []string{"/a"},
|
||||
},
|
||||
{
|
||||
name: "deeply nested subdirectory is excluded",
|
||||
input: []string{
|
||||
"/a/file.conf",
|
||||
"/a/b/file.conf",
|
||||
"/a/b/c/file.conf",
|
||||
"/x/y/file.conf",
|
||||
},
|
||||
expect: []string{"/a", "/x/y"},
|
||||
},
|
||||
{
|
||||
name: "duplicate parent directories deduplicated",
|
||||
input: []string{
|
||||
"/a/b/file1.conf",
|
||||
"/a/b/file2.conf",
|
||||
"/a/b/sub/file3.conf",
|
||||
},
|
||||
expect: []string{"/a/b"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := get_unique_directories(tc.input)
|
||||
sort.Strings(result)
|
||||
sort.Strings(tc.expect)
|
||||
if diff := cmp.Diff(tc.expect, result); diff != "" {
|
||||
t.Fatalf("get_unique_directories mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSetOfConfigFiles(t *testing.T) {
|
||||
tdir := resolve_path(t.TempDir())
|
||||
subdir := filepath.Join(tdir, "sub")
|
||||
if err := os.Mkdir(subdir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
main_conf := filepath.Join(tdir, "kitty.conf")
|
||||
included_conf := filepath.Join(subdir, "included.conf")
|
||||
|
||||
// Main config that includes another file
|
||||
write_file(t, main_conf, "include sub/included.conf\nfont_size 12\n")
|
||||
write_file(t, included_conf, "background black\n")
|
||||
|
||||
result := get_set_of_config_files([]string{main_conf})
|
||||
|
||||
// Must contain the main config file
|
||||
if !result.Has(main_conf) {
|
||||
t.Errorf("Expected set to contain main config %q", main_conf)
|
||||
}
|
||||
// Must contain the included file
|
||||
if !result.Has(included_conf) {
|
||||
t.Errorf("Expected set to contain included config %q", included_conf)
|
||||
}
|
||||
// Must contain auto color scheme files (even if they don't exist)
|
||||
for _, name := range []string{"dark-theme.auto.conf", "light-theme.auto.conf", "no-preference-theme.auto.conf"} {
|
||||
expected := filepath.Join(tdir, name)
|
||||
if !result.Has(expected) {
|
||||
t.Errorf("Expected set to contain auto color scheme file %q", expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// wait_for_count waits until counter reaches at least target or timeout fires.
|
||||
// Returns the count observed.
|
||||
func wait_for_count(counter *atomic.Int32, target int32, timeout time.Duration) int32 {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if counter.Load() >= target {
|
||||
return counter.Load()
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
return counter.Load()
|
||||
}
|
||||
|
||||
// TestWatchForConfigChanges consolidates all watcher integration tests into a single
|
||||
// function that starts the watcher once and confirms readiness via prime_watcher
|
||||
// instead of a blind time.Sleep. Include-watching scenarios (file changes, include
|
||||
// added/removed from main config, include added to an already-included file) run as
|
||||
// sequential subtests.
|
||||
func TestWatchForConfigChanges(t *testing.T) {
|
||||
tdir := resolve_path(t.TempDir())
|
||||
subdir := filepath.Join(tdir, "sub")
|
||||
extradir := filepath.Join(tdir, "extra")
|
||||
extra2dir := filepath.Join(tdir, "extra2")
|
||||
for _, d := range []string{subdir, extradir, extra2dir} {
|
||||
if err := os.Mkdir(d, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
main_conf := filepath.Join(tdir, "kitty.conf")
|
||||
included_conf := filepath.Join(subdir, "included.conf")
|
||||
dark_theme := filepath.Join(tdir, "dark-theme.auto.conf")
|
||||
unrelated := filepath.Join(tdir, "unrelated.txt")
|
||||
extra_conf := filepath.Join(extradir, "custom.conf")
|
||||
extra2_conf := filepath.Join(extra2dir, "another.conf")
|
||||
|
||||
write_file(t, main_conf, "include sub/included.conf\n")
|
||||
write_file(t, included_conf, "background black\n")
|
||||
write_file(t, dark_theme, "background #000000\n")
|
||||
write_file(t, unrelated, "this file should not trigger action\n")
|
||||
write_file(t, extra_conf, "background black\n")
|
||||
write_file(t, extra2_conf, "background black\n")
|
||||
|
||||
var action_count atomic.Int32
|
||||
action := func() error {
|
||||
action_count.Add(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
const debounce = 50 * time.Millisecond
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- watch_for_config_changes(ctx, action, debounce, []string{main_conf})
|
||||
}()
|
||||
|
||||
// Confirm the watcher is ready by writing to the dark-theme auto conf (an
|
||||
// always-watched file that does not affect the include graph) and waiting for
|
||||
// an action to fire. This replaces the blind time.Sleep used previously.
|
||||
prime_watcher(t, dark_theme, &action_count, debounce)
|
||||
|
||||
t.Run("main config change triggers action", func(t *testing.T) {
|
||||
before := action_count.Load()
|
||||
write_file(t, main_conf, "include sub/included.conf\nfont_size 13\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action to be called after main config change, count=%d", action_count.Load())
|
||||
}
|
||||
})
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
|
||||
t.Run("included file change triggers action", func(t *testing.T) {
|
||||
before := action_count.Load()
|
||||
write_file(t, included_conf, "background white\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action to be called after included file change, count=%d", action_count.Load())
|
||||
}
|
||||
})
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
|
||||
t.Run("auto color scheme file change triggers action", func(t *testing.T) {
|
||||
before := action_count.Load()
|
||||
write_file(t, dark_theme, "background #111111\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action to be called after dark-theme.auto.conf change, count=%d", action_count.Load())
|
||||
}
|
||||
})
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
|
||||
t.Run("unrelated file change does not trigger action", func(t *testing.T) {
|
||||
before := action_count.Load()
|
||||
write_file(t, unrelated, "still unrelated\n")
|
||||
time.Sleep(debounce + 200*time.Millisecond)
|
||||
if after := action_count.Load(); after != before {
|
||||
t.Fatalf("Expected action NOT to be called for unrelated file, count went from %d to %d", before, after)
|
||||
}
|
||||
})
|
||||
|
||||
// include added to main config: extradir must become watched.
|
||||
// sync_watched_dirs() runs before action() in the event loop, so by the time
|
||||
// wait_for_count returns the new directory is already registered.
|
||||
t.Run("include added to main config is watched", func(t *testing.T) {
|
||||
before := action_count.Load()
|
||||
write_file(t, main_conf, "include sub/included.conf\ninclude extra/custom.conf\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action after kitty.conf gained include directive")
|
||||
}
|
||||
// Let the debounce window clear before writing to the newly watched file.
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
before = action_count.Load()
|
||||
write_file(t, extra_conf, "background white\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action after modifying newly included file")
|
||||
}
|
||||
})
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
|
||||
// include added to an already-included file: extra2dir must become watched.
|
||||
t.Run("include added to already-included file adds its parent dir", func(t *testing.T) {
|
||||
// Add an include to sub/included.conf that points into extra2dir, which is
|
||||
// not currently watched. The watcher re-scans the full include graph on
|
||||
// every conf-file change, so extra2dir must be added to the watch set.
|
||||
before := action_count.Load()
|
||||
write_file(t, included_conf, "include ../extra2/another.conf\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action after sub/included.conf gained include directive")
|
||||
}
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
// extra2dir is now watched; a change to extra2/another.conf must fire.
|
||||
before = action_count.Load()
|
||||
write_file(t, extra2_conf, "background blue\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action after modifying file included from an already-included conf file")
|
||||
}
|
||||
})
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
|
||||
// include removed from main config: extradir must be dropped from the watch set.
|
||||
t.Run("include removed from main config is no longer watched", func(t *testing.T) {
|
||||
before := action_count.Load()
|
||||
write_file(t, main_conf, "include sub/included.conf\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action after kitty.conf lost include directive")
|
||||
}
|
||||
time.Sleep(debounce + 20*time.Millisecond)
|
||||
before = action_count.Load()
|
||||
write_file(t, extra_conf, "background green\n")
|
||||
time.Sleep(debounce + 200*time.Millisecond)
|
||||
if after := action_count.Load(); after != before {
|
||||
t.Fatalf("Expected NO action after modifying removed-include file, count went from %d to %d", before, after)
|
||||
}
|
||||
})
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("watch_for_config_changes returned error: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("watch_for_config_changes did not exit after context cancel")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWatchForConfigChangesEmptyConfigDir verifies that the watcher starts
|
||||
// successfully when the config file does not yet exist (e.g. KITTY_CONFIG_DIRECTORY
|
||||
// points to a fresh empty directory) and fires the action once the file is created.
|
||||
func TestWatchForConfigChangesEmptyConfigDir(t *testing.T) {
|
||||
tdir := resolve_path(t.TempDir())
|
||||
main_conf := filepath.Join(tdir, "kitty.conf")
|
||||
// main_conf intentionally does not exist — simulates a fresh empty config directory.
|
||||
|
||||
var action_count atomic.Int32
|
||||
action := func() error {
|
||||
action_count.Add(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
const debounce = 50 * time.Millisecond
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- watch_for_config_changes(ctx, action, debounce, []string{main_conf})
|
||||
}()
|
||||
|
||||
// prime_watcher creates kitty.conf on its first write and retries until
|
||||
// the watcher delivers an event, confirming the directory watch is active.
|
||||
prime_watcher(t, main_conf, &action_count, debounce)
|
||||
|
||||
// A subsequent modification must also trigger an action.
|
||||
before := action_count.Load()
|
||||
write_file(t, main_conf, "font_size 14\n")
|
||||
if wait_for_count(&action_count, before+1, 2*time.Second) <= before {
|
||||
t.Fatalf("Expected action after modifying config file created in previously empty dir")
|
||||
}
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("watch_for_config_changes returned error: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("watch_for_config_changes did not exit after context cancel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatchForConfigChangesDebounce(t *testing.T) {
|
||||
tdir := t.TempDir()
|
||||
main_conf := filepath.Join(tdir, "kitty.conf")
|
||||
write_file(t, main_conf, "font_size 12\n")
|
||||
|
||||
var action_count atomic.Int32
|
||||
action := func() error {
|
||||
action_count.Add(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
const debounce = 300 * time.Millisecond
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- watch_for_config_changes(ctx, action, debounce, []string{main_conf})
|
||||
}()
|
||||
|
||||
// Confirm the watcher is ready before sending the burst.
|
||||
prime_watcher(t, main_conf, &action_count, debounce)
|
||||
|
||||
// Write to the file several times rapidly within the debounce window.
|
||||
// The fswatcher debouncer drops events that occur within the cooldown period
|
||||
// after the first event, so only the first write should produce an action call.
|
||||
before_burst := action_count.Load()
|
||||
for i := range 5 {
|
||||
write_file(t, main_conf, fmt.Sprintf("font_size %d\n", 12+i))
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
|
||||
// Wait for up to one full debounce period for the first action to fire.
|
||||
count_after_burst := wait_for_count(&action_count, before_burst+1, debounce+500*time.Millisecond)
|
||||
action_calls_in_burst := count_after_burst - before_burst
|
||||
|
||||
// Debouncing should have collapsed the burst into at most 2 calls.
|
||||
// The fswatcher debouncer uses leading-edge logic: the first event fires
|
||||
// immediately and subsequent events within the cooldown window are dropped.
|
||||
// A trailing event may fire at the end of the cooldown window, giving at most 2.
|
||||
if action_calls_in_burst == 0 {
|
||||
t.Fatalf("Expected at least one action call after burst of writes, got 0")
|
||||
}
|
||||
if action_calls_in_burst > 2 {
|
||||
t.Fatalf("Expected debouncing to collapse burst: want ≤2 calls, got %d", action_calls_in_burst)
|
||||
}
|
||||
|
||||
// After waiting well past the debounce window, a new write should trigger exactly one more call.
|
||||
time.Sleep(debounce + 100*time.Millisecond)
|
||||
before_new := action_count.Load()
|
||||
write_file(t, main_conf, "font_size 20\n")
|
||||
count_new := wait_for_count(&action_count, before_new+1, 2*time.Second)
|
||||
if count_new <= before_new {
|
||||
t.Fatalf("Expected action after post-debounce write, count went from %d to %d", before_new, count_new)
|
||||
}
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("watch_for_config_changes returned error: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("watch_for_config_changes did not exit after context cancel")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user