mirror of
https://github.com/golang/go
synced 2024-11-21 18:14:42 -07:00
test: invoke go command in run.go
Lets us run multifile tests and tests with arguments. R=golang-dev, r CC=golang-dev https://golang.org/cl/5753068
This commit is contained in:
parent
5aee1f3a0f
commit
105c5fa666
@ -1,4 +1,4 @@
|
||||
// $G $D/$F.go $D/cmplxdivide1.go && $L $D/$F.$A && ./$A.out
|
||||
// run cmplxdivide1.go
|
||||
|
||||
// Copyright 2010 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
|
115
test/run.go
115
test/run.go
@ -210,6 +210,8 @@ func runTests() {
|
||||
}
|
||||
}
|
||||
|
||||
var cwd, _ = os.Getwd()
|
||||
|
||||
func (t *test) goFileName() string {
|
||||
return filepath.Join(t.dir, t.gofile)
|
||||
}
|
||||
@ -237,7 +239,13 @@ func (t *test) run() {
|
||||
if strings.HasPrefix(action, "//") {
|
||||
action = action[2:]
|
||||
}
|
||||
action = strings.TrimSpace(action)
|
||||
|
||||
var args []string
|
||||
f := strings.Fields(action)
|
||||
if len(f) > 0 {
|
||||
action = f[0]
|
||||
args = f[1:]
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "cmpout":
|
||||
@ -257,66 +265,52 @@ func (t *test) run() {
|
||||
err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644)
|
||||
check(err)
|
||||
|
||||
cmd := exec.Command("go", "tool", gc, "-e", "-o", "a."+letter, t.gofile)
|
||||
useTmp := true
|
||||
runcmd := func(args ...string) ([]byte, error) {
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
var buf bytes.Buffer
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &buf
|
||||
if useTmp {
|
||||
cmd.Dir = t.tempDir
|
||||
err = cmd.Run()
|
||||
out := buf.String()
|
||||
|
||||
if action == "errorcheck" {
|
||||
t.err = t.errorCheck(out)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("build = %v (%q)", err, out)
|
||||
return
|
||||
}
|
||||
|
||||
if action == "compile" {
|
||||
return
|
||||
}
|
||||
|
||||
if action == "build" || action == "run" {
|
||||
buf.Reset()
|
||||
cmd = exec.Command("go", "tool", ld, "-o", "a.out", "a."+letter)
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &buf
|
||||
cmd.Dir = t.tempDir
|
||||
err = cmd.Run()
|
||||
out = buf.String()
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("link = %v (%q)", err, out)
|
||||
return
|
||||
}
|
||||
if action == "build" {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if action == "run" {
|
||||
buf.Reset()
|
||||
cmd = exec.Command(filepath.Join(t.tempDir, "a.out"))
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &buf
|
||||
cmd.Dir = t.tempDir
|
||||
cmd.Env = append(cmd.Env, "GOARCH="+runtime.GOARCH)
|
||||
err = cmd.Run()
|
||||
out = buf.String()
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("run = %v (%q)", err, out)
|
||||
return
|
||||
}
|
||||
|
||||
if out != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("output differs; got:\n%s", out)
|
||||
}
|
||||
return
|
||||
}
|
||||
cmd.Env = append(cmd.Env, "GOOS="+runtime.GOOS, "GOARCH="+runtime.GOARCH)
|
||||
err := cmd.Run()
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
long := filepath.Join(cwd, t.goFileName())
|
||||
switch action {
|
||||
default:
|
||||
t.err = fmt.Errorf("unimplemented action %q", action)
|
||||
|
||||
case "errorcheck":
|
||||
out, _ := runcmd("go", "tool", gc, "-e", "-o", "a."+letter, long)
|
||||
t.err = t.errorCheck(string(out), long, t.gofile)
|
||||
return
|
||||
|
||||
case "compile":
|
||||
out, err := runcmd("go", "tool", gc, "-e", "-o", "a."+letter, long)
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("%s\n%s", err, out)
|
||||
}
|
||||
|
||||
case "build":
|
||||
out, err := runcmd("go", "build", "-o", "a.exe", long)
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("%s\n%s", err, out)
|
||||
}
|
||||
|
||||
case "run":
|
||||
useTmp = false
|
||||
out, err := runcmd(append([]string{"go", "run", t.goFileName()}, args...)...)
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("%s\n%s", err, out)
|
||||
}
|
||||
if string(out) != t.expectedOutput() {
|
||||
t.err = fmt.Errorf("incorrect output\n%s", out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *test) String() string {
|
||||
@ -337,7 +331,7 @@ func (t *test) expectedOutput() string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (t *test) errorCheck(outStr string) (err error) {
|
||||
func (t *test) errorCheck(outStr string, full, short string) (err error) {
|
||||
defer func() {
|
||||
if *verbose && err != nil {
|
||||
log.Printf("%s gc output:\n%s", t, outStr)
|
||||
@ -356,11 +350,16 @@ func (t *test) errorCheck(outStr string) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Cut directory name.
|
||||
for i := range out {
|
||||
out[i] = strings.Replace(out[i], full, short, -1)
|
||||
}
|
||||
|
||||
for _, we := range t.wantedErrors() {
|
||||
var errmsgs []string
|
||||
errmsgs, out = partitionStrings(we.filterRe, out)
|
||||
if len(errmsgs) == 0 {
|
||||
errs = append(errs, fmt.Errorf("errchk: %s:%d: missing expected error: %s", we.file, we.lineNum, we.reStr))
|
||||
errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
|
||||
continue
|
||||
}
|
||||
matched := false
|
||||
@ -372,7 +371,7 @@ func (t *test) errorCheck(outStr string) (err error) {
|
||||
}
|
||||
}
|
||||
if !matched {
|
||||
errs = append(errs, fmt.Errorf("errchk: %s:%d: error(s) on line didn't match pattern: %s", we.file, we.lineNum, we.reStr))
|
||||
errs = append(errs, fmt.Errorf("%s:%d: no match for %q in%s", we.file, we.lineNum, we.reStr, strings.Join(out, "\n")))
|
||||
continue
|
||||
}
|
||||
}
|
||||
@ -384,7 +383,7 @@ func (t *test) errorCheck(outStr string) (err error) {
|
||||
return errs[0]
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("Multiple errors:\n")
|
||||
fmt.Fprintf(&buf, "\n")
|
||||
for _, err := range errs {
|
||||
fmt.Fprintf(&buf, "%s\n", err.Error())
|
||||
}
|
||||
|
16
test/testlib
16
test/testlib
@ -14,7 +14,21 @@ build() {
|
||||
}
|
||||
|
||||
run() {
|
||||
$G $D/$F.go && $L $F.$A && ./$A.out "$@"
|
||||
gofiles=""
|
||||
ingo=true
|
||||
while $ingo; do
|
||||
case "$1" in
|
||||
*.go)
|
||||
gofiles="$gofiles $1"
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
ingo=false
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
$G $D/$F.go "$gofiles" && $L $F.$A && ./$A.out "$@"
|
||||
}
|
||||
|
||||
cmpout() {
|
||||
|
Loading…
Reference in New Issue
Block a user