builtin_fn_cmd_unix_test.go 1.4 KB

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