mirror of
https://github.com/golang/go.git
synced 2025-05-05 07:33:00 +00:00
test: compile source files as if from "test" module
This CL updates test/run.go to compile xxx.dir/x.go with a package path of "test/x" instead of just "x". This prevents collisions with standard library packages. It also requires updating a handful of tests to account for the updated package paths. Fixes #25693. Change-Id: I49208c56ab3cb229ed667d547cd6e004d2175fcf Reviewed-on: https://go-review.googlesource.com/c/go/+/395258 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
b2643c6739
commit
b95d332c7e
@ -1,10 +1,7 @@
|
||||
// +build !windows
|
||||
// errorcheckdir -n
|
||||
// errorcheckdir
|
||||
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ignored
|
||||
|
||||
// TODO(ysmolsky): Fix golang.org/issue/25693 to enable on Windows.
|
||||
|
@ -5,9 +5,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"./mysync"
|
||||
"log"
|
||||
"runtime"
|
||||
|
||||
"./mysync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -23,8 +24,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
expecting := []string{
|
||||
"mysync.(*WaitGroup).Add",
|
||||
"mysync.(*WaitGroup).Done",
|
||||
"test/mysync.(*WaitGroup).Add",
|
||||
"test/mysync.(*WaitGroup).Done",
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if frames[i].Function != expecting[i] {
|
||||
|
@ -51,14 +51,14 @@ func f() int {
|
||||
}
|
||||
iter := runtime.CallersFrames(pcs[:n])
|
||||
f, more := iter.Next()
|
||||
if f.Function != "a.f" || !strings.HasSuffix(f.File, "a.go") || f.Line != 22 {
|
||||
if f.Function != "test/a.f" || !strings.HasSuffix(f.File, "a.go") || f.Line != 22 {
|
||||
panic(fmt.Sprintf("bad f %v\n", f))
|
||||
}
|
||||
if !more {
|
||||
panic("traceback truncated after f")
|
||||
}
|
||||
f, more = iter.Next()
|
||||
if f.Function != "a.init" || !strings.HasSuffix(f.File, "a.go") || f.Line != 15 {
|
||||
if f.Function != "test/a.init" || !strings.HasSuffix(f.File, "a.go") || f.Line != 15 {
|
||||
panic(fmt.Sprintf("bad init %v\n", f))
|
||||
}
|
||||
if !more {
|
||||
|
@ -5,11 +5,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"./a"
|
||||
_ "unsafe"
|
||||
|
||||
"./a"
|
||||
)
|
||||
|
||||
//go:linkname s a.s
|
||||
//go:linkname s test/a.s
|
||||
var s string
|
||||
|
||||
func main() {
|
||||
|
@ -1,9 +1,9 @@
|
||||
package p
|
||||
|
||||
import (
|
||||
"./a" // ERROR "imported and not used: \x22a\x22 as surprise|imported and not used: surprise"
|
||||
"./b" // ERROR "imported and not used: \x22b\x22 as surprise2|imported and not used: surprise2"
|
||||
b "./b" // ERROR "imported and not used: \x22b\x22$|imported and not used: surprise2"
|
||||
"./a" // ERROR "imported and not used: \x22test/a\x22 as surprise|imported and not used: surprise"
|
||||
"./b" // ERROR "imported and not used: \x22test/b\x22 as surprise2|imported and not used: surprise2"
|
||||
b "./b" // ERROR "imported and not used: \x22test/b\x22$|imported and not used: surprise2"
|
||||
foo "math" // ERROR "imported and not used: \x22math\x22 as foo|imported and not used: math"
|
||||
"fmt" // actually used
|
||||
"strings" // ERROR "imported and not used: \x22strings\x22|imported and not used: strings"
|
||||
|
@ -2,7 +2,7 @@ package y
|
||||
|
||||
import _ "unsafe"
|
||||
|
||||
//go:linkname byteIndex linkname1.indexByte
|
||||
//go:linkname byteIndex test/linkname1.indexByte
|
||||
func byteIndex(xs []byte, b byte) int // ERROR "leaking param: xs"
|
||||
|
||||
func ContainsSlash(data []byte) bool { // ERROR "leaking param: data" "can inline ContainsSlash"
|
||||
|
32
test/run.go
32
test/run.go
@ -263,14 +263,13 @@ func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, er
|
||||
return runcmd(cmd...)
|
||||
}
|
||||
|
||||
func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool, pkgname string, names ...string) (out []byte, err error) {
|
||||
if pkgname != "main" {
|
||||
pkgname = strings.TrimSuffix(names[0], ".go")
|
||||
}
|
||||
cmd := []string{goTool(), "tool", "compile", "-e", "-p=" + pkgname}
|
||||
if localImports {
|
||||
// Set relative path for local imports and import search path to current dir.
|
||||
cmd = append(cmd, "-D", ".", "-I", ".")
|
||||
func compileInDir(runcmd runCmd, dir string, flags []string, pkgname string, names ...string) (out []byte, err error) {
|
||||
cmd := []string{goTool(), "tool", "compile", "-e", "-D", "test", "-I", "."}
|
||||
if pkgname == "main" {
|
||||
cmd = append(cmd, "-p=main")
|
||||
} else {
|
||||
pkgname = path.Join("test", strings.TrimSuffix(names[0], ".go"))
|
||||
cmd = append(cmd, "-o", pkgname+".a", "-p", pkgname)
|
||||
}
|
||||
cmd = append(cmd, flags...)
|
||||
if *linkshared {
|
||||
@ -615,7 +614,6 @@ func (t *test) run() {
|
||||
wantError := false
|
||||
wantAuto := false
|
||||
singlefilepkgs := false
|
||||
localImports := true
|
||||
f, err := splitQuoted(action)
|
||||
if err != nil {
|
||||
t.err = fmt.Errorf("invalid test recipe: %v", err)
|
||||
@ -659,12 +657,6 @@ func (t *test) run() {
|
||||
wantError = false
|
||||
case "-s":
|
||||
singlefilepkgs = true
|
||||
case "-n":
|
||||
// Do not set relative path for local imports to current dir,
|
||||
// e.g. do not pass -D . -I . to the compiler.
|
||||
// Used in fixedbugs/bug345.go to allow compilation and import of local pkg.
|
||||
// See golang.org/issue/25635
|
||||
localImports = false
|
||||
case "-t": // timeout in seconds
|
||||
args = args[1:]
|
||||
var err error
|
||||
@ -886,7 +878,7 @@ func (t *test) run() {
|
||||
return
|
||||
}
|
||||
for _, pkg := range pkgs {
|
||||
_, t.err = compileInDir(runcmd, longdir, flags, localImports, pkg.name, pkg.files...)
|
||||
_, t.err = compileInDir(runcmd, longdir, flags, pkg.name, pkg.files...)
|
||||
if t.err != nil {
|
||||
return
|
||||
}
|
||||
@ -910,7 +902,7 @@ func (t *test) run() {
|
||||
errPkg--
|
||||
}
|
||||
for i, pkg := range pkgs {
|
||||
out, err := compileInDir(runcmd, longdir, flags, localImports, pkg.name, pkg.files...)
|
||||
out, err := compileInDir(runcmd, longdir, flags, pkg.name, pkg.files...)
|
||||
if i == errPkg {
|
||||
if wantError && err == nil {
|
||||
t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
|
||||
@ -959,7 +951,7 @@ func (t *test) run() {
|
||||
}
|
||||
|
||||
for i, pkg := range pkgs {
|
||||
_, err := compileInDir(runcmd, longdir, flags, localImports, pkg.name, pkg.files...)
|
||||
_, err := compileInDir(runcmd, longdir, flags, pkg.name, pkg.files...)
|
||||
// Allow this package compilation fail based on conditions below;
|
||||
// its errors were checked in previous case.
|
||||
if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
|
||||
@ -1283,6 +1275,10 @@ func (t *test) makeTempDir() {
|
||||
if *keep {
|
||||
log.Printf("Temporary directory is %s", t.tempDir)
|
||||
}
|
||||
err = os.Mkdir(filepath.Join(t.tempDir, "test"), 0o755)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// checkExpectedOutput compares the output from compiling and/or running with the contents
|
||||
|
Loading…
x
Reference in New Issue
Block a user