Skip to content

Commit

Permalink
feat: evaluate symlinks when connecting (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
glebglazov authored Sep 6, 2024
1 parent 5312186 commit 989ef47
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 23 deletions.
5 changes: 5 additions & 0 deletions namer/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func NewNamer(pathwrap pathwrap.Path, git git.Git) Namer {
}

func (n *RealNamer) Name(path string) (string, error) {
path, err := n.pathwrap.EvalSymlinks(path)
if err != nil {
return "", err
}

strategies := []func(*RealNamer, string) (string, error){
gitBareName,
gitName,
Expand Down
84 changes: 61 additions & 23 deletions namer/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,69 @@ import (
)

func TestFromPath(t *testing.T) {
mockPathwrap := new(pathwrap.MockPath)
mockGit := new(git.MockGit)
n := NewNamer(mockPathwrap, mockGit)

t.Run("name for git repo", func(t *testing.T) {
mockGit.On("ShowTopLevel", "/Users/josh/c/dotfiles/.config/neovim").Return(true, "/Users/josh/c/dotfiles", nil)
mockGit.On("GitCommonDir", "/Users/josh/c/dotfiles/.config/neovim").Return(true, "", nil)
mockPathwrap.On("Base", "/Users/josh/c/dotfiles").Return("dotfiles")
name, _ := n.Name("/Users/josh/c/dotfiles/.config/neovim")
assert.Equal(t, "dotfiles/_config/neovim", name)
})
t.Run("when path does not contain a symlink", func(t *testing.T) {
mockPathwrap := new(pathwrap.MockPath)
mockGit := new(git.MockGit)
n := NewNamer(mockPathwrap, mockGit)

t.Run("name for git repo", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/dotfiles/.config/neovim").Return("/Users/josh/config/dotfiles/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "/Users/josh/config/dotfiles", nil)
mockGit.On("GitCommonDir", "/Users/josh/config/dotfiles/.config/neovim").Return(true, "", nil)
mockPathwrap.On("Base", "/Users/josh/config/dotfiles").Return("dotfiles")
name, _ := n.Name("/Users/josh/config/dotfiles/.config/neovim")
assert.Equal(t, "dotfiles/_config/neovim", name)
})

t.Run("name for git worktree", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/config/sesh/main").Return("/Users/josh/config/sesh/main", nil)
mockGit.On("ShowTopLevel", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/main", nil)
mockGit.On("GitCommonDir", "/Users/josh/config/sesh/main").Return(true, "/Users/josh/config/sesh/.bare", nil)
mockPathwrap.On("Base", "/Users/josh/config/sesh").Return("sesh")
name, _ := n.Name("/Users/josh/config/sesh/main")
assert.Equal(t, "sesh/main", name)
})

t.Run("name for git worktree", func(t *testing.T) {
mockGit.On("ShowTopLevel", "/Users/josh/c/sesh/main").Return(true, "/Users/josh/c/sesh/main", nil)
mockGit.On("GitCommonDir", "/Users/josh/c/sesh/main").Return(true, "/Users/josh/c/sesh/.bare", nil)
mockPathwrap.On("Base", "/Users/josh/c/sesh").Return("sesh")
name, _ := n.Name("/Users/josh/c/sesh/main")
assert.Equal(t, "sesh/main", name)
t.Run("returns base on non-git dir", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/.config/neovim").Return("/Users/josh/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
name, _ := n.Name("/Users/josh/.config/neovim")
assert.Equal(t, "neovim", name)
})
})

t.Run("returns base on non-git dir", func(t *testing.T) {
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
name, _ := n.Name("/Users/josh/.config/neovim")
assert.Equal(t, "neovim", name)
t.Run("when path contains a symlink", func(t *testing.T) {
mockPathwrap := new(pathwrap.MockPath)
mockGit := new(git.MockGit)
n := NewNamer(mockPathwrap, mockGit)

t.Run("name for symlinked file in symlinked git repo", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/d/.c/neovim").Return("/Users/josh/dotfiles/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/dotfiles/.config/neovim").Return(true, "/Users/josh/dotfiles", nil)
mockGit.On("GitCommonDir", "/Users/josh/dotfiles/.config/neovim").Return(true, "", nil)
mockPathwrap.On("Base", "/Users/josh/dotfiles").Return("dotfiles")
name, _ := n.Name("/Users/josh/d/.c/neovim")
assert.Equal(t, "dotfiles/_config/neovim", name)
})

t.Run("name for git worktree", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/p/sesh/main").Return("/Users/josh/projects/sesh/main", nil)
mockGit.On("ShowTopLevel", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/main", nil)
mockGit.On("GitCommonDir", "/Users/josh/projects/sesh/main").Return(true, "/Users/josh/projects/sesh/.bare", nil)
mockPathwrap.On("Base", "/Users/josh/projects/sesh").Return("sesh")
name, _ := n.Name("/Users/josh/p/sesh/main")
assert.Equal(t, "sesh/main", name)
})

t.Run("returns base on non-git dir", func(t *testing.T) {
mockPathwrap.On("EvalSymlinks", "/Users/josh/c/neovim").Return("/Users/josh/.config/neovim", nil)
mockGit.On("ShowTopLevel", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockGit.On("GitCommonDir", "/Users/josh/.config/neovim").Return(false, "", fmt.Errorf("not a git repository (or any of the parent"))
mockPathwrap.On("Base", "/Users/josh/.config/neovim").Return("neovim")
name, _ := n.Name("/Users/josh/c/neovim")
assert.Equal(t, "neovim", name)
})
})
}
56 changes: 56 additions & 0 deletions pathwrap/mock_Path.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pathwrap/pathwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Path interface {
Join(elem ...string) string
Abs(path string) (string, error)
Base(path string) string
EvalSymlinks(path string) (string, error)
}

type RealPath struct{}
Expand All @@ -28,3 +29,7 @@ func (p *RealPath) Abs(path string) (string, error) {
func (p *RealPath) Base(path string) string {
return filepath.Base(path)
}

func (p *RealPath) EvalSymlinks(path string) (string, error) {
return filepath.EvalSymlinks(path)
}

0 comments on commit 989ef47

Please sign in to comment.