fix(file-manager): browse symlinked directories and expand home
Symlinked directories were listed as files, so Enter opened them in the editor instead of browsing them. Mappings and the remote control API reach the panel without a shell, so --start-dir now expands a leading ~ itself. Add the missing Python integration tests for the result handler.
This commit is contained in:
4 files changed
+195
-3
No files matched your search
@@ -80,7 +80,17 @@ func read_dir(dir string, show_hidden bool, sort_by string) ([]Entry, error) {
|
||||
continue
|
||||
}
|
||||
e := Entry{Name: name, IsDir: item.IsDir()}
|
||||
if info, ierr := item.Info(); ierr == nil {
|
||||
info, ierr := item.Info()
|
||||
if item.Type()&os.ModeSymlink != 0 {
|
||||
// A symlink is reported as its own type, so a link to a directory
|
||||
// would be opened in the editor instead of being browsed. Follow
|
||||
// it to classify the entry; broken links stay plain entries.
|
||||
if target, terr := os.Stat(filepath.Join(dir, name)); terr == nil {
|
||||
info, ierr = target, nil
|
||||
e.IsDir = target.IsDir()
|
||||
}
|
||||
}
|
||||
if ierr == nil {
|
||||
e.ModTime = info.ModTime().Unix()
|
||||
e.Size = info.Size()
|
||||
}
|
||||
@@ -90,6 +100,23 @@ func read_dir(dir string, show_hidden bool, sort_by string) ([]Entry, error) {
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// expand_home expands a leading ~ so that a directory given in a mapping or by
|
||||
// the remote control API, where no shell expands it, names the same directory
|
||||
// as it would on the command line.
|
||||
func expand_home(path string) (string, error) {
|
||||
if path != "~" && !strings.HasPrefix(path, "~/") {
|
||||
return path, nil
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if path == "~" {
|
||||
return home, nil
|
||||
}
|
||||
return filepath.Join(home, path[2:]), nil
|
||||
}
|
||||
|
||||
func entry_path(dir, name string) string {
|
||||
return filepath.Join(dir, name)
|
||||
}
|
||||
@@ -169,6 +196,10 @@ func resolve_start_dir(candidate string) (string, error) {
|
||||
}
|
||||
candidate = cwd
|
||||
}
|
||||
candidate, err := expand_home(candidate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
abs, err := filepath.Abs(candidate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -80,6 +80,66 @@ func TestReadDirReportsErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDirAcceptsAnEmptyDirectory(t *testing.T) {
|
||||
entries, err := read_dir(t.TempDir(), false, "name")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("an empty directory must list nothing, got %#v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDirReportsPermissionDenied(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Chmod(dir, 0o000); err != nil {
|
||||
t.Skipf("cannot change permissions: %v", err)
|
||||
}
|
||||
// Make the directory readable again before TempDir cleans it up.
|
||||
defer os.Chmod(dir, 0o700) //nolint:errcheck
|
||||
if _, err := read_dir(dir, false, "name"); err == nil {
|
||||
t.Skip("the current user can read a mode 000 directory, probably running as root")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDirTreatsSymlinkedDirectoryAsDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(dir, "target"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("target", filepath.Join(dir, "link")); err != nil {
|
||||
t.Skipf("cannot create symlinks: %v", err)
|
||||
}
|
||||
entries, err := read_dir(dir, false, "name")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, e := range entries {
|
||||
if e.Name == "link" {
|
||||
if !e.IsDir {
|
||||
t.Fatal("a symlink to a directory must be browsable, not opened as a file")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("the symlink is missing from the listing: %#v", entries)
|
||||
}
|
||||
|
||||
func TestReadDirHandlesNonUTF8Names(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
name := string([]byte{'b', 'a', 'd', 0xff, '.', 't', 'x', 't'})
|
||||
if err := os.WriteFile(filepath.Join(dir, name), nil, 0o644); err != nil {
|
||||
t.Skipf("the filesystem rejects non-UTF-8 names: %v", err)
|
||||
}
|
||||
entries, err := read_dir(dir, false, "name")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 || entries[0].Name != name {
|
||||
t.Fatalf("the non-UTF-8 name must be listed unchanged, got %#v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateEntryName(t *testing.T) {
|
||||
for _, name := range []string{"", ".", "..", "a/b", "a\x00b"} {
|
||||
if err := validate_entry_name(name); err == nil {
|
||||
@@ -148,6 +208,22 @@ func TestCreateRenameAndDelete(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStartDirExpandsHome(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
projects := filepath.Join(home, "projects")
|
||||
if err := os.Mkdir(projects, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("HOME", home)
|
||||
got, err := resolve_start_dir("~/projects")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != projects {
|
||||
t.Fatalf("resolve_start_dir(~/projects) = %q, wanted %q", got, projects)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStartDirAcceptsFiles(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
file := filepath.Join(dir, "file.txt")
|
||||
|
||||
Reference in new issue
Block a user