builtin_fn_cmd_unix_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //go:build !windows && !plan9 && !js
  2. package eval_test
  3. import (
  4. "os/exec"
  5. "testing"
  6. . "src.elv.sh/pkg/eval/evaltest"
  7. )
  8. func TestHasExternal(t *testing.T) {
  9. Test(t,
  10. That("has-external sh").Puts(true),
  11. That("has-external random-invalid-command").Puts(false),
  12. )
  13. }
  14. func TestSearchExternal(t *testing.T) {
  15. Test(t,
  16. // Even on UNIX systems we can't assume that commands like `sh` or
  17. // `test` are in a specific directory. Those commands might be in /bin
  18. // or /usr/bin. However, on all systems we currently support it will
  19. // be in /bin and, possibly, /usr/bin. So ensure we limit the search
  20. // to the one universal UNIX directory for basic commands.
  21. That("{ tmp E:PATH = /bin; search-external sh }").Puts("/bin/sh"),
  22. // We should check for a specific error if the external command cannot
  23. // be found. However, the current implementation of `search-external`
  24. // returns the raw error returned by a Go runtime function over which
  25. // we have no control.
  26. //
  27. // TODO: Replace the raw Go runtime `exec.LookPath` error with an
  28. // Elvish error; possibly wrapping the Go runtime error. Then tighten
  29. // this test to that specific error.
  30. That("search-external random-invalid-command").Throws(ErrorWithType(&exec.Error{})),
  31. )
  32. }
  33. func TestExternal(t *testing.T) {
  34. Test(t,
  35. That(`(external sh) -c 'echo external-sh'`).Prints("external-sh\n"),
  36. )
  37. }