fix(cd): harden zoxide directory suggestions
This commit is contained in:
@@ -1,58 +1,47 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/fs"
|
||||
)
|
||||
|
||||
func TestZoxideGenerator(t *testing.T) {
|
||||
// Setup: Create a mock zoxide binary
|
||||
tmp := t.TempDir()
|
||||
mockZoxide := filepath.Join(tmp, "zoxide")
|
||||
|
||||
// Script that prints mock directories
|
||||
script := "#!/bin/sh\necho \"/home/verse/project1\n/home/verse/docs\n/home/verse/dev/iris\""
|
||||
_ = os.WriteFile(mockZoxide, []byte(script), 0755)
|
||||
dataDir := t.TempDir()
|
||||
project := filepath.Join(dataDir, "project1")
|
||||
docs := filepath.Join(dataDir, "docs")
|
||||
iris := filepath.Join(dataDir, "dev", "iris")
|
||||
missing := filepath.Join(dataDir, "deleted")
|
||||
for _, path := range []string{project, docs, iris} {
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
t.Fatalf("create directory %q: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
t.Setenv("PATH", tmp+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
binDir := t.TempDir()
|
||||
mockZoxide := filepath.Join(binDir, "zoxide")
|
||||
script := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' %q %q %q %q\n", project, docs, iris, missing)
|
||||
if err := os.WriteFile(mockZoxide, []byte(script), 0o755); err != nil {
|
||||
t.Fatalf("create mock zoxide: %v", err)
|
||||
}
|
||||
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
|
||||
gen := fs.ZoxideGenerator()
|
||||
|
||||
|
||||
t.Run("Query returns correct result when partial is empty", func(t *testing.T) {
|
||||
results := gen([]string{"z", ""}, "z ", "")
|
||||
if len(results) == 0 {
|
||||
t.Errorf("Expected results from zoxide history, got 0")
|
||||
results := gen([]string{"z", ""}, "z ", "")
|
||||
if len(results) != 3 {
|
||||
t.Fatalf("suggestion count = %d, want 3: %#v", len(results), results)
|
||||
}
|
||||
for _, result := range results {
|
||||
if result.Cmd == missing {
|
||||
t.Fatalf("missing directory was suggested: %#v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
t.Run("Path replaces home dir with ~", func(t *testing.T) {
|
||||
home, _ := os.UserHomeDir()
|
||||
results := gen([]string{"z", ""}, "z ", "")
|
||||
foundHome := false
|
||||
for _, r := range results {
|
||||
if strings.HasPrefix(r.Desc, "~") {
|
||||
foundHome = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundHome && home != "" {
|
||||
t.Logf("Warning: Did not find ~ in descriptions, home is %s", home)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
t.Run("Sort by descending score", func(t *testing.T) {
|
||||
results := gen([]string{"z", "i"}, "z ", "i")
|
||||
if len(results) >= 1 {
|
||||
if results[0].Cmd == "" {
|
||||
t.Errorf("Empty result command")
|
||||
}
|
||||
}
|
||||
})
|
||||
results = gen([]string{"z", "iris"}, "z ", "iris")
|
||||
if len(results) != 1 || results[0].Cmd != iris {
|
||||
t.Fatalf("fuzzy suggestions = %#v, want %q", results, iris)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user