2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2020-08-17 03:18:20 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
2020-10-17 10:35:38 +08:00
|
|
|
"runtime"
|
2020-08-17 03:18:20 +08:00
|
|
|
"testing"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval"
|
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/testutil"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
|
|
|
"src.elv.sh/pkg/eval/vars"
|
2020-08-17 03:18:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuiltinPwd(t *testing.T) {
|
2021-08-07 05:18:09 +08:00
|
|
|
tmpHome := testutil.InTempHome(t)
|
2020-08-17 03:18:20 +08:00
|
|
|
|
2022-06-21 03:29:14 +08:00
|
|
|
must.MkdirAll("dir1")
|
|
|
|
must.MkdirAll("dir2")
|
2020-08-17 03:18:20 +08:00
|
|
|
dir1 := filepath.Join(tmpHome, "dir1")
|
|
|
|
dir2 := filepath.Join(tmpHome, "dir2")
|
|
|
|
|
|
|
|
Test(t,
|
2022-03-20 17:31:38 +08:00
|
|
|
That(`{ tmp pwd = dir1; put $pwd }; put $pwd`).Puts(dir1, tmpHome),
|
|
|
|
That(`{ tmp pwd = (num 1); put $pwd }`).Throws(vars.ErrPathMustBeString),
|
2020-08-17 03:18:20 +08:00
|
|
|
)
|
2020-10-17 10:35:38 +08:00
|
|
|
|
|
|
|
// 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,
|
2022-03-20 17:31:38 +08:00
|
|
|
That(`cd $E:HOME\dir2; { tmp pwd = $E:HOME; put $pwd }; put $pwd`).
|
|
|
|
Puts(tmpHome, dir2),
|
|
|
|
That(`cd $E:HOME\dir2; { tmp pwd = ..\dir1; put $pwd }; put $pwd`).
|
|
|
|
Puts(dir1, dir2),
|
|
|
|
That(`cd $E:HOME\dir1; { tmp pwd = ..\dir2; put $pwd }; put $pwd`).
|
|
|
|
Puts(dir2, dir1),
|
2020-10-17 10:35:38 +08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
Test(t,
|
2022-03-20 17:31:38 +08:00
|
|
|
That(`cd ~/dir2; { tmp pwd = ~; put $pwd }; put $pwd`).
|
|
|
|
Puts(tmpHome, dir2),
|
|
|
|
That(`cd ~/dir2; { tmp pwd = ~/dir1; put $pwd }; put $pwd`).
|
|
|
|
Puts(dir1, dir2),
|
|
|
|
That(`cd ~/dir1; { tmp pwd = ../dir2; put $pwd }; put $pwd`).
|
|
|
|
Puts(dir2, dir1),
|
2020-10-17 10:35:38 +08:00
|
|
|
)
|
|
|
|
}
|
2020-08-17 03:18:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the behavior when the CWD cannot be determined.
|
|
|
|
func TestBuiltinPwd_GetwdError(t *testing.T) {
|
2022-06-20 06:56:18 +08:00
|
|
|
testutil.Set(t, Getwd, func() (string, error) { return "", errors.New("cwd unknown") })
|
2020-08-17 03:18:20 +08:00
|
|
|
|
2022-06-20 06:56:18 +08:00
|
|
|
Test(t, That(`put $pwd`).Puts("/unknown/pwd"))
|
2020-08-17 03:18:20 +08:00
|
|
|
}
|