eval: Test Chdir.

This commit is contained in:
Qi Xiao 2017-12-22 23:03:22 +00:00
parent 3db78def10
commit d28b246d12

55
eval/chdir_test.go Normal file
View File

@ -0,0 +1,55 @@
package eval
import (
"os"
"testing"
"time"
"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) {
util.WithTempDir(func(destDir string) {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
defer func() {
err = os.Chdir(pwd)
if err != nil {
panic(err)
}
}()
chanAddedDir := make(chan string)
testAddDirer := testAddDirer(func(dir string, weight float64) error {
chanAddedDir <- dir
return nil
})
err = Chdir(destDir, testAddDirer)
if err != nil {
t.Errorf("Chdir => error %v", err)
}
if env := os.Getenv("PWD"); env != destDir {
t.Errorf("$PWD is %q after Chdir, want %q", env, destDir)
}
select {
case addedDir := <-chanAddedDir:
if addedDir != destDir {
t.Errorf("Chdir called AddDir %q, want %q", addedDir[0], destDir)
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Chdir did not call AddDir within 100ms")
}
})
}