2017-12-23 07:03:22 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-03-22 04:51:08 +08:00
|
|
|
"github.com/elves/elvish/parse"
|
2017-12-23 07:03:22 +08:00
|
|
|
"github.com/elves/elvish/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testAddDirer func(string, float64) error
|
|
|
|
|
|
|
|
func (t testAddDirer) AddDir(dir string, weight float64) error {
|
|
|
|
return t(dir, weight)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChdir(t *testing.T) {
|
2018-03-22 04:51:08 +08:00
|
|
|
inWithTempDir(func(pwd, dst string) {
|
|
|
|
ev := NewEvaler()
|
2017-12-23 07:03:22 +08:00
|
|
|
|
2018-03-22 04:51:08 +08:00
|
|
|
argDirInBefore, argDirInAfter := "", ""
|
|
|
|
ev.AddBeforeChdir(func(dir string) { argDirInBefore = dir })
|
|
|
|
ev.AddAfterChdir(func(dir string) { argDirInAfter = dir })
|
2017-12-23 07:03:22 +08:00
|
|
|
|
2018-03-22 04:51:08 +08:00
|
|
|
err := ev.Chdir(dst)
|
2017-12-23 07:03:22 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Chdir => error %v", err)
|
|
|
|
}
|
2018-03-22 04:51:08 +08:00
|
|
|
if envPwd := os.Getenv("PWD"); envPwd != dst {
|
|
|
|
t.Errorf("$PWD is %q after Chdir, want %q", envPwd, dst)
|
2017-12-23 07:03:22 +08:00
|
|
|
}
|
|
|
|
|
2018-03-22 04:51:08 +08:00
|
|
|
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)
|
2017-12-23 07:03:22 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-12-23 07:23:52 +08:00
|
|
|
|
2018-03-22 04:51:08 +08:00
|
|
|
func TestChdirElvishHooks(t *testing.T) {
|
|
|
|
inWithTempDir(func(pwd, dst string) {
|
2018-05-22 08:08:11 +08:00
|
|
|
Test(t,
|
2018-03-22 04:51:08 +08:00
|
|
|
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),
|
2018-05-22 08:08:11 +08:00
|
|
|
)
|
2018-03-22 04:51:08 +08:00
|
|
|
})
|
|
|
|
}
|
2017-12-23 07:23:52 +08:00
|
|
|
|
|
|
|
func TestChdirError(t *testing.T) {
|
2018-03-22 04:51:08 +08:00
|
|
|
util.InTempDir(func(pwd string) {
|
|
|
|
ev := NewEvaler()
|
|
|
|
err := ev.Chdir("i/dont/exist")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Chdir => no error when dir does not exist")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func inWithTempDir(f func(pwd, other string)) {
|
|
|
|
util.InTempDir(func(pwd string) {
|
|
|
|
util.WithTempDir(func(other string) {
|
|
|
|
f(pwd, other)
|
|
|
|
})
|
2017-12-23 07:23:52 +08:00
|
|
|
})
|
|
|
|
}
|