github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/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 . "src.elv.sh/pkg/eval" 11 "src.elv.sh/pkg/testutil" 12 13 . "src.elv.sh/pkg/eval/evaltest" 14 "src.elv.sh/pkg/fsutil" 15 "src.elv.sh/pkg/parse" 16 ) 17 18 // For error injection into the fsutil.GetHome function. 19 func currentUser() (*user.User, error) { 20 return nil, fmt.Errorf("user unknown") 21 } 22 23 func TestTildeAbbr(t *testing.T) { 24 tmpHome, cleanup := testutil.InTempHome() 25 defer cleanup() 26 27 testutil.MustMkdirAll("dir") 28 testutil.MustCreateEmpty("file") 29 30 Test(t, 31 That("tilde-abbr "+parse.Quote(filepath.Join(tmpHome, "foobar"))). 32 Puts(filepath.Join("~", "foobar")), 33 ) 34 } 35 36 func TestCd(t *testing.T) { 37 tmpHome, cleanup := testutil.InTempHome() 38 defer cleanup() 39 40 testutil.MustMkdirAll("d1") 41 d1Path := filepath.Join(tmpHome, "d1") 42 43 // We install this mock for all tests, not just the one that needs it, 44 // because it should not be invoked by any of the other tests. 45 fsutil.CurrentUser = currentUser 46 defer func() { fsutil.CurrentUser = user.Current }() 47 48 Test(t, 49 That(`cd dir1 dir2`).Throws(ErrArgs, "cd dir1 dir2"), 50 // Basic `cd` test and verification that `$pwd` is correct. 51 That(`old = $pwd; cd `+d1Path+`; put $pwd; cd $old; eq $old $pwd`).Puts(d1Path, true), 52 // Verify that `cd` with no arg defaults to the home directory. 53 That(`cd `+d1Path+`; cd; eq $pwd $E:HOME`).Puts(true), 54 // Verify that `cd` with no arg and no $E:HOME var fails since our 55 // currentUser mock should result in being unable to dynamically 56 // determine the user's home directory. 57 That(`unset-env HOME; cd; set-env HOME `+tmpHome).Throws( 58 errors.New("can't resolve ~: user unknown"), "cd"), 59 ) 60 } 61 62 func TestDirHistory(t *testing.T) { 63 // TODO: Add a Store mock so we can test the behavior when a history Store 64 // is available. 65 Test(t, 66 That(`dir-history`).Throws(ErrStoreNotConnected, "dir-history"), 67 ) 68 }