mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
6e8ecc8bd5
- Move NewEnvListVar to pkg/eval/vars. - De-export GlobPattern, GlobFlag and ExternalCmd. - Merge editor.go and chdir.go into eval.go, value_helper.go into compile_value.go. - Remove eval_internal_test.go and replace it with a new test for $pid.
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
package eval_test
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
. "github.com/elves/elvish/pkg/eval"
|
|
"github.com/elves/elvish/pkg/testutil"
|
|
|
|
. "github.com/elves/elvish/pkg/eval/evaltest"
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
|
)
|
|
|
|
func TestBuiltinPwd(t *testing.T) {
|
|
tmpHome, cleanup := testutil.InTempHome()
|
|
defer cleanup()
|
|
|
|
testutil.MustMkdirAll("dir1")
|
|
testutil.MustMkdirAll("dir2")
|
|
dir1 := filepath.Join(tmpHome, "dir1")
|
|
dir2 := filepath.Join(tmpHome, "dir2")
|
|
|
|
Test(t,
|
|
That(`pwd=dir1 put $pwd; put $pwd`).Puts(dir1, tmpHome),
|
|
That(`pwd=(float64 1) put $pwd`).Throws(vars.ErrPathMustBeString, "pwd=(float64 1)"),
|
|
)
|
|
|
|
// We could separate these two test variants into separate unit test
|
|
// modules but that's overkill for this situation and makes the
|
|
// equivalence between the two environments harder to see.
|
|
if runtime.GOOS == "windows" {
|
|
Test(t,
|
|
That(`cd $E:HOME\dir2; pwd=$E:HOME put $pwd; put $pwd`).Puts(tmpHome, dir2),
|
|
That(`cd $E:HOME\dir2; pwd=..\dir1 put $pwd; put $pwd`).Puts(dir1, dir2),
|
|
That(`cd $E:HOME\dir1; pwd=..\dir2 put $pwd; put $pwd`).Puts(dir2, dir1),
|
|
)
|
|
} else {
|
|
Test(t,
|
|
That(`cd ~/dir2; pwd=~ put $pwd; put $pwd`).Puts(tmpHome, dir2),
|
|
That(`cd ~/dir2; pwd=~/dir1 put $pwd; put $pwd`).Puts(dir1, dir2),
|
|
That(`cd ~/dir1; pwd=../dir2 put $pwd; put $pwd`).Puts(dir2, dir1),
|
|
)
|
|
}
|
|
}
|
|
|
|
// Verify the behavior when the CWD cannot be determined.
|
|
func TestBuiltinPwd_GetwdError(t *testing.T) {
|
|
origGetwd := Getwd
|
|
Getwd = mockGetwdWithError
|
|
defer func() { Getwd = origGetwd }()
|
|
|
|
Test(t,
|
|
That(`put $pwd`).Puts("/unknown/pwd"),
|
|
)
|
|
}
|
|
|
|
func mockGetwdWithError() (string, error) {
|
|
return "", errors.New("cwd unknown")
|
|
}
|