elvish/pkg/eval/builtin_fn_cmd_unix_test.go
Kurtis Rader 87656c99fa Replace AnyError in tests with a specific error
The `AnyError` placeholder error can cause tests to succeed for errors
other than what was expected. That is, the use of `AnyError` can mask
bugs in a unit test. So replace it with the specific error, or error type,
the test expects to be raised.

This does not remove the anyError structure because it is used in
the TestCase.DoesNotCompile() method. To keep the size of this change
as small as possible I want to defer updating that use to a separate
change. However, remove the public AnyError var so future test writers
don't attempt to use it.
2021-12-13 01:08:24 +00:00

45 lines
1.4 KiB
Go

//go:build !windows && !plan9 && !js
// +build !windows,!plan9,!js
package eval_test
import (
"os/exec"
"testing"
. "src.elv.sh/pkg/eval/evaltest"
)
func TestHasExternal(t *testing.T) {
Test(t,
That("has-external sh").Puts(true),
That("has-external random-invalid-command").Puts(false),
)
}
func TestSearchExternal(t *testing.T) {
Test(t,
// Even on UNIX systems we can't assume that commands like `sh` or
// `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
// to the one universal UNIX directory for basic commands.
That("E:PATH=/bin search-external sh").Puts("/bin/sh"),
// 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.
That("search-external random-invalid-command").Throws(ErrorWithType(&exec.Error{})),
)
}
func TestExternal(t *testing.T) {
Test(t,
That(`(external sh) -c 'echo external-sh'`).Prints("external-sh\n"),
)
}