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"
|
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
|
|
|
|
2022-06-20 06:34:36 +08:00
|
|
|
. "src.elv.sh/pkg/eval"
|
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"
|
2022-06-21 03:29:14 +08:00
|
|
|
"src.elv.sh/pkg/must"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/parse"
|
2022-01-03 07:23:52 +08:00
|
|
|
"src.elv.sh/pkg/testutil"
|
2017-12-17 13:52:55 +08:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2022-06-21 03:29:14 +08:00
|
|
|
must.MkdirAll("dir")
|
|
|
|
must.CreateEmpty("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
|
|
|
|
2022-06-21 03:29:14 +08:00
|
|
|
must.MkdirAll("d1")
|
2020-08-09 11:08:04 +08:00
|
|
|
d1Path := filepath.Join(tmpHome, "d1")
|
|
|
|
|
|
|
|
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.
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var old = $pwd", "cd "+d1Path, "put $pwd", "cd $old", "eq $old $pwd").
|
|
|
|
Puts(d1Path, true),
|
2020-08-09 11:08:04 +08:00
|
|
|
// Verify that `cd` with no arg defaults to the home directory.
|
|
|
|
That(`cd `+d1Path+`; cd; eq $pwd $E:HOME`).Puts(true),
|
|
|
|
)
|
|
|
|
}
|
2022-06-20 06:34:36 +08:00
|
|
|
|
|
|
|
func TestCd_GetHomeError(t *testing.T) {
|
|
|
|
err := errors.New("fake error")
|
|
|
|
testutil.Set(t, GetHome, func(name string) (string, error) { return "", err })
|
|
|
|
|
|
|
|
Test(t, That("cd").Throws(err, "cd"))
|
|
|
|
}
|