mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-05 10:31:30 -06:00
Finding and running build scripts remains in Go (tools/run_all_build.go and supporting libs), but the build scripts themselves are in python now. This is because Go doesn't have much support for copying files and directories around, which is kind of the primary focus of build and packaging scripts. Towards issue 677
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package runner
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
const (
|
|
boilerplate = `
|
|
import os
|
|
|
|
%s
|
|
`
|
|
buildFileBasename = "build.py"
|
|
)
|
|
|
|
func TestSerialRunnerTestSuite(t *testing.T) {
|
|
suite.Run(t, &SerialRunnerTestSuite{})
|
|
}
|
|
|
|
type SerialRunnerTestSuite struct {
|
|
suite.Suite
|
|
dir string
|
|
index int
|
|
}
|
|
|
|
func (suite *SerialRunnerTestSuite) SetupTest() {
|
|
var err error
|
|
suite.dir, err = ioutil.TempDir(os.TempDir(), "")
|
|
suite.NoError(err)
|
|
}
|
|
|
|
func (suite *SerialRunnerTestSuite) TearDownTest() {
|
|
os.Remove(suite.dir)
|
|
}
|
|
|
|
func (suite *SerialRunnerTestSuite) TestEnvVars() {
|
|
makeEnvVarPrintBuildFile := func(path, varname string) {
|
|
fmtStatement := fmt.Sprintf(`print os.environ['%s']`, varname)
|
|
suite.makeTestBuildFile(path, []string{fmtStatement})
|
|
}
|
|
|
|
type testCase struct {
|
|
path, varname, expected string
|
|
}
|
|
env := Env{
|
|
"PATH": os.Getenv("PATH"),
|
|
"GOPATH": os.Getenv("GOPATH"),
|
|
"NOMS_CHECKOUT_PATH": "/where/noms/is",
|
|
"ATTIC_CHECKOUT_PATH": "/where/attic/is",
|
|
}
|
|
tests := []testCase{}
|
|
for n, v := range env {
|
|
tc := testCase{suite.uniqueBuildFile(), n, v}
|
|
makeEnvVarPrintBuildFile(tc.path, tc.varname)
|
|
tests = append(tests, tc)
|
|
}
|
|
gorootTestCase := testCase{suite.uniqueBuildFile(), "GOROOT", runtime.GOROOT()}
|
|
makeEnvVarPrintBuildFile(gorootTestCase.path, gorootTestCase.varname)
|
|
tests = append(tests, gorootTestCase)
|
|
|
|
log := &bytes.Buffer{}
|
|
if suite.True(Serial(log, log, env, suite.dir, buildFileBasename), "Serial() should have succeeded! logs:\n%s", string(log.Bytes())) {
|
|
logText := string(log.Bytes())
|
|
for _, tc := range tests {
|
|
suite.Contains(logText, tc.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (suite *SerialRunnerTestSuite) TestFailure() {
|
|
type testCase struct {
|
|
path, expected string
|
|
}
|
|
tests := []testCase{
|
|
testCase{suite.uniqueBuildFile(), "Scoobaz"},
|
|
testCase{suite.uniqueBuildFile(), "at the disco"},
|
|
}
|
|
goodOne := testCase{suite.uniqueBuildFile(), "All's well"}
|
|
|
|
suite.makeTestBuildFile(tests[0].path, []string{"Scoobaz() # Won't compile."})
|
|
suite.makeTestBuildFile(tests[1].path, []string{`assert(False, "at the disco") # Won't run.`})
|
|
suite.makeTestBuildFile(goodOne.path, []string{fmt.Sprintf(`print "%s"`, goodOne.expected)})
|
|
|
|
log := &bytes.Buffer{}
|
|
suite.False(Serial(log, log, Env{}, suite.dir, buildFileBasename))
|
|
logText := string(log.Bytes())
|
|
suite.Contains(logText, tests[0].expected)
|
|
suite.Contains(logText, tests[1].expected)
|
|
}
|
|
|
|
func (suite *SerialRunnerTestSuite) uniqueBuildFile() string {
|
|
suite.index++
|
|
return filepath.Join(suite.dir, fmt.Sprintf("%d", suite.index), buildFileBasename)
|
|
}
|
|
|
|
func (suite *SerialRunnerTestSuite) makeTestBuildFile(path string, statements []string) {
|
|
buf := &bytes.Buffer{}
|
|
fmt.Fprintf(buf, boilerplate, strings.Join(statements, "\n"))
|
|
err := os.MkdirAll(filepath.Dir(path), 0777)
|
|
suite.NoError(err)
|
|
err = ioutil.WriteFile(path, buf.Bytes(), 0755)
|
|
suite.NoError(err)
|
|
}
|