github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/builtin_fn_cmd_unix_test.go (about)

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