The panel and the shell now share their directory out of the box; whoever wants the previous independence sets sync_cwd no. The request is only written once the shell has published its directory, since that file is what proves its integration hook will read the request: a shell without shell integration still gets the cd typed into the terminal instead of losing the change.
121 lines
3.6 KiB
Go
121 lines
3.6 KiB
Go
// 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 TestShellHasHookNeedsAPublishedDirectory(t *testing.T) {
|
|
dir := t.TempDir()
|
|
s := new_shell_sync(dir, "5", true)
|
|
if s.shell_has_hook() {
|
|
t.Fatal("the shell published nothing yet")
|
|
}
|
|
if err := s.publish_request(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.shell_has_hook() {
|
|
t.Fatal("a request must not be mistaken for a published directory")
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "5"), []byte(dir), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !s.shell_has_hook() {
|
|
t.Fatal("a published directory means the shell has the hook")
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|