mirror of https://github.com/rwxrob/bonzai
parent
017e0d3d2e
commit
47f7819e4a
@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2021 Robert S. Muhlestein.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"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 {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("missing name of executable")
|
||||
}
|
||||
path, err := exec.LookPath(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// exits the program unless there is an error
|
||||
return syscall.Exec(path, args, os.Environ())
|
||||
}
|
||||
|
||||
// Run 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 {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("missing name of executable")
|
||||
}
|
||||
path, err := exec.LookPath(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command(path, args[1:]...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stderr = os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright 2021 Robert S. Muhlestein.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
if os.Getenv("TESTING_EXEC") == "1" {
|
||||
err := Exec("go", "version")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], "-test.run=TestExec")
|
||||
cmd.Env = append(os.Environ(), "TESTING_EXEC=1")
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("process exited with %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExec_noargs(t *testing.T) {
|
||||
err := Exec()
|
||||
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")
|
||||
if err == nil {
|
||||
t.Error("should have failed since no command")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExec_nocmd(t *testing.T) {
|
||||
err := Exec("__inoexist")
|
||||
if err == nil {
|
||||
t.Error("should have failed since no command")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
err := Run("go", "version")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue