test: clean up run.go

• Inline check function because it's more readable.
• Delete toolPath because it was unused.
• Use strings.TrimPrefix because it's simpler.
• Remove out variable because its value was unused.
• Rename serr to err because it's more consistent.

Change-Id: I084fb4f8b399578834d5eea29a673c386cf3a357
Reviewed-on: https://go-review.googlesource.com/c/go/+/218701
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
This commit is contained in:
Dmitri Shuralyov 2020-02-09 01:40:45 -05:00
parent 90c71cec5b
commit 821b799482

View file

@ -166,14 +166,6 @@ func main() {
}
}
func toolPath(name string) string {
p := filepath.Join(os.Getenv("GOROOT"), "bin", "tool", name)
if _, err := os.Stat(p); err != nil {
log.Fatalf("didn't find binary at %s", p)
}
return p
}
// goTool reports the path of the go tool to use to run the tests.
// If possible, use the same Go used to run run.go, otherwise
// fallback to the go version found in the PATH.
@ -201,10 +193,14 @@ func shardMatch(name string) bool {
func goFiles(dir string) []string {
f, err := os.Open(dir)
check(err)
if err != nil {
log.Fatal(err)
}
dirnames, err := f.Readdirnames(-1)
f.Close()
check(err)
if err != nil {
log.Fatal(err)
}
names := []string{}
for _, name := range dirnames {
if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
@ -262,12 +258,6 @@ type skipError string
func (s skipError) Error() string { return string(s) }
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
// test holds the state of a test.
type test struct {
dir, gofile string
@ -361,7 +351,9 @@ func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
for _, file := range files {
name := file.Name()
pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
check(err)
if err != nil {
log.Fatal(err)
}
i, ok := m[pkgname]
if singlefilepkgs || !ok {
i = len(pkgs)
@ -500,9 +492,7 @@ func (t *test) run() {
// skip first line
action = action[nl+1:]
}
if strings.HasPrefix(action, "//") {
action = action[2:]
}
action = strings.TrimPrefix(action, "//")
// Check for build constraints only up to the actual code.
pkgPos := strings.Index(t.src, "\npackage")
@ -601,7 +591,9 @@ func (t *test) run() {
}
err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644)
check(err)
if err != nil {
log.Fatal(err)
}
// A few tests (of things like the environment) require these to be set.
if os.Getenv("GOOS") == "" {
@ -814,8 +806,10 @@ func (t *test) run() {
pflags = append(pflags, flags...)
if setpkgpaths {
fp := filepath.Join(longdir, gofiles[0])
pkgname, serr := getPackageNameFromSource(fp)
check(serr)
pkgname, err := getPackageNameFromSource(fp)
if err != nil {
log.Fatal(err)
}
pflags = append(pflags, "-p", pkgname)
}
_, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
@ -967,13 +961,13 @@ func (t *test) run() {
longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile)
cmd = append(cmd, flags...)
cmd = append(cmd, longdirgofile)
out, err := runcmd(cmd...)
_, err := runcmd(cmd...)
if err != nil {
t.err = err
return
}
cmd = []string{"./a.exe"}
out, err = runcmd(append(cmd, args...)...)
out, err := runcmd(append(cmd, args...)...)
if err != nil {
t.err = err
return
@ -1129,7 +1123,9 @@ func (t *test) String() string {
func (t *test) makeTempDir() {
var err error
t.tempDir, err = ioutil.TempDir("", "")
check(err)
if err != nil {
log.Fatal(err)
}
if *keep {
log.Printf("Temporary directory is %s", t.tempDir)
}