From 7574ffba9d75045fed27286356478aeba4723175 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 10 Oct 2024 21:40:58 +0200 Subject: [PATCH] actionPath: fix symlinks --- example/cmd/action_test.go | 35 +++++++++++++++++++++++++++++++++++ internalActions.go | 16 ++++++++++------ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/example/cmd/action_test.go b/example/cmd/action_test.go index 3e288319..1b1a47da 100644 --- a/example/cmd/action_test.go +++ b/example/cmd/action_test.go @@ -1,6 +1,8 @@ package cmd import ( + "os" + "runtime" "testing" "github.com/carapace-sh/carapace" @@ -350,3 +352,36 @@ func TestActionMultipartsN(t *testing.T) { Usage("ActionMultiPartsN()")) }) } + +func TestSymlink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("no symlink support on windows") + } + + sandbox.Package(t, "github.com/carapace-sh/carapace/example")(func(s *sandbox.Sandbox) { + s.Files( + "dirA/file1.txt", "", + "dirA/file2.png", "", + "dirB/dirC/file3.go", "", + "dirB/file4.md", "", + "file5.go", "", + ) + c := s.NewContext() + os.Symlink(c.Dir+"/dirA", c.Dir+"/symA") + + s.Run("action", "--directories", ""). + Expect(carapace.ActionValues("dirA/", "dirB/", "symA/"). + Tag("directories"). + StyleF(style.ForPath). + NoSpace('/'). + Usage("ActionDirectories()")) + + s.Run("action", "--files", "symA/"). + Expect(carapace.ActionValues("file1.txt", "file2.png"). + Prefix("symA/"). + Tag("files"). + StyleF(style.ForPath). + NoSpace('/'). + Usage("ActionFiles()")) + }) +} diff --git a/internalActions.go b/internalActions.go index 03a38ff1..054f10d9 100644 --- a/internalActions.go +++ b/internalActions.go @@ -47,19 +47,23 @@ func actionPath(fileSuffixes []string, dirOnly bool) Action { continue } - resolvedFile, err := file.Info() + info, err := file.Info() if err != nil { return ActionMessage(err.Error()) } - if resolved, err := filepath.EvalSymlinks(actualFolder + file.Name()); err == nil { - if stat, err := os.Stat(resolved); err == nil { - resolvedFile = stat + symlinkedDir := false + if evaluatedPath, err := filepath.EvalSymlinks(actualFolder + "/" + file.Name()); err == nil { + if evaluatedInfo, err := os.Stat(evaluatedPath); err == nil { + symlinkedDir = evaluatedInfo.IsDir() } } - if resolvedFile.IsDir() { + switch { + case info.IsDir(): vals = append(vals, displayFolder+file.Name()+"/", style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()+"/"), c)) - } else if !dirOnly { + case symlinkedDir: + vals = append(vals, displayFolder+file.Name()+"/", style.ForPath(filepath.Clean(actualFolder+"/"+file.Name()), c)) // TODO colorist not returning the symlink color + case !dirOnly: if len(fileSuffixes) == 0 { fileSuffixes = []string{""} }