github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/builtin_fn_fs_test.go (about) 1 package eval_test 2 3 import ( 4 "errors" 5 "fmt" 6 "os/user" 7 "path/filepath" 8 "testing" 9 10 "github.com/markusbkk/elvish/pkg/eval/errs" 11 . "github.com/markusbkk/elvish/pkg/eval/evaltest" 12 "github.com/markusbkk/elvish/pkg/fsutil" 13 "github.com/markusbkk/elvish/pkg/parse" 14 "github.com/markusbkk/elvish/pkg/testutil" 15 ) 16 17 // For error injection into the fsutil.GetHome function. 18 func currentUser() (*user.User, error) { 19 return nil, fmt.Errorf("user unknown") 20 } 21 22 func TestTildeAbbr(t *testing.T) { 23 tmpHome := testutil.InTempHome(t) 24 25 testutil.MustMkdirAll("dir") 26 testutil.MustCreateEmpty("file") 27 28 Test(t, 29 That("tilde-abbr "+parse.Quote(filepath.Join(tmpHome, "foobar"))). 30 Puts(filepath.Join("~", "foobar")), 31 ) 32 } 33 34 func TestCd(t *testing.T) { 35 tmpHome := testutil.InTempHome(t) 36 37 testutil.MustMkdirAll("d1") 38 d1Path := filepath.Join(tmpHome, "d1") 39 40 // We install this mock for all tests, not just the one that needs it, 41 // because it should not be invoked by any of the other tests. 42 fsutil.CurrentUser = currentUser 43 defer func() { fsutil.CurrentUser = user.Current }() 44 45 Test(t, 46 That(`cd dir1 dir2`).Throws(ErrorWithType(errs.ArityMismatch{}), "cd dir1 dir2"), 47 // Basic `cd` test and verification that `$pwd` is correct. 48 That("var old = $pwd", "cd "+d1Path, "put $pwd", "cd $old", "eq $old $pwd"). 49 Puts(d1Path, true), 50 // Verify that `cd` with no arg defaults to the home directory. 51 That(`cd `+d1Path+`; cd; eq $pwd $E:HOME`).Puts(true), 52 // Verify that `cd` with no arg and no $E:HOME var fails since our 53 // currentUser mock should result in being unable to dynamically 54 // determine the user's home directory. 55 That(`unset-env HOME; cd; set-env HOME `+tmpHome).Throws( 56 errors.New("can't resolve ~: user unknown"), "cd"), 57 ) 58 }