Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkgs/mcpx/internal/source/abs_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package source

import "testing"

func TestAbsExpandsOnlyBareTildeAndTildeSlash(t *testing.T) {
// #1702 / #2988, rule pinned by triage task 1921 and mirrored from
// searchpath's TestOnlyBareTildeAndTildeSlashExpandHome: only a bare
// ~ and ~/... name the caller's home. A ~name path is not expanded --
// it resolves literally under Dir -- because resolving it under the
// caller's home silently reads a lookalike directory (and invents
// one for users that do not exist), while a passwd lookup would
// answer differently for the CLI user and the daemon user.
t.Setenv("HOME", "/home/testhome")
cases := []struct{ in, want string }{
{"~", "/home/testhome"},
{"~/x", "/home/testhome/x"},
{"~alice/x", "/w/~alice/x"},
{"~nosuchuser/x", "/w/~nosuchuser/x"},
{"~scratch", "/w/~scratch"},
}
for _, c := range cases {
if got := abs(c.in, "/w"); got != c.want {
t.Errorf("abs(%q) = %q, want %q", c.in, got, c.want)
}
}
}
2 changes: 1 addition & 1 deletion pkgs/mcpx/internal/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func abs(path, dir string) string {
if filepath.IsAbs(path) {
return path
}
if strings.HasPrefix(path, "~") {
if path == "~" || strings.HasPrefix(path, "~/") {
if home, err := os.UserHomeDir(); err == nil {
return filepath.Join(home, strings.TrimPrefix(path, "~"))
}
Expand Down
31 changes: 31 additions & 0 deletions pkgs/mcpx/internal/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,34 @@ func TestTextAndFileAtTheSameLevelIsAConflict(t *testing.T) {
t.Errorf("one form alone is fine: %v", err)
}
}

func TestResolveFileWithTildeNameReadsLiteralDirUnderDir(t *testing.T) {
// End to end for #2988: @file:~alice/... must read the literal
// ~alice directory under Dir, never $HOME/alice.
home := t.TempDir()
t.Setenv("HOME", home)
if err := os.MkdirAll(filepath.Join(home, "alice"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(home, "alice", "note.txt"), []byte("from home"), 0o644); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
literal := filepath.Join(dir, "~alice")
if err := os.MkdirAll(literal, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(literal, "note.txt"), []byte("from literal dir"), 0o644); err != nil {
t.Fatal(err)
}
got, err := source.Resolve("@file:~alice/note.txt", source.Options{Dir: dir})
if err != nil {
t.Fatal(err)
}
if got.Text != "from literal dir" {
t.Errorf("Resolve(@file:~alice/note.txt) text = %q, want %q", got.Text, "from literal dir")
}
if len(got.Files) != 1 || got.Files[0] != filepath.Join(literal, "note.txt") {
t.Errorf("Resolve(@file:~alice/note.txt) files = %v, want [%s]", got.Files, filepath.Join(literal, "note.txt"))
}
}
Loading