Cleanup previous PR
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run

Normalise the cwd passed to CompleteFiles once, so that a relative cwd,
such as a config directory from a relative XDG_CONFIG_HOME, no longer gets
joined onto itself twice. This also removes the second resolution pass
against cwd and the absolutize_path helper. Check for errors in the test
setup and test relative, ../ and ~/ prefixes against a custom cwd as well
as a relative cwd.
This commit is contained in:
Kovid Goyal 2026-11-14 08:29:17 +05:30
parent 2b47a83a0d
commit add52d9cf7
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 38 additions and 20 deletions

View file

@ -16,14 +16,6 @@ import (
var _ = fmt.Print
func absolutize_path(path, cwd string) string {
path = utils.Expanduser(path)
if !filepath.IsAbs(path) {
path = filepath.Join(cwd, path)
}
return filepath.Clean(path)
}
type FileEntry struct {
Name, CompletionCandidate, Abspath string
Mode os.FileMode
@ -37,16 +29,23 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error {
if err != nil {
return err
}
} else {
// cwd may be relative, for example, when the config directory comes from a relative XDG_CONFIG_HOME
cwd = utils.Abspath(utils.Expanduser(cwd))
}
// relative prefixes are resolved against cwd, not the process working directory
location := utils.Expanduser(prefix)
if !filepath.IsAbs(location) {
location = filepath.Join(cwd, location)
}
location := absolutize_path(prefix, cwd)
base_dir := ""
joinable_prefix := ""
switch prefix {
case ".":
base_dir = "."
base_dir = cwd
joinable_prefix = ""
case "./":
base_dir = "."
base_dir = cwd
joinable_prefix = "./"
case "/":
base_dir = "/"
@ -72,9 +71,6 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error {
if base_dir == "" {
base_dir = cwd
}
if !strings.HasPrefix(base_dir, "~") && !filepath.IsAbs(base_dir) {
base_dir = filepath.Join(cwd, base_dir)
}
// fmt.Printf("prefix=%#v base_dir=%#v joinable_prefix=%#v\n", prefix, base_dir, joinable_prefix)
entries, err := os.ReadDir(base_dir)
if err != nil {

View file

@ -104,14 +104,36 @@ func TestCompleteFiles(t *testing.T) {
// paths are resolved relative to the cwd passed in, such as the config directory, not the process cwd
cdir := t.TempDir()
os.Mkdir(filepath.Join(cdir, "sessions"), 0700)
os.WriteFile(filepath.Join(cdir, "sessions", "work.kitty-session"), nil, 0600)
for _, prefix := range []string{"sessions/", "sessions/w"} {
if err := os.Mkdir(filepath.Join(cdir, "sessions"), 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(cdir, "sessions", "work.kitty-session"), nil, 0600); err != nil {
t.Fatal(err)
}
test_with_cwd := func(prefix, cwd string, expected ...string) {
t.Helper()
actual := []string{}
CompleteFiles(prefix, func(entry *FileEntry) { actual = append(actual, entry.CompletionCandidate) }, cdir)
if !reflect.DeepEqual(actual, []string{"sessions/work.kitty-session"}) {
t.Fatalf("Did not complete %#v relative to the cwd argument: %#v", prefix, actual)
if err := CompleteFiles(prefix, func(entry *FileEntry) { actual = append(actual, entry.CompletionCandidate) }, cwd); err != nil {
t.Fatalf("Completing %#v relative to %#v failed: %s", prefix, cwd, err)
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Did not complete %#v relative to the cwd argument %#v\nExpected: %#v\nActual: %#v", prefix, cwd, expected, actual)
}
}
test_with_cwd("sessions/", cdir, "sessions/work.kitty-session")
test_with_cwd("sessions/w", cdir, "sessions/work.kitty-session")
test_with_cwd("./sessions/w", cdir, "./sessions/work.kitty-session")
test_with_cwd("../sessions/w", filepath.Join(cdir, "sessions"), "../sessions/work.kitty-session")
t.Setenv("HOME", cdir)
test_with_cwd("~/sessions/w", tdir, "~/sessions/work.kitty-session")
// a relative cwd, such as a config directory from a relative XDG_CONFIG_HOME, is relative to the process cwd
if err := os.Chdir(filepath.Dir(cdir)); err != nil {
t.Fatal(err)
}
test_with_cwd("sessions/", filepath.Base(cdir), "sessions/work.kitty-session")
test_with_cwd("s", filepath.Base(cdir), "sessions/")
if err := os.Chdir(tdir); err != nil {
t.Fatal(err)
}
// symlinks to directories are directories