Skip to content

Commit

Permalink
actionPath: fix symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 11, 2024
1 parent dd08710 commit 7574ffb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
35 changes: 35 additions & 0 deletions example/cmd/action_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"
"runtime"
"testing"

"github.com/carapace-sh/carapace"
Expand Down Expand Up @@ -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")

Check failure on line 370 in example/cmd/action_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.Symlink` is not checked (errcheck)

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()"))
})
}
16 changes: 10 additions & 6 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{""}
}
Expand Down

0 comments on commit 7574ffb

Please sign in to comment.