feat(file-manager): sync the working directory with the shell
The panel and the shell each kept their own directory. The new sync_cwd option keeps them in step both ways, through files in a session directory exported by kitty rather than by typing a command into the terminal: the shell applies the directory chosen in the panel from its prompt hook with builtin cd, so nothing lands in the history and a half typed multi-line command is not submitted.
This commit is contained in:
12 files changed
+425
-6
No files matched your search
@@ -0,0 +1,100 @@
|
||||
// License: GPLv3 Copyright: 2026, FWS
|
||||
|
||||
package file_manager
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewShellSyncNeedsOptionDirectoryAndWindow(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if s := new_shell_sync(dir, "7", false); s != nil {
|
||||
t.Fatal("the sync must stay off until the option is enabled")
|
||||
}
|
||||
if s := new_shell_sync("", "7", true); s != nil {
|
||||
t.Fatal("the sync must be off without a session directory")
|
||||
}
|
||||
if s := new_shell_sync(dir, "", true); s != nil {
|
||||
t.Fatal("the sync must be off without a shell window id")
|
||||
}
|
||||
if s := new_shell_sync(filepath.Join(dir, "missing"), "7", true); s != nil {
|
||||
t.Fatal("the sync must be off when the session directory is missing")
|
||||
}
|
||||
if s := new_shell_sync(dir, "7", true); s == nil {
|
||||
t.Fatal("the sync must be on with an option, a directory and a window id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishRequestIsReadableAndLeavesNoTemporaryFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := new_shell_sync(dir, "42", true)
|
||||
if err := s.publish_request("/tmp/target dir"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.ReadFile(filepath.Join(dir, "42.set"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != "/tmp/target dir" {
|
||||
t.Fatalf("unexpected request payload %q", got)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(dir, "42.set.tmp")); err == nil {
|
||||
t.Fatal("the temporary file must be renamed, not left behind")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShellCwdIgnoresUnusablePaths(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := new_shell_sync(dir, "9", true)
|
||||
published := filepath.Join(dir, "9")
|
||||
for _, content := range []string{"", " \n", "relative/path", filepath.Join(dir, "gone")} {
|
||||
if err := os.WriteFile(published, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := s.shell_cwd(); got != "" {
|
||||
t.Fatalf("%q must not be followed, got %q", content, got)
|
||||
}
|
||||
}
|
||||
if got := s.shell_cwd(); got != "" {
|
||||
t.Fatalf("a missing file must not be followed, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTargetFollowsEachShellMoveOnce(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := new_shell_sync(dir, "3", true)
|
||||
moved := t.TempDir()
|
||||
s.last = dir
|
||||
if got := s.new_target(dir); got != "" {
|
||||
t.Fatalf("the shell has not moved yet, got %q", got)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "3"), []byte(moved), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := s.new_target(dir); got != moved {
|
||||
t.Fatalf("wanted %q, got %q", moved, got)
|
||||
}
|
||||
if got := s.new_target(dir); got != "" {
|
||||
t.Fatalf("a shell move must only be followed once, got %q", got)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "3"), []byte(dir), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := s.new_target(dir); got != "" {
|
||||
t.Fatalf("moving to the directory already displayed must be a no-op, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResultPayloadAdvertisesSyncOnlyWhenHandled(t *testing.T) {
|
||||
h := &Handler{result_action: "cd", result_path: "/tmp"}
|
||||
if payload := h.result_payload(); len(payload) != 2 {
|
||||
t.Fatalf("the shell must be typed into when the sync did not handle the move: %#v", payload)
|
||||
}
|
||||
h.sync_handled = true
|
||||
payload := h.result_payload()
|
||||
if payload["sync"] != "1" || payload["path"] != "/tmp" {
|
||||
t.Fatalf("unexpected payload: %#v", payload)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user