mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
a943dc764a
There are some Navigation mode corner cases whose current behavior is surprising. Such as inserting a space before the filename when the space isn't wanted or redundant. Also, selecting an empty directory and pressing Enter should insert nothing rather than an empty string. Fixes #1169
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package eval_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/elves/elvish/pkg/env"
|
|
. "github.com/elves/elvish/pkg/eval"
|
|
|
|
. "github.com/elves/elvish/pkg/eval/evaltest"
|
|
"github.com/elves/elvish/pkg/parse"
|
|
"github.com/elves/elvish/pkg/testutil"
|
|
)
|
|
|
|
func TestChdir(t *testing.T) {
|
|
dst, cleanup := testutil.TestDir()
|
|
defer cleanup()
|
|
|
|
ev := NewEvaler()
|
|
|
|
argDirInBefore, argDirInAfter := "", ""
|
|
ev.AddBeforeChdir(func(dir string) { argDirInBefore = dir })
|
|
ev.AddAfterChdir(func(dir string) { argDirInAfter = dir })
|
|
|
|
back := saveWd()
|
|
defer back()
|
|
|
|
err := ev.Chdir(dst)
|
|
|
|
if err != nil {
|
|
t.Errorf("Chdir => error %v", err)
|
|
}
|
|
if envPwd := os.Getenv(env.PWD); envPwd != dst {
|
|
t.Errorf("$PWD is %q after Chdir, want %q", envPwd, dst)
|
|
}
|
|
|
|
if argDirInBefore != dst {
|
|
t.Errorf("Chdir called before-hook with %q, want %q",
|
|
argDirInBefore, dst)
|
|
}
|
|
if argDirInAfter != dst {
|
|
t.Errorf("Chdir called before-hook with %q, want %q",
|
|
argDirInAfter, dst)
|
|
}
|
|
}
|
|
|
|
func TestChdirElvishHooks(t *testing.T) {
|
|
dst, cleanup := testutil.TestDir()
|
|
defer cleanup()
|
|
|
|
back := saveWd()
|
|
defer back()
|
|
|
|
Test(t,
|
|
That(`
|
|
dir-in-before dir-in-after = '' ''
|
|
@before-chdir = [dst]{ dir-in-before = $dst }
|
|
@after-chdir = [dst]{ dir-in-after = $dst }
|
|
cd `+parse.Quote(dst)+`
|
|
put $dir-in-before $dir-in-after
|
|
`).Puts(dst, dst),
|
|
)
|
|
}
|
|
|
|
func TestChdirError(t *testing.T) {
|
|
_, cleanup := testutil.InTestDir()
|
|
defer cleanup()
|
|
|
|
ev := NewEvaler()
|
|
err := ev.Chdir("i/dont/exist")
|
|
if err == nil {
|
|
t.Errorf("Chdir => no error when dir does not exist")
|
|
}
|
|
}
|
|
|
|
// Saves the current working directory, and returns a function for returning to
|
|
// it.
|
|
func saveWd() func() {
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return func() {
|
|
testutil.MustChdir(wd)
|
|
}
|
|
}
|