Skip to content

Commit 989ef47

Browse files
authored
feat: evaluate symlinks when connecting (#150)
1 parent 5312186 commit 989ef47

File tree

4 files changed

+127
-23
lines changed

4 files changed

+127
-23
lines changed

namer/namer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ func NewNamer(pathwrap pathwrap.Path, git git.Git) Namer {
2525
}
2626

2727
func (n *RealNamer) Name(path string) (string, error) {
28+
path, err := n.pathwrap.EvalSymlinks(path)
29+
if err != nil {
30+
return "", err
31+
}
32+
2833
strategies := []func(*RealNamer, string) (string, error){
2934
gitBareName,
3035
gitName,

namer/namer_test.go

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,69 @@ import (
1010
)
1111

1212
func TestFromPath(t *testing.T) {
13-
mockPathwrap := new(pathwrap.MockPath)
14-
mockGit := new(git.MockGit)
15-
n := NewNamer(mockPathwrap, mockGit)
16-
17-
t.Run("name for git repo", func(t *testing.T) {
18-
mockGit.On("ShowTopLevel", "/Users/josh/c/dotfiles/.config/neovim").Return(true, "/Users/josh/c/dotfiles", nil)
19-
mockGit.On("GitCommonDir", "/Users/josh/c/dotfiles/.config/neovim").Return(true, "", nil)
20-
mockPathwrap.On("Base", "/Users/josh/c/dotfiles").Return("dotfiles")
21-
name, _ := n.Name("/Users/josh/c/dotfiles/.config/neovim")
22-
assert.Equal(t, "dotfiles/_config/neovim", name)
23-
})
13+
t.Run("when path does not contain a symlink", func(t *testing.T) {
14+
mockPathwrap := new(pathwrap.MockPath)
15+
mockGit := new(git.MockGit)
16+
n := NewNamer(mockPathwrap, mockGit)
17+
18+
t.Run("name for git repo", func(t *testing.T) {
19+
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/dotfiles/.config/neovim").Return("/Users/josh/config/dotfiles/.config/neovim", nil)
20+
mockGit.On("ShowTopLevel", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "/Users/josh/config/dotfiles", nil)
21+
mockGit.On("GitCommonDir", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "", nil)
22+
mockPathwrap.On("Base", "/Users/josh/config/dotfiles").Return("dotfiles")
23+
name, _ := n.Name("/Users/josh/config/dotfiles/.config/neovim")
24+
assert.Equal(t, "dotfiles/_config/neovim", name)
25+
})
26+
27+
t.Run("name for git worktree", func(t *testing.T) {
28+
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/sesh/main").Return("/Users/josh/config/sesh/main", nil)
29+
mockGit.On("ShowTopLevel", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/main", nil)
30+
mockGit.On("GitCommonDir", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/.bare", nil)
31+
mockPathwrap.On("Base", "/Users/josh/config/sesh").Return("sesh")
32+
name, _ := n.Name("/Users/josh/config/sesh/main")
33+
assert.Equal(t, "sesh/main", name)
34+
})
2435

25-
t.Run("name for git worktree", func(t *testing.T) {
26-
mockGit.On("ShowTopLevel", "/Users/josh/c/sesh/main").Return(true, "/Users/josh/c/sesh/main", nil)
27-
mockGit.On("GitCommonDir", "/Users/josh/c/sesh/main").Return(true, "/Users/josh/c/sesh/.bare", nil)
28-
mockPathwrap.On("Base", "/Users/josh/c/sesh").Return("sesh")
29-
name, _ := n.Name("/Users/josh/c/sesh/main")
30-
assert.Equal(t, "sesh/main", name)
36+
t.Run("returns base on non-git dir", func(t *testing.T) {
37+
mockPathwrap.On("EvalSymlinks", "/Users/josh/.config/neovim").Return("/Users/josh/.config/neovim", nil)
38+
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
39+
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
40+
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
41+
name, _ := n.Name("/Users/josh/.config/neovim")
42+
assert.Equal(t, "neovim", name)
43+
})
3144
})
3245

33-
t.Run("returns base on non-git dir", func(t *testing.T) {
34-
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
35-
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
36-
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
37-
name, _ := n.Name("/Users/josh/.config/neovim")
38-
assert.Equal(t, "neovim", name)
46+
t.Run("when path contains a symlink", func(t *testing.T) {
47+
mockPathwrap := new(pathwrap.MockPath)
48+
mockGit := new(git.MockGit)
49+
n := NewNamer(mockPathwrap, mockGit)
50+
51+
t.Run("name for symlinked file in symlinked git repo", func(t *testing.T) {
52+
mockPathwrap.On("EvalSymlinks", "/Users/josh/d/.c/neovim").Return("/Users/josh/dotfiles/.config/neovim", nil)
53+
mockGit.On("ShowTopLevel", "/Users/josh/dotfiles/.config/neovim").Return(true, "/Users/josh/dotfiles", nil)
54+
mockGit.On("GitCommonDir", "/Users/josh/dotfiles/.config/neovim").Return(true, "", nil)
55+
mockPathwrap.On("Base", "/Users/josh/dotfiles").Return("dotfiles")
56+
name, _ := n.Name("/Users/josh/d/.c/neovim")
57+
assert.Equal(t, "dotfiles/_config/neovim", name)
58+
})
59+
60+
t.Run("name for git worktree", func(t *testing.T) {
61+
mockPathwrap.On("EvalSymlinks", "/Users/josh/p/sesh/main").Return("/Users/josh/projects/sesh/main", nil)
62+
mockGit.On("ShowTopLevel", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/main", nil)
63+
mockGit.On("GitCommonDir", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/.bare", nil)
64+
mockPathwrap.On("Base", "/Users/josh/projects/sesh").Return("sesh")
65+
name, _ := n.Name("/Users/josh/p/sesh/main")
66+
assert.Equal(t, "sesh/main", name)
67+
})
68+
69+
t.Run("returns base on non-git dir", func(t *testing.T) {
70+
mockPathwrap.On("EvalSymlinks", "/Users/josh/c/neovim").Return("/Users/josh/.config/neovim", nil)
71+
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
72+
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
73+
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
74+
name, _ := n.Name("/Users/josh/c/neovim")
75+
assert.Equal(t, "neovim", name)
76+
})
3977
})
4078
}

pathwrap/mock_Path.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pathwrap/pathwrap.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type Path interface {
99
Join(elem ...string) string
1010
Abs(path string) (string, error)
1111
Base(path string) string
12+
EvalSymlinks(path string) (string, error)
1213
}
1314

1415
type RealPath struct{}
@@ -28,3 +29,7 @@ func (p *RealPath) Abs(path string) (string, error) {
2829
func (p *RealPath) Base(path string) string {
2930
return filepath.Base(path)
3031
}
32+
33+
func (p *RealPath) EvalSymlinks(path string) (string, error) {
34+
return filepath.EvalSymlinks(path)
35+
}

0 commit comments

Comments
 (0)