2023-03-03 07:48:01 +08:00
|
|
|
//go:build unix
|
2020-08-08 12:50:41 +08:00
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2020-08-08 12:50:41 +08:00
|
|
|
|
|
|
|
import (
|
2021-07-15 10:31:01 +08:00
|
|
|
"os/exec"
|
2020-08-08 12:50:41 +08:00
|
|
|
"testing"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2020-08-08 12:50:41 +08:00
|
|
|
)
|
|
|
|
|
2021-04-08 07:42:58 +08:00
|
|
|
func TestHasExternal(t *testing.T) {
|
2020-08-08 12:50:41 +08:00
|
|
|
Test(t,
|
|
|
|
That("has-external sh").Puts(true),
|
|
|
|
That("has-external random-invalid-command").Puts(false),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:42:58 +08:00
|
|
|
func TestSearchExternal(t *testing.T) {
|
2020-08-08 12:50:41 +08:00
|
|
|
Test(t,
|
2023-01-05 02:09:11 +08:00
|
|
|
// Even on Unix systems we can't assume that commands like `sh` or
|
2020-08-08 12:50:41 +08:00
|
|
|
// `test` are in a specific directory. Those commands might be in /bin
|
|
|
|
// or /usr/bin. However, on all systems we currently support it will
|
|
|
|
// be in /bin and, possibly, /usr/bin. So ensure we limit the search
|
2023-01-05 02:09:11 +08:00
|
|
|
// to the one universal Unix directory for basic commands.
|
2022-03-20 17:31:38 +08:00
|
|
|
That("{ tmp E:PATH = /bin; search-external sh }").Puts("/bin/sh"),
|
2020-08-08 12:50:41 +08:00
|
|
|
// We should check for a specific error if the external command cannot
|
|
|
|
// be found. However, the current implementation of `search-external`
|
|
|
|
// returns the raw error returned by a Go runtime function over which
|
|
|
|
// we have no control.
|
|
|
|
//
|
|
|
|
// TODO: Replace the raw Go runtime `exec.LookPath` error with an
|
|
|
|
// Elvish error; possibly wrapping the Go runtime error. Then tighten
|
|
|
|
// this test to that specific error.
|
2021-07-15 10:31:01 +08:00
|
|
|
That("search-external random-invalid-command").Throws(ErrorWithType(&exec.Error{})),
|
2020-08-08 12:50:41 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-08 07:42:58 +08:00
|
|
|
func TestExternal(t *testing.T) {
|
2020-08-08 12:50:41 +08:00
|
|
|
Test(t,
|
|
|
|
That(`(external sh) -c 'echo external-sh'`).Prints("external-sh\n"),
|
|
|
|
)
|
|
|
|
}
|