elvish/pkg/eval/external_cmd_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

37 lines
1.2 KiB
Go

package eval_test
import (
"os"
"os/exec"
"testing"
. "src.elv.sh/pkg/eval"
. "src.elv.sh/pkg/eval/evaltest"
"src.elv.sh/pkg/testutil"
)
func TestBuiltinFnExternal(t *testing.T) {
tmpHome := testutil.InTempHome(t)
testutil.Setenv(t, "PATH", tmpHome+":"+os.Getenv("PATH"))
Test(t,
That(`e = (external true); kind-of $e`).Puts("fn"),
That(`e = (external true); put (repr $e)`).Puts("<external true>"),
That(`e = (external false); m = [&$e=true]; put (repr $m)`).Puts("[&<external false>=true]"),
// Test calling of external commands.
That(`e = (external true); $e`).DoesNothing(),
That(`e = (external true); $e &option`).Throws(ErrExternalCmdOpts, "$e &option"),
That(`e = (external false); $e`).Throws(CmdExit(
ExternalCmdExit{CmdName: "false", WaitStatus: exitWaitStatus(1)})),
// TODO: Modify the ExternalCmd.Call method to wrap the Go error in a
// predictable Elvish error so we don't have to resort to using
// ThrowsAny in the following tests.
//
// The command shouldn't be found when run so we should get an
// exception along the lines of "executable file not found in $PATH".
That(`e = (external true); E:PATH=/ $e`).Throws(ErrorWithType(&exec.Error{})),
)
}