mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-23 18:58:50 -06:00
Add ForceRunInDir()
This helper allows the caller to run a command in a given directory, and panic on failure.
This commit is contained in:
@@ -32,6 +32,14 @@ func ForceRun(exe string, args ...string) {
|
||||
d.Chk.NoError(err)
|
||||
}
|
||||
|
||||
// ForceRunInDir runs 'exe [args...]' in the given directory, and d.Chk()s on failure. Inherits the environment of the current process.
|
||||
func ForceRunInDir(dir, exe string, args ...string) {
|
||||
info, err := os.Stat(dir)
|
||||
d.Exp.NoError(err, "Can't stat %s", dir)
|
||||
d.Exp.True(info.IsDir(), "%s must be a path to a directory.", dir)
|
||||
d.Chk.NoError(runEnvDir(os.Stdout, os.Stderr, Env{}, dir, exe, args...))
|
||||
}
|
||||
|
||||
// runEnvDir 'exe [args...]' in dir with the environment env overlaid on that of the current process. If dir == "", use the current working directory.
|
||||
func runEnvDir(out, err io.Writer, env Env, dir, exe string, args ...string) error {
|
||||
cmd := exec.Command(exe, args...)
|
||||
|
||||
@@ -3,6 +3,7 @@ package runner
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -42,6 +43,34 @@ func (suite *SerialRunnerTestSuite) TearDownTest() {
|
||||
os.Remove(suite.dir)
|
||||
}
|
||||
|
||||
func (suite *SerialRunnerTestSuite) TestForceRunInDir() {
|
||||
scriptPath := filepath.Join(suite.dir, buildFileBasename)
|
||||
suite.makeTestBuildFile(scriptPath, []string{"print os.getcwd()"})
|
||||
|
||||
old := os.Stdout // keep backup of the real stdout
|
||||
r, w, err := os.Pipe()
|
||||
suite.NoError(err)
|
||||
os.Stdout = w
|
||||
defer func() { os.Stdout = old }()
|
||||
defer r.Close()
|
||||
|
||||
outC := make(chan string)
|
||||
// copy the output in a separate goroutine so printing can't block indefinitely
|
||||
go func() {
|
||||
buf := &bytes.Buffer{}
|
||||
io.Copy(buf, r)
|
||||
outC <- buf.String()
|
||||
}()
|
||||
|
||||
ForceRunInDir(suite.dir, "python", scriptPath)
|
||||
|
||||
w.Close()
|
||||
out := strings.TrimSpace(<-outC)
|
||||
actualSuiteDir, err := filepath.EvalSymlinks(suite.dir)
|
||||
suite.NoError(err)
|
||||
suite.Equal(actualSuiteDir, out)
|
||||
}
|
||||
|
||||
func (suite *SerialRunnerTestSuite) TestEnvVars() {
|
||||
makeEnvVarPrintBuildFile := func(path, varname string) {
|
||||
fmtStatement := fmt.Sprintf(`print os.environ['%s']`, varname)
|
||||
|
||||
Reference in New Issue
Block a user