2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2017-12-17 13:20:03 +08:00
|
|
|
|
2017-12-17 13:52:55 +08:00
|
|
|
import (
|
2020-08-09 11:08:04 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os/user"
|
2017-12-17 13:52:55 +08:00
|
|
|
"path/filepath"
|
2017-12-22 04:49:14 +08:00
|
|
|
"testing"
|
2018-05-22 07:54:29 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval"
|
|
|
|
"src.elv.sh/pkg/testutil"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-06-04 10:51:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
|
|
|
"src.elv.sh/pkg/fsutil"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2017-12-17 13:52:55 +08:00
|
|
|
)
|
|
|
|
|
2020-09-03 12:27:18 +08:00
|
|
|
// For error injection into the fsutil.GetHome function.
|
2020-08-09 11:08:04 +08:00
|
|
|
func currentUser() (*user.User, error) {
|
|
|
|
return nil, fmt.Errorf("user unknown")
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:07:50 +08:00
|
|
|
func TestTildeAbbr(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
tmpHome := testutil.InTempHome(t)
|
2018-05-22 07:54:29 +08:00
|
|
|
|
2020-09-05 04:57:20 +08:00
|
|
|
testutil.MustMkdirAll("dir")
|
|
|
|
testutil.MustCreateEmpty("file")
|
2017-12-17 13:42:30 +08:00
|
|
|
|
2018-10-12 11:02:14 +08:00
|
|
|
Test(t,
|
|
|
|
That("tilde-abbr "+parse.Quote(filepath.Join(tmpHome, "foobar"))).
|
|
|
|
Puts(filepath.Join("~", "foobar")),
|
|
|
|
)
|
2017-12-17 13:20:03 +08:00
|
|
|
}
|
2020-08-09 11:08:04 +08:00
|
|
|
|
2021-04-09 06:07:50 +08:00
|
|
|
func TestCd(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
tmpHome := testutil.InTempHome(t)
|
2020-08-09 11:08:04 +08:00
|
|
|
|
2020-09-05 04:57:20 +08:00
|
|
|
testutil.MustMkdirAll("d1")
|
2020-08-09 11:08:04 +08:00
|
|
|
d1Path := filepath.Join(tmpHome, "d1")
|
|
|
|
|
|
|
|
// We install this mock for all tests, not just the one that needs it,
|
|
|
|
// because it should not be invoked by any of the other tests.
|
2020-09-03 12:27:18 +08:00
|
|
|
fsutil.CurrentUser = currentUser
|
|
|
|
defer func() { fsutil.CurrentUser = user.Current }()
|
2020-08-09 11:08:04 +08:00
|
|
|
|
|
|
|
Test(t,
|
2021-06-04 10:51:38 +08:00
|
|
|
That(`cd dir1 dir2`).Throws(ErrorWithType(errs.ArityMismatch{}), "cd dir1 dir2"),
|
2020-08-09 11:08:04 +08:00
|
|
|
// Basic `cd` test and verification that `$pwd` is correct.
|
|
|
|
That(`old = $pwd; cd `+d1Path+`; put $pwd; cd $old; eq $old $pwd`).Puts(d1Path, true),
|
|
|
|
// Verify that `cd` with no arg defaults to the home directory.
|
|
|
|
That(`cd `+d1Path+`; cd; eq $pwd $E:HOME`).Puts(true),
|
|
|
|
// Verify that `cd` with no arg and no $E:HOME var fails since our
|
|
|
|
// currentUser mock should result in being unable to dynamically
|
|
|
|
// determine the user's home directory.
|
|
|
|
That(`unset-env HOME; cd; set-env HOME `+tmpHome).Throws(
|
|
|
|
errors.New("can't resolve ~: user unknown"), "cd"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-09 06:07:50 +08:00
|
|
|
func TestDirHistory(t *testing.T) {
|
2020-08-09 11:08:04 +08:00
|
|
|
// TODO: Add a Store mock so we can test the behavior when a history Store
|
|
|
|
// is available.
|
|
|
|
Test(t,
|
|
|
|
That(`dir-history`).Throws(ErrStoreNotConnected, "dir-history"),
|
|
|
|
)
|
|
|
|
}
|