mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-24 10:30:48 -06:00
Add RunInDir()
Another helper function to run a command in a given directory, but without panicing on failure.
This commit is contained in:
@@ -40,6 +40,11 @@ func ForceRunInDir(dir, exe string, args ...string) {
|
||||
d.Chk.NoError(runEnvDir(os.Stdout, os.Stderr, Env{}, dir, exe, args...))
|
||||
}
|
||||
|
||||
// RunInDir runs 'exe [args...]' in the given directory, returning any failure. The child's stdout and stderr are mapped to out and err respectively. Inherits the environment of the current process.
|
||||
func RunInDir(out, err io.Writer, dir, exe string, args ...string) error {
|
||||
return runEnvDir(out, err, 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...)
|
||||
|
||||
@@ -16,7 +16,8 @@ import (
|
||||
|
||||
const (
|
||||
boilerplate = `
|
||||
import os
|
||||
from __future__ import print_function
|
||||
import os, sys
|
||||
|
||||
%s
|
||||
`
|
||||
@@ -45,7 +46,7 @@ func (suite *SerialRunnerTestSuite) TearDownTest() {
|
||||
|
||||
func (suite *SerialRunnerTestSuite) TestForceRunInDir() {
|
||||
scriptPath := filepath.Join(suite.dir, buildFileBasename)
|
||||
suite.makeTestBuildFile(scriptPath, []string{"print os.getcwd()"})
|
||||
suite.makeTestBuildFile(scriptPath, []string{"print(os.getcwd(), file=sys.stdout)"})
|
||||
|
||||
old := os.Stdout // keep backup of the real stdout
|
||||
r, w, err := os.Pipe()
|
||||
@@ -71,9 +72,25 @@ func (suite *SerialRunnerTestSuite) TestForceRunInDir() {
|
||||
suite.Equal(actualSuiteDir, out)
|
||||
}
|
||||
|
||||
func (suite *SerialRunnerTestSuite) TestRunInDir() {
|
||||
scriptPath := filepath.Join(suite.dir, buildFileBasename)
|
||||
suite.makeTestBuildFile(scriptPath, []string{
|
||||
"print(os.getcwd(), file=sys.stdout)",
|
||||
"print('error', file=sys.stderr)",
|
||||
})
|
||||
|
||||
stdout := &bytes.Buffer{}
|
||||
stderr := &bytes.Buffer{}
|
||||
RunInDir(stdout, stderr, suite.dir, "python", scriptPath)
|
||||
actualSuiteDir, err := filepath.EvalSymlinks(suite.dir)
|
||||
suite.NoError(err)
|
||||
suite.Equal(actualSuiteDir, strings.TrimSpace(string(stdout.Bytes())))
|
||||
suite.Equal("error", strings.TrimSpace(string(stderr.Bytes())))
|
||||
}
|
||||
|
||||
func (suite *SerialRunnerTestSuite) TestEnvVars() {
|
||||
makeEnvVarPrintBuildFile := func(path, varname string) {
|
||||
fmtStatement := fmt.Sprintf(`print os.environ['%s']`, varname)
|
||||
fmtStatement := fmt.Sprintf(`print(os.environ['%s'], file=sys.stdout)`, varname)
|
||||
suite.makeTestBuildFile(path, []string{fmtStatement})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user