mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
eval: Test Chdir.
This commit is contained in:
parent
3db78def10
commit
d28b246d12
55
eval/chdir_test.go
Normal file
55
eval/chdir_test.go
Normal 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")
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user