Rename Exec->SysExec, Run->Exec

pull/53/head
rwxrob 2 years ago
parent 8bfb8cf5a2
commit 13dcfca403
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -10,16 +10,15 @@ import (
"syscall"
)
// Exec (not to be confused with Execute) will check for the existence
// of the first argument as an executable on the system and then execute
// it using syscall.Exec(), which replaces the currently running program
// with the new one in all respects (stdin, stdout, stderr, process ID,
// signal handling, etc).
//
// Note that although this is exceptionally faster and cleaner than
// calling any of the os/exec variations it may be less compatible with
// different operating systems.
func Exec(args ...string) error {
// SysExec will check for the existence of the first argument as an
// executable on the system and then execute it using syscall.Exec(),
// which replaces the currently running program with the new one in all
// respects (stdin, stdout, stderr, process ID, signal handling, etc).
// Generally speaking, this is only available on UNIX variations. This
// is exceptionally faster and cleaner than calling any of the os/exec
// variations, but it can make your code far be less compatible
// with different operating systems.
func SysExec(args ...string) error {
if len(args) == 0 {
return fmt.Errorf("missing name of executable")
}
@ -31,13 +30,13 @@ func Exec(args ...string) error {
return syscall.Exec(path, args, os.Environ())
}
// Run checks for existence of first argument as an executable on the
// Exec checks for existence of first argument as an executable on the
// system and then runs it without exiting in a way that is supported
// across different operating systems. The stdin, stdout, and stderr are
// connected directly to that of the calling program. Use more specific
// exec alternatives if intercepting stdout and stderr are desired. Also
// see Exec.
func Run(args ...string) error {
// across all architectures that Go supports. The stdin, stdout, and stderr are
// connected directly to that of the calling program. Sometimes this is
// insufficient and the UNIX-specific SysExec is preferred. For example,
// when handing over control to a terminal editor such as Vim.
func Exec(args ...string) error {
if len(args) == 0 {
return fmt.Errorf("missing name of executable")
}

@ -11,7 +11,7 @@ import (
// go coverage detection is fucked for this sort of stuff, oh well, we
// did the test even if coverage falsely reports 50%
func TestExec(t *testing.T) {
func TestSysExec(t *testing.T) {
if os.Getenv("TESTING_EXEC") == "1" {
err := Exec("go", "version")
if err != nil {
@ -19,7 +19,7 @@ func TestExec(t *testing.T) {
}
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestExec")
cmd := exec.Command(os.Args[0], "-test.run=TestSysExec")
cmd.Env = append(os.Environ(), "TESTING_EXEC=1")
err := cmd.Run()
if err != nil {
@ -27,22 +27,15 @@ func TestExec(t *testing.T) {
}
}
func TestExec_noargs(t *testing.T) {
err := Exec()
func TestSysExec_noargs(t *testing.T) {
err := SysExec()
if err == nil {
t.Error("should have failed since no command")
}
}
func TestRun_noargs(t *testing.T) {
err := Run()
if err == nil {
t.Error("should have failed since no command")
}
}
func TestRun_nocmd(t *testing.T) {
err := Run("__inoexist")
func TestExec_noargs(t *testing.T) {
err := Exec()
if err == nil {
t.Error("should have failed since no command")
}
@ -55,8 +48,8 @@ func TestExec_nocmd(t *testing.T) {
}
}
func TestRun(t *testing.T) {
err := Run("go", "version")
func TestExec(t *testing.T) {
err := Exec("go", "version")
if err != nil {
t.Error(err)
}

Loading…
Cancel
Save