libgo: update to final Go 1.8 release
[official-gcc.git] / libgo / go / cmd / go / go_test.go
blob56de65ce55ae3d65e6bf0db6d7538a2d022572fa
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package main_test
7 import (
8 "bytes"
9 "fmt"
10 "go/build"
11 "go/format"
12 "internal/race"
13 "internal/testenv"
14 "io"
15 "io/ioutil"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "regexp"
20 "runtime"
21 "strconv"
22 "strings"
23 "testing"
24 "time"
27 var (
28 canRun = true // whether we can run go or ./testgo
29 canRace = false // whether we can run the race detector
30 canCgo = false // whether we can use cgo
32 exeSuffix string // ".exe" on Windows
34 skipExternal = false // skip external tests
37 func init() {
38 switch runtime.GOOS {
39 case "android", "nacl":
40 canRun = false
41 case "darwin":
42 switch runtime.GOARCH {
43 case "arm", "arm64":
44 canRun = false
46 case "linux":
47 switch runtime.GOARCH {
48 case "arm":
49 // many linux/arm machines are too slow to run
50 // the full set of external tests.
51 skipExternal = true
52 case "mips", "mipsle", "mips64", "mips64le":
53 // Also slow.
54 skipExternal = true
55 if testenv.Builder() != "" {
56 // On the builders, skip the cmd/go
57 // tests. They're too slow and already
58 // covered by other ports. There's
59 // nothing os/arch specific in the
60 // tests.
61 canRun = false
64 case "freebsd":
65 switch runtime.GOARCH {
66 case "arm":
67 // many freebsd/arm machines are too slow to run
68 // the full set of external tests.
69 skipExternal = true
70 canRun = false
72 case "windows":
73 exeSuffix = ".exe"
77 // The TestMain function creates a go command for testing purposes and
78 // deletes it after the tests have been run.
79 func TestMain(m *testing.M) {
80 if canRun {
81 args := []string{"build", "-tags", "testgo", "-o", "testgo" + exeSuffix}
82 if race.Enabled {
83 args = append(args, "-race")
85 out, err := exec.Command("go", args...).CombinedOutput()
86 if err != nil {
87 fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out)
88 os.Exit(2)
91 if out, err := exec.Command("./testgo"+exeSuffix, "env", "CGO_ENABLED").Output(); err != nil {
92 fmt.Fprintf(os.Stderr, "running testgo failed: %v\n", err)
93 canRun = false
94 } else {
95 canCgo, err = strconv.ParseBool(strings.TrimSpace(string(out)))
96 if err != nil {
97 fmt.Fprintf(os.Stderr, "can't parse go env CGO_ENABLED output: %v\n", strings.TrimSpace(string(out)))
101 switch runtime.GOOS {
102 case "linux", "darwin", "freebsd", "windows":
103 canRace = canCgo && runtime.GOARCH == "amd64"
107 // Don't let these environment variables confuse the test.
108 os.Unsetenv("GOBIN")
109 os.Unsetenv("GOPATH")
110 os.Unsetenv("GIT_ALLOW_PROTOCOL")
111 if home, ccacheDir := os.Getenv("HOME"), os.Getenv("CCACHE_DIR"); home != "" && ccacheDir == "" {
112 // On some systems the default C compiler is ccache.
113 // Setting HOME to a non-existent directory will break
114 // those systems. Set CCACHE_DIR to cope. Issue 17668.
115 os.Setenv("CCACHE_DIR", filepath.Join(home, ".ccache"))
117 os.Setenv("HOME", "/test-go-home-does-not-exist")
119 r := m.Run()
121 if canRun {
122 os.Remove("testgo" + exeSuffix)
125 os.Exit(r)
128 // The length of an mtime tick on this system. This is an estimate of
129 // how long we need to sleep to ensure that the mtime of two files is
130 // different.
131 // We used to try to be clever but that didn't always work (see golang.org/issue/12205).
132 var mtimeTick time.Duration = 1 * time.Second
134 // Manage a single run of the testgo binary.
135 type testgoData struct {
136 t *testing.T
137 temps []string
138 wd string
139 env []string
140 tempdir string
141 ran bool
142 inParallel bool
143 stdout, stderr bytes.Buffer
146 // testgo sets up for a test that runs testgo.
147 func testgo(t *testing.T) *testgoData {
148 testenv.MustHaveGoBuild(t)
150 if skipExternal {
151 t.Skip("skipping external tests on %s/%s", runtime.GOOS, runtime.GOARCH)
154 return &testgoData{t: t}
157 // must gives a fatal error if err is not nil.
158 func (tg *testgoData) must(err error) {
159 if err != nil {
160 tg.t.Fatal(err)
164 // check gives a test non-fatal error if err is not nil.
165 func (tg *testgoData) check(err error) {
166 if err != nil {
167 tg.t.Error(err)
171 // parallel runs the test in parallel by calling t.Parallel.
172 func (tg *testgoData) parallel() {
173 if tg.ran {
174 tg.t.Fatal("internal testsuite error: call to parallel after run")
176 if tg.wd != "" {
177 tg.t.Fatal("internal testsuite error: call to parallel after cd")
179 for _, e := range tg.env {
180 if strings.HasPrefix(e, "GOROOT=") || strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
181 val := e[strings.Index(e, "=")+1:]
182 if strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata") {
183 tg.t.Fatalf("internal testsuite error: call to parallel with testdata in environment (%s)", e)
187 tg.inParallel = true
188 tg.t.Parallel()
191 // pwd returns the current directory.
192 func (tg *testgoData) pwd() string {
193 wd, err := os.Getwd()
194 if err != nil {
195 tg.t.Fatalf("could not get working directory: %v", err)
197 return wd
200 // cd changes the current directory to the named directory. Note that
201 // using this means that the test must not be run in parallel with any
202 // other tests.
203 func (tg *testgoData) cd(dir string) {
204 if tg.inParallel {
205 tg.t.Fatal("internal testsuite error: changing directory when running in parallel")
207 if tg.wd == "" {
208 tg.wd = tg.pwd()
210 abs, err := filepath.Abs(dir)
211 tg.must(os.Chdir(dir))
212 if err == nil {
213 tg.setenv("PWD", abs)
217 // sleep sleeps for one tick, where a tick is a conservative estimate
218 // of how long it takes for a file modification to get a different
219 // mtime.
220 func (tg *testgoData) sleep() {
221 time.Sleep(mtimeTick)
224 // setenv sets an environment variable to use when running the test go
225 // command.
226 func (tg *testgoData) setenv(name, val string) {
227 if tg.inParallel && (name == "GOROOT" || name == "GOPATH" || name == "GOBIN") && (strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata")) {
228 tg.t.Fatalf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val)
230 tg.unsetenv(name)
231 tg.env = append(tg.env, name+"="+val)
234 // unsetenv removes an environment variable.
235 func (tg *testgoData) unsetenv(name string) {
236 if tg.env == nil {
237 tg.env = append([]string(nil), os.Environ()...)
239 for i, v := range tg.env {
240 if strings.HasPrefix(v, name+"=") {
241 tg.env = append(tg.env[:i], tg.env[i+1:]...)
242 break
247 // doRun runs the test go command, recording stdout and stderr and
248 // returning exit status.
249 func (tg *testgoData) doRun(args []string) error {
250 if !canRun {
251 panic("testgoData.doRun called but canRun false")
253 if tg.inParallel {
254 for _, arg := range args {
255 if strings.HasPrefix(arg, "testdata") || strings.HasPrefix(arg, "./testdata") {
256 tg.t.Fatal("internal testsuite error: parallel run using testdata")
260 tg.t.Logf("running testgo %v", args)
261 var prog string
262 if tg.wd == "" {
263 prog = "./testgo" + exeSuffix
264 } else {
265 prog = filepath.Join(tg.wd, "testgo"+exeSuffix)
267 cmd := exec.Command(prog, args...)
268 tg.stdout.Reset()
269 tg.stderr.Reset()
270 cmd.Stdout = &tg.stdout
271 cmd.Stderr = &tg.stderr
272 cmd.Env = tg.env
273 status := cmd.Run()
274 if tg.stdout.Len() > 0 {
275 tg.t.Log("standard output:")
276 tg.t.Log(tg.stdout.String())
278 if tg.stderr.Len() > 0 {
279 tg.t.Log("standard error:")
280 tg.t.Log(tg.stderr.String())
282 tg.ran = true
283 return status
286 // run runs the test go command, and expects it to succeed.
287 func (tg *testgoData) run(args ...string) {
288 if status := tg.doRun(args); status != nil {
289 tg.t.Logf("go %v failed unexpectedly: %v", args, status)
290 tg.t.FailNow()
294 // runFail runs the test go command, and expects it to fail.
295 func (tg *testgoData) runFail(args ...string) {
296 if status := tg.doRun(args); status == nil {
297 tg.t.Fatal("testgo succeeded unexpectedly")
298 } else {
299 tg.t.Log("testgo failed as expected:", status)
303 // runGit runs a git command, and expects it to succeed.
304 func (tg *testgoData) runGit(dir string, args ...string) {
305 cmd := exec.Command("git", args...)
306 tg.stdout.Reset()
307 tg.stderr.Reset()
308 cmd.Stdout = &tg.stdout
309 cmd.Stderr = &tg.stderr
310 cmd.Dir = dir
311 cmd.Env = tg.env
312 status := cmd.Run()
313 if tg.stdout.Len() > 0 {
314 tg.t.Log("git standard output:")
315 tg.t.Log(tg.stdout.String())
317 if tg.stderr.Len() > 0 {
318 tg.t.Log("git standard error:")
319 tg.t.Log(tg.stderr.String())
321 if status != nil {
322 tg.t.Logf("git %v failed unexpectedly: %v", args, status)
323 tg.t.FailNow()
327 // getStdout returns standard output of the testgo run as a string.
328 func (tg *testgoData) getStdout() string {
329 if !tg.ran {
330 tg.t.Fatal("internal testsuite error: stdout called before run")
332 return tg.stdout.String()
335 // getStderr returns standard error of the testgo run as a string.
336 func (tg *testgoData) getStderr() string {
337 if !tg.ran {
338 tg.t.Fatal("internal testsuite error: stdout called before run")
340 return tg.stderr.String()
343 // doGrepMatch looks for a regular expression in a buffer, and returns
344 // whether it is found. The regular expression is matched against
345 // each line separately, as with the grep command.
346 func (tg *testgoData) doGrepMatch(match string, b *bytes.Buffer) bool {
347 if !tg.ran {
348 tg.t.Fatal("internal testsuite error: grep called before run")
350 re := regexp.MustCompile(match)
351 for _, ln := range bytes.Split(b.Bytes(), []byte{'\n'}) {
352 if re.Match(ln) {
353 return true
356 return false
359 // doGrep looks for a regular expression in a buffer and fails if it
360 // is not found. The name argument is the name of the output we are
361 // searching, "output" or "error". The msg argument is logged on
362 // failure.
363 func (tg *testgoData) doGrep(match string, b *bytes.Buffer, name, msg string) {
364 if !tg.doGrepMatch(match, b) {
365 tg.t.Log(msg)
366 tg.t.Logf("pattern %v not found in standard %s", match, name)
367 tg.t.FailNow()
371 // grepStdout looks for a regular expression in the test run's
372 // standard output and fails, logging msg, if it is not found.
373 func (tg *testgoData) grepStdout(match, msg string) {
374 tg.doGrep(match, &tg.stdout, "output", msg)
377 // grepStderr looks for a regular expression in the test run's
378 // standard error and fails, logging msg, if it is not found.
379 func (tg *testgoData) grepStderr(match, msg string) {
380 tg.doGrep(match, &tg.stderr, "error", msg)
383 // grepBoth looks for a regular expression in the test run's standard
384 // output or stand error and fails, logging msg, if it is not found.
385 func (tg *testgoData) grepBoth(match, msg string) {
386 if !tg.doGrepMatch(match, &tg.stdout) && !tg.doGrepMatch(match, &tg.stderr) {
387 tg.t.Log(msg)
388 tg.t.Logf("pattern %v not found in standard output or standard error", match)
389 tg.t.FailNow()
393 // doGrepNot looks for a regular expression in a buffer and fails if
394 // it is found. The name and msg arguments are as for doGrep.
395 func (tg *testgoData) doGrepNot(match string, b *bytes.Buffer, name, msg string) {
396 if tg.doGrepMatch(match, b) {
397 tg.t.Log(msg)
398 tg.t.Logf("pattern %v found unexpectedly in standard %s", match, name)
399 tg.t.FailNow()
403 // grepStdoutNot looks for a regular expression in the test run's
404 // standard output and fails, logging msg, if it is found.
405 func (tg *testgoData) grepStdoutNot(match, msg string) {
406 tg.doGrepNot(match, &tg.stdout, "output", msg)
409 // grepStderrNot looks for a regular expression in the test run's
410 // standard error and fails, logging msg, if it is found.
411 func (tg *testgoData) grepStderrNot(match, msg string) {
412 tg.doGrepNot(match, &tg.stderr, "error", msg)
415 // grepBothNot looks for a regular expression in the test run's
416 // standard output or stand error and fails, logging msg, if it is
417 // found.
418 func (tg *testgoData) grepBothNot(match, msg string) {
419 if tg.doGrepMatch(match, &tg.stdout) || tg.doGrepMatch(match, &tg.stderr) {
420 tg.t.Log(msg)
421 tg.t.Fatalf("pattern %v found unexpectedly in standard output or standard error", match)
425 // doGrepCount counts the number of times a regexp is seen in a buffer.
426 func (tg *testgoData) doGrepCount(match string, b *bytes.Buffer) int {
427 if !tg.ran {
428 tg.t.Fatal("internal testsuite error: doGrepCount called before run")
430 re := regexp.MustCompile(match)
431 c := 0
432 for _, ln := range bytes.Split(b.Bytes(), []byte{'\n'}) {
433 if re.Match(ln) {
437 return c
440 // grepCountBoth returns the number of times a regexp is seen in both
441 // standard output and standard error.
442 func (tg *testgoData) grepCountBoth(match string) int {
443 return tg.doGrepCount(match, &tg.stdout) + tg.doGrepCount(match, &tg.stderr)
446 // creatingTemp records that the test plans to create a temporary file
447 // or directory. If the file or directory exists already, it will be
448 // removed. When the test completes, the file or directory will be
449 // removed if it exists.
450 func (tg *testgoData) creatingTemp(path string) {
451 if filepath.IsAbs(path) && !strings.HasPrefix(path, tg.tempdir) {
452 tg.t.Fatalf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path)
454 // If we have changed the working directory, make sure we have
455 // an absolute path, because we are going to change directory
456 // back before we remove the temporary.
457 if tg.wd != "" && !filepath.IsAbs(path) {
458 path = filepath.Join(tg.pwd(), path)
460 tg.must(os.RemoveAll(path))
461 tg.temps = append(tg.temps, path)
464 // makeTempdir makes a temporary directory for a run of testgo. If
465 // the temporary directory was already created, this does nothing.
466 func (tg *testgoData) makeTempdir() {
467 if tg.tempdir == "" {
468 var err error
469 tg.tempdir, err = ioutil.TempDir("", "gotest")
470 tg.must(err)
474 // tempFile adds a temporary file for a run of testgo.
475 func (tg *testgoData) tempFile(path, contents string) {
476 tg.makeTempdir()
477 tg.must(os.MkdirAll(filepath.Join(tg.tempdir, filepath.Dir(path)), 0755))
478 bytes := []byte(contents)
479 if strings.HasSuffix(path, ".go") {
480 formatted, err := format.Source(bytes)
481 if err == nil {
482 bytes = formatted
485 tg.must(ioutil.WriteFile(filepath.Join(tg.tempdir, path), bytes, 0644))
488 // tempDir adds a temporary directory for a run of testgo.
489 func (tg *testgoData) tempDir(path string) {
490 tg.makeTempdir()
491 if err := os.MkdirAll(filepath.Join(tg.tempdir, path), 0755); err != nil && !os.IsExist(err) {
492 tg.t.Fatal(err)
496 // path returns the absolute pathname to file with the temporary
497 // directory.
498 func (tg *testgoData) path(name string) string {
499 if tg.tempdir == "" {
500 tg.t.Fatalf("internal testsuite error: path(%q) with no tempdir", name)
502 if name == "." {
503 return tg.tempdir
505 return filepath.Join(tg.tempdir, name)
508 // mustExist fails if path does not exist.
509 func (tg *testgoData) mustExist(path string) {
510 if _, err := os.Stat(path); err != nil {
511 if os.IsNotExist(err) {
512 tg.t.Fatalf("%s does not exist but should", path)
514 tg.t.Fatalf("%s stat failed: %v", path, err)
518 // mustNotExist fails if path exists.
519 func (tg *testgoData) mustNotExist(path string) {
520 if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {
521 tg.t.Fatalf("%s exists but should not (%v)", path, err)
525 // wantExecutable fails with msg if path is not executable.
526 func (tg *testgoData) wantExecutable(path, msg string) {
527 if st, err := os.Stat(path); err != nil {
528 if !os.IsNotExist(err) {
529 tg.t.Log(err)
531 tg.t.Fatal(msg)
532 } else {
533 if runtime.GOOS != "windows" && st.Mode()&0111 == 0 {
534 tg.t.Fatalf("binary %s exists but is not executable", path)
539 // wantArchive fails if path is not an archive.
540 func (tg *testgoData) wantArchive(path string) {
541 f, err := os.Open(path)
542 if err != nil {
543 tg.t.Fatal(err)
545 buf := make([]byte, 100)
546 io.ReadFull(f, buf)
547 f.Close()
548 if !bytes.HasPrefix(buf, []byte("!<arch>\n")) {
549 tg.t.Fatalf("file %s exists but is not an archive", path)
553 // isStale reports whether pkg is stale, and why
554 func (tg *testgoData) isStale(pkg string) (bool, string) {
555 tg.run("list", "-f", "{{.Stale}}:{{.StaleReason}}", pkg)
556 v := strings.TrimSpace(tg.getStdout())
557 f := strings.SplitN(v, ":", 2)
558 if len(f) == 2 {
559 switch f[0] {
560 case "true":
561 return true, f[1]
562 case "false":
563 return false, f[1]
566 tg.t.Fatalf("unexpected output checking staleness of package %v: %v", pkg, v)
567 panic("unreachable")
570 // wantStale fails with msg if pkg is not stale.
571 func (tg *testgoData) wantStale(pkg, reason, msg string) {
572 stale, why := tg.isStale(pkg)
573 if !stale {
574 tg.t.Fatal(msg)
576 if reason == "" && why != "" || !strings.Contains(why, reason) {
577 tg.t.Errorf("wrong reason for Stale=true: %q, want %q", why, reason)
581 // wantNotStale fails with msg if pkg is stale.
582 func (tg *testgoData) wantNotStale(pkg, reason, msg string) {
583 stale, why := tg.isStale(pkg)
584 if stale {
585 tg.t.Fatal(msg)
587 if reason == "" && why != "" || !strings.Contains(why, reason) {
588 tg.t.Errorf("wrong reason for Stale=false: %q, want %q", why, reason)
592 // cleanup cleans up a test that runs testgo.
593 func (tg *testgoData) cleanup() {
594 if tg.wd != "" {
595 if err := os.Chdir(tg.wd); err != nil {
596 // We are unlikely to be able to continue.
597 fmt.Fprintln(os.Stderr, "could not restore working directory, crashing:", err)
598 os.Exit(2)
601 for _, path := range tg.temps {
602 tg.check(os.RemoveAll(path))
604 if tg.tempdir != "" {
605 tg.check(os.RemoveAll(tg.tempdir))
609 // failSSH puts an ssh executable in the PATH that always fails.
610 // This is to stub out uses of ssh by go get.
611 func (tg *testgoData) failSSH() {
612 wd, err := os.Getwd()
613 if err != nil {
614 tg.t.Fatal(err)
616 fail := filepath.Join(wd, "testdata/failssh")
617 tg.setenv("PATH", fmt.Sprintf("%v%c%v", fail, filepath.ListSeparator, os.Getenv("PATH")))
620 func TestFileLineInErrorMessages(t *testing.T) {
621 tg := testgo(t)
622 defer tg.cleanup()
623 tg.parallel()
624 tg.tempFile("err.go", `package main; import "bar"`)
625 path := tg.path("err.go")
626 tg.runFail("run", path)
627 shortPath := path
628 if rel, err := filepath.Rel(tg.pwd(), path); err == nil && len(rel) < len(path) {
629 shortPath = rel
631 tg.grepStderr("^"+regexp.QuoteMeta(shortPath)+":", "missing file:line in error message")
634 func TestProgramNameInCrashMessages(t *testing.T) {
635 tg := testgo(t)
636 defer tg.cleanup()
637 tg.parallel()
638 tg.tempFile("triv.go", `package main; func main() {}`)
639 tg.runFail("build", "-ldflags", "-crash_for_testing", tg.path("triv.go"))
640 tg.grepStderr(`[/\\]tool[/\\].*[/\\]link`, "missing linker name in error message")
643 func TestBrokenTestsWithoutTestFunctionsAllFail(t *testing.T) {
644 tg := testgo(t)
645 defer tg.cleanup()
646 // TODO: tg.parallel()
647 tg.runFail("test", "./testdata/src/badtest/...")
648 tg.grepBothNot("^ok", "test passed unexpectedly")
649 tg.grepBoth("FAIL.*badtest/badexec", "test did not run everything")
650 tg.grepBoth("FAIL.*badtest/badsyntax", "test did not run everything")
651 tg.grepBoth("FAIL.*badtest/badvar", "test did not run everything")
654 func TestGoBuildDashAInDevBranch(t *testing.T) {
655 if testing.Short() {
656 t.Skip("don't rebuild the standard library in short mode")
659 tg := testgo(t)
660 defer tg.cleanup()
661 tg.run("install", "math") // should be up to date already but just in case
662 tg.setenv("TESTGO_IS_GO_RELEASE", "0")
663 tg.run("build", "-v", "-a", "math")
664 tg.grepStderr("runtime", "testgo build -a math in dev branch DID NOT build runtime, but should have")
666 // Everything is out of date. Rebuild to leave things in a better state.
667 tg.run("install", "std")
670 func TestGoBuildDashAInReleaseBranch(t *testing.T) {
671 if testing.Short() {
672 t.Skip("don't rebuild the standard library in short mode")
675 tg := testgo(t)
676 defer tg.cleanup()
677 tg.run("install", "math", "net/http") // should be up to date already but just in case
678 tg.setenv("TESTGO_IS_GO_RELEASE", "1")
679 tg.run("install", "-v", "-a", "math")
680 tg.grepStderr("runtime", "testgo build -a math in release branch DID NOT build runtime, but should have")
682 // Now runtime.a is updated (newer mtime), so everything would look stale if not for being a release.
683 tg.run("build", "-v", "net/http")
684 tg.grepStderrNot("strconv", "testgo build -v net/http in release branch with newer runtime.a DID build strconv but should not have")
685 tg.grepStderrNot("golang.org/x/net/http2/hpack", "testgo build -v net/http in release branch with newer runtime.a DID build .../golang.org/x/net/http2/hpack but should not have")
686 tg.grepStderrNot("net/http", "testgo build -v net/http in release branch with newer runtime.a DID build net/http but should not have")
688 // Everything is out of date. Rebuild to leave things in a better state.
689 tg.run("install", "std")
692 func TestNewReleaseRebuildsStalePackagesInGOPATH(t *testing.T) {
693 if testing.Short() {
694 t.Skip("don't rebuild the standard library in short mode")
697 tg := testgo(t)
698 defer tg.cleanup()
700 addNL := func(name string) (restore func()) {
701 data, err := ioutil.ReadFile(name)
702 if err != nil {
703 t.Fatal(err)
705 old := data
706 data = append(data, '\n')
707 if err := ioutil.WriteFile(name, append(data, '\n'), 0666); err != nil {
708 t.Fatal(err)
710 tg.sleep()
711 return func() {
712 if err := ioutil.WriteFile(name, old, 0666); err != nil {
713 t.Fatal(err)
718 tg.setenv("TESTGO_IS_GO_RELEASE", "1")
720 tg.tempFile("d1/src/p1/p1.go", `package p1`)
721 tg.setenv("GOPATH", tg.path("d1"))
722 tg.run("install", "-a", "p1")
723 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale, incorrectly")
724 tg.sleep()
726 // Changing mtime and content of runtime/internal/sys/sys.go
727 // should have no effect: we're in a release, which doesn't rebuild
728 // for general mtime or content changes.
729 sys := runtime.GOROOT() + "/src/runtime/internal/sys/sys.go"
730 restore := addNL(sys)
731 defer restore()
732 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale, incorrectly, after updating runtime/internal/sys/sys.go")
733 restore()
734 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale, incorrectly, after restoring runtime/internal/sys/sys.go")
736 // But changing runtime/internal/sys/zversion.go should have an effect:
737 // that's how we tell when we flip from one release to another.
738 zversion := runtime.GOROOT() + "/src/runtime/internal/sys/zversion.go"
739 restore = addNL(zversion)
740 defer restore()
741 tg.wantStale("p1", "build ID mismatch", "./testgo list claims p1 is NOT stale, incorrectly, after changing to new release")
742 restore()
743 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale, incorrectly, after changing back to old release")
744 addNL(zversion)
745 tg.wantStale("p1", "build ID mismatch", "./testgo list claims p1 is NOT stale, incorrectly, after changing again to new release")
746 tg.run("install", "p1")
747 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale after building with new release")
749 // Restore to "old" release.
750 restore()
751 tg.wantStale("p1", "build ID mismatch", "./testgo list claims p1 is NOT stale, incorrectly, after changing to old release after new build")
752 tg.run("install", "p1")
753 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale after building with old release")
755 // Everything is out of date. Rebuild to leave things in a better state.
756 tg.run("install", "std")
759 func TestGoListStandard(t *testing.T) {
760 tg := testgo(t)
761 defer tg.cleanup()
762 // TODO: tg.parallel()
763 tg.cd(runtime.GOROOT() + "/src")
764 tg.run("list", "-f", "{{if not .Standard}}{{.ImportPath}}{{end}}", "./...")
765 stdout := tg.getStdout()
766 for _, line := range strings.Split(stdout, "\n") {
767 if strings.HasPrefix(line, "_/") && strings.HasSuffix(line, "/src") {
768 // $GOROOT/src shows up if there are any .go files there.
769 // We don't care.
770 continue
772 if line == "" {
773 continue
775 t.Errorf("package in GOROOT not listed as standard: %v", line)
778 // Similarly, expanding std should include some of our vendored code.
779 tg.run("list", "std", "cmd")
780 tg.grepStdout("golang.org/x/net/http2/hpack", "list std cmd did not mention vendored hpack")
781 tg.grepStdout("golang.org/x/arch/x86/x86asm", "list std cmd did not mention vendored x86asm")
784 func TestGoInstallCleansUpAfterGoBuild(t *testing.T) {
785 tg := testgo(t)
786 defer tg.cleanup()
787 // TODO: tg.parallel()
788 tg.tempFile("src/mycmd/main.go", `package main; func main(){}`)
789 tg.setenv("GOPATH", tg.path("."))
790 tg.cd(tg.path("src/mycmd"))
792 doesNotExist := func(file, msg string) {
793 if _, err := os.Stat(file); err == nil {
794 t.Fatal(msg)
795 } else if !os.IsNotExist(err) {
796 t.Fatal(msg, "error:", err)
800 tg.run("build")
801 tg.wantExecutable("mycmd"+exeSuffix, "testgo build did not write command binary")
802 tg.run("install")
803 doesNotExist("mycmd"+exeSuffix, "testgo install did not remove command binary")
804 tg.run("build")
805 tg.wantExecutable("mycmd"+exeSuffix, "testgo build did not write command binary (second time)")
806 // Running install with arguments does not remove the target,
807 // even in the same directory.
808 tg.run("install", "mycmd")
809 tg.wantExecutable("mycmd"+exeSuffix, "testgo install mycmd removed command binary when run in mycmd")
810 tg.run("build")
811 tg.wantExecutable("mycmd"+exeSuffix, "testgo build did not write command binary (third time)")
812 // And especially not outside the directory.
813 tg.cd(tg.path("."))
814 if data, err := ioutil.ReadFile("src/mycmd/mycmd" + exeSuffix); err != nil {
815 t.Fatal("could not read file:", err)
816 } else {
817 if err := ioutil.WriteFile("mycmd"+exeSuffix, data, 0555); err != nil {
818 t.Fatal("could not write file:", err)
821 tg.run("install", "mycmd")
822 tg.wantExecutable("src/mycmd/mycmd"+exeSuffix, "testgo install mycmd removed command binary from its source dir when run outside mycmd")
823 tg.wantExecutable("mycmd"+exeSuffix, "testgo install mycmd removed command binary from current dir when run outside mycmd")
826 func TestGoInstallRebuildsStalePackagesInOtherGOPATH(t *testing.T) {
827 tg := testgo(t)
828 defer tg.cleanup()
829 tg.parallel()
830 tg.tempFile("d1/src/p1/p1.go", `package p1
831 import "p2"
832 func F() { p2.F() }`)
833 tg.tempFile("d2/src/p2/p2.go", `package p2
834 func F() {}`)
835 sep := string(filepath.ListSeparator)
836 tg.setenv("GOPATH", tg.path("d1")+sep+tg.path("d2"))
837 tg.run("install", "p1")
838 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale, incorrectly")
839 tg.wantNotStale("p2", "", "./testgo list claims p2 is stale, incorrectly")
840 tg.sleep()
841 if f, err := os.OpenFile(tg.path("d2/src/p2/p2.go"), os.O_WRONLY|os.O_APPEND, 0); err != nil {
842 t.Fatal(err)
843 } else if _, err = f.WriteString(`func G() {}`); err != nil {
844 t.Fatal(err)
845 } else {
846 tg.must(f.Close())
848 tg.wantStale("p2", "newer source file", "./testgo list claims p2 is NOT stale, incorrectly")
849 tg.wantStale("p1", "stale dependency", "./testgo list claims p1 is NOT stale, incorrectly")
851 tg.run("install", "p1")
852 tg.wantNotStale("p2", "", "./testgo list claims p2 is stale after reinstall, incorrectly")
853 tg.wantNotStale("p1", "", "./testgo list claims p1 is stale after reinstall, incorrectly")
856 func TestGoInstallDetectsRemovedFiles(t *testing.T) {
857 tg := testgo(t)
858 defer tg.cleanup()
859 tg.parallel()
860 tg.tempFile("src/mypkg/x.go", `package mypkg`)
861 tg.tempFile("src/mypkg/y.go", `package mypkg`)
862 tg.tempFile("src/mypkg/z.go", `// +build missingtag
864 package mypkg`)
865 tg.setenv("GOPATH", tg.path("."))
866 tg.run("install", "mypkg")
867 tg.wantNotStale("mypkg", "", "./testgo list mypkg claims mypkg is stale, incorrectly")
868 // z.go was not part of the build; removing it is okay.
869 tg.must(os.Remove(tg.path("src/mypkg/z.go")))
870 tg.wantNotStale("mypkg", "", "./testgo list mypkg claims mypkg is stale after removing z.go; should not be stale")
871 // y.go was part of the package; removing it should be detected.
872 tg.must(os.Remove(tg.path("src/mypkg/y.go")))
873 tg.wantStale("mypkg", "build ID mismatch", "./testgo list mypkg claims mypkg is NOT stale after removing y.go; should be stale")
876 func TestWildcardMatchesSyntaxErrorDirs(t *testing.T) {
877 tg := testgo(t)
878 defer tg.cleanup()
879 // TODO: tg.parallel()
880 tg.tempFile("src/mypkg/x.go", `package mypkg`)
881 tg.tempFile("src/mypkg/y.go", `pkg mypackage`)
882 tg.setenv("GOPATH", tg.path("."))
883 tg.cd(tg.path("src/mypkg"))
884 tg.runFail("list", "./...")
885 tg.runFail("build", "./...")
886 tg.runFail("install", "./...")
889 func TestGoListWithTags(t *testing.T) {
890 tg := testgo(t)
891 defer tg.cleanup()
892 tg.tempFile("src/mypkg/x.go", "// +build thetag\n\npackage mypkg\n")
893 tg.setenv("GOPATH", tg.path("."))
894 tg.cd(tg.path("./src"))
895 tg.run("list", "-tags=thetag", "./my...")
896 tg.grepStdout("mypkg", "did not find mypkg")
899 func TestGoInstallErrorOnCrossCompileToBin(t *testing.T) {
900 if testing.Short() {
901 t.Skip("don't install into GOROOT in short mode")
904 tg := testgo(t)
905 defer tg.cleanup()
906 tg.tempFile("src/mycmd/x.go", `package main
907 func main() {}`)
908 tg.setenv("GOPATH", tg.path("."))
909 tg.cd(tg.path("src/mycmd"))
911 tg.run("build", "mycmd")
913 goarch := "386"
914 if runtime.GOARCH == "386" {
915 goarch = "amd64"
917 tg.setenv("GOOS", "linux")
918 tg.setenv("GOARCH", goarch)
919 tg.run("install", "mycmd")
920 tg.setenv("GOBIN", tg.path("."))
921 tg.runFail("install", "mycmd")
922 tg.run("install", "cmd/pack")
925 func TestGoInstallDetectsRemovedFilesInPackageMain(t *testing.T) {
926 tg := testgo(t)
927 defer tg.cleanup()
928 tg.parallel()
929 tg.tempFile("src/mycmd/x.go", `package main
930 func main() {}`)
931 tg.tempFile("src/mycmd/y.go", `package main`)
932 tg.tempFile("src/mycmd/z.go", `// +build missingtag
934 package main`)
935 tg.setenv("GOPATH", tg.path("."))
936 tg.run("install", "mycmd")
937 tg.wantNotStale("mycmd", "", "./testgo list mypkg claims mycmd is stale, incorrectly")
938 // z.go was not part of the build; removing it is okay.
939 tg.must(os.Remove(tg.path("src/mycmd/z.go")))
940 tg.wantNotStale("mycmd", "", "./testgo list mycmd claims mycmd is stale after removing z.go; should not be stale")
941 // y.go was part of the package; removing it should be detected.
942 tg.must(os.Remove(tg.path("src/mycmd/y.go")))
943 tg.wantStale("mycmd", "build ID mismatch", "./testgo list mycmd claims mycmd is NOT stale after removing y.go; should be stale")
946 func testLocalRun(tg *testgoData, exepath, local, match string) {
947 out, err := exec.Command(exepath).Output()
948 if err != nil {
949 tg.t.Fatalf("error running %v: %v", exepath, err)
951 if !regexp.MustCompile(match).Match(out) {
952 tg.t.Log(string(out))
953 tg.t.Errorf("testdata/%s/easy.go did not generate expected output", local)
957 func testLocalEasy(tg *testgoData, local string) {
958 exepath := "./easy" + exeSuffix
959 tg.creatingTemp(exepath)
960 tg.run("build", "-o", exepath, filepath.Join("testdata", local, "easy.go"))
961 testLocalRun(tg, exepath, local, `(?m)^easysub\.Hello`)
964 func testLocalEasySub(tg *testgoData, local string) {
965 exepath := "./easysub" + exeSuffix
966 tg.creatingTemp(exepath)
967 tg.run("build", "-o", exepath, filepath.Join("testdata", local, "easysub", "main.go"))
968 testLocalRun(tg, exepath, local, `(?m)^easysub\.Hello`)
971 func testLocalHard(tg *testgoData, local string) {
972 exepath := "./hard" + exeSuffix
973 tg.creatingTemp(exepath)
974 tg.run("build", "-o", exepath, filepath.Join("testdata", local, "hard.go"))
975 testLocalRun(tg, exepath, local, `(?m)^sub\.Hello`)
978 func testLocalInstall(tg *testgoData, local string) {
979 tg.runFail("install", filepath.Join("testdata", local, "easy.go"))
982 func TestLocalImportsEasy(t *testing.T) {
983 tg := testgo(t)
984 defer tg.cleanup()
985 testLocalEasy(tg, "local")
988 func TestLocalImportsEasySub(t *testing.T) {
989 tg := testgo(t)
990 defer tg.cleanup()
991 testLocalEasySub(tg, "local")
994 func TestLocalImportsHard(t *testing.T) {
995 tg := testgo(t)
996 defer tg.cleanup()
997 testLocalHard(tg, "local")
1000 func TestLocalImportsGoInstallShouldFail(t *testing.T) {
1001 tg := testgo(t)
1002 defer tg.cleanup()
1003 testLocalInstall(tg, "local")
1006 const badDirName = `#$%:, &()*;<=>?\^{}`
1008 func copyBad(tg *testgoData) {
1009 if runtime.GOOS == "windows" {
1010 tg.t.Skipf("skipping test because %q is an invalid directory name", badDirName)
1013 tg.must(filepath.Walk("testdata/local",
1014 func(path string, info os.FileInfo, err error) error {
1015 if err != nil {
1016 return err
1018 if info.IsDir() {
1019 return nil
1021 var data []byte
1022 data, err = ioutil.ReadFile(path)
1023 if err != nil {
1024 return err
1026 newpath := strings.Replace(path, "local", badDirName, 1)
1027 tg.tempFile(newpath, string(data))
1028 return nil
1030 tg.cd(tg.path("."))
1033 func TestBadImportsEasy(t *testing.T) {
1034 tg := testgo(t)
1035 defer tg.cleanup()
1036 // TODO: tg.parallel()
1037 copyBad(tg)
1038 testLocalEasy(tg, badDirName)
1041 func TestBadImportsEasySub(t *testing.T) {
1042 tg := testgo(t)
1043 defer tg.cleanup()
1044 copyBad(tg)
1045 testLocalEasySub(tg, badDirName)
1048 func TestBadImportsHard(t *testing.T) {
1049 tg := testgo(t)
1050 defer tg.cleanup()
1051 copyBad(tg)
1052 testLocalHard(tg, badDirName)
1055 func TestBadImportsGoInstallShouldFail(t *testing.T) {
1056 tg := testgo(t)
1057 defer tg.cleanup()
1058 copyBad(tg)
1059 testLocalInstall(tg, badDirName)
1062 func TestInternalPackagesInGOROOTAreRespected(t *testing.T) {
1063 tg := testgo(t)
1064 defer tg.cleanup()
1065 tg.runFail("build", "-v", "./testdata/testinternal")
1066 tg.grepBoth(`testinternal(\/|\\)p\.go\:3\:8\: use of internal package not allowed`, "wrong error message for testdata/testinternal")
1069 func TestInternalPackagesOutsideGOROOTAreRespected(t *testing.T) {
1070 tg := testgo(t)
1071 defer tg.cleanup()
1072 tg.runFail("build", "-v", "./testdata/testinternal2")
1073 tg.grepBoth(`testinternal2(\/|\\)p\.go\:3\:8\: use of internal package not allowed`, "wrote error message for testdata/testinternal2")
1076 func TestRunInternal(t *testing.T) {
1077 tg := testgo(t)
1078 defer tg.cleanup()
1079 dir := filepath.Join(tg.pwd(), "testdata")
1080 tg.setenv("GOPATH", dir)
1081 tg.run("run", filepath.Join(dir, "src/run/good.go"))
1082 tg.runFail("run", filepath.Join(dir, "src/run/bad.go"))
1083 tg.grepStderr(`testdata(\/|\\)src(\/|\\)run(\/|\\)bad\.go\:3\:8\: use of internal package not allowed`, "unexpected error for run/bad.go")
1086 func testMove(t *testing.T, vcs, url, base, config string) {
1087 testenv.MustHaveExternalNetwork(t)
1089 tg := testgo(t)
1090 defer tg.cleanup()
1091 tg.parallel()
1092 tg.tempDir("src")
1093 tg.setenv("GOPATH", tg.path("."))
1094 tg.run("get", "-d", url)
1095 tg.run("get", "-d", "-u", url)
1096 switch vcs {
1097 case "svn":
1098 // SVN doesn't believe in text files so we can't just edit the config.
1099 // Check out a different repo into the wrong place.
1100 tg.must(os.RemoveAll(tg.path("src/code.google.com/p/rsc-svn")))
1101 tg.run("get", "-d", "-u", "code.google.com/p/rsc-svn2/trunk")
1102 tg.must(os.Rename(tg.path("src/code.google.com/p/rsc-svn2"), tg.path("src/code.google.com/p/rsc-svn")))
1103 default:
1104 path := tg.path(filepath.Join("src", config))
1105 data, err := ioutil.ReadFile(path)
1106 tg.must(err)
1107 data = bytes.Replace(data, []byte(base), []byte(base+"XXX"), -1)
1108 tg.must(ioutil.WriteFile(path, data, 0644))
1110 if vcs == "git" {
1111 // git will ask for a username and password when we
1112 // run go get -d -f -u. An empty username and
1113 // password will work. Prevent asking by setting
1114 // GIT_ASKPASS.
1115 tg.creatingTemp("sink" + exeSuffix)
1116 tg.tempFile("src/sink/sink.go", `package main; func main() {}`)
1117 tg.run("build", "-o", "sink"+exeSuffix, "sink")
1118 tg.setenv("GIT_ASKPASS", filepath.Join(tg.pwd(), "sink"+exeSuffix))
1120 tg.runFail("get", "-d", "-u", url)
1121 tg.grepStderr("is a custom import path for", "go get -d -u "+url+" failed for wrong reason")
1122 tg.runFail("get", "-d", "-f", "-u", url)
1123 tg.grepStderr("validating server certificate|not found", "go get -d -f -u "+url+" failed for wrong reason")
1126 func TestInternalPackageErrorsAreHandled(t *testing.T) {
1127 tg := testgo(t)
1128 defer tg.cleanup()
1129 tg.run("list", "./testdata/testinternal3")
1132 func TestInternalCache(t *testing.T) {
1133 tg := testgo(t)
1134 defer tg.cleanup()
1135 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/testinternal4"))
1136 tg.runFail("build", "p")
1137 tg.grepStderr("internal", "did not fail to build p")
1140 func TestMoveGit(t *testing.T) {
1141 testMove(t, "git", "rsc.io/pdf", "pdf", "rsc.io/pdf/.git/config")
1144 // TODO(rsc): Set up a test case on bitbucket for hg.
1145 // func TestMoveHG(t *testing.T) {
1146 // testMove(t, "hg", "rsc.io/x86/x86asm", "x86", "rsc.io/x86/.hg/hgrc")
1147 // }
1149 // TODO(rsc): Set up a test case on SourceForge (?) for svn.
1150 // func testMoveSVN(t *testing.T) {
1151 // testMove(t, "svn", "code.google.com/p/rsc-svn/trunk", "-", "-")
1152 // }
1154 func TestImportCommandMatch(t *testing.T) {
1155 tg := testgo(t)
1156 defer tg.cleanup()
1157 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/importcom"))
1158 tg.run("build", "./testdata/importcom/works.go")
1161 func TestImportCommentMismatch(t *testing.T) {
1162 tg := testgo(t)
1163 defer tg.cleanup()
1164 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/importcom"))
1165 tg.runFail("build", "./testdata/importcom/wrongplace.go")
1166 tg.grepStderr(`wrongplace expects import "my/x"`, "go build did not mention incorrect import")
1169 func TestImportCommentSyntaxError(t *testing.T) {
1170 tg := testgo(t)
1171 defer tg.cleanup()
1172 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/importcom"))
1173 tg.runFail("build", "./testdata/importcom/bad.go")
1174 tg.grepStderr("cannot parse import comment", "go build did not mention syntax error")
1177 func TestImportCommentConflict(t *testing.T) {
1178 tg := testgo(t)
1179 defer tg.cleanup()
1180 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/importcom"))
1181 tg.runFail("build", "./testdata/importcom/conflict.go")
1182 tg.grepStderr("found import comments", "go build did not mention comment conflict")
1185 // cmd/go: custom import path checking should not apply to Go packages without import comment.
1186 func TestIssue10952(t *testing.T) {
1187 testenv.MustHaveExternalNetwork(t)
1188 if _, err := exec.LookPath("git"); err != nil {
1189 t.Skip("skipping because git binary not found")
1192 tg := testgo(t)
1193 defer tg.cleanup()
1194 tg.parallel()
1195 tg.tempDir("src")
1196 tg.setenv("GOPATH", tg.path("."))
1197 const importPath = "github.com/zombiezen/go-get-issue-10952"
1198 tg.run("get", "-d", "-u", importPath)
1199 repoDir := tg.path("src/" + importPath)
1200 tg.runGit(repoDir, "remote", "set-url", "origin", "https://"+importPath+".git")
1201 tg.run("get", "-d", "-u", importPath)
1204 func TestIssue16471(t *testing.T) {
1205 testenv.MustHaveExternalNetwork(t)
1206 if _, err := exec.LookPath("git"); err != nil {
1207 t.Skip("skipping because git binary not found")
1210 tg := testgo(t)
1211 defer tg.cleanup()
1212 tg.parallel()
1213 tg.tempDir("src")
1214 tg.setenv("GOPATH", tg.path("."))
1215 tg.must(os.MkdirAll(tg.path("src/rsc.io/go-get-issue-10952"), 0755))
1216 tg.runGit(tg.path("src/rsc.io"), "clone", "https://github.com/zombiezen/go-get-issue-10952")
1217 tg.runFail("get", "-u", "rsc.io/go-get-issue-10952")
1218 tg.grepStderr("rsc.io/go-get-issue-10952 is a custom import path for https://github.com/rsc/go-get-issue-10952, but .* is checked out from https://github.com/zombiezen/go-get-issue-10952", "did not detect updated import path")
1221 // Test git clone URL that uses SCP-like syntax and custom import path checking.
1222 func TestIssue11457(t *testing.T) {
1223 testenv.MustHaveExternalNetwork(t)
1224 if _, err := exec.LookPath("git"); err != nil {
1225 t.Skip("skipping because git binary not found")
1228 tg := testgo(t)
1229 defer tg.cleanup()
1230 tg.parallel()
1231 tg.tempDir("src")
1232 tg.setenv("GOPATH", tg.path("."))
1233 const importPath = "rsc.io/go-get-issue-11457"
1234 tg.run("get", "-d", "-u", importPath)
1235 repoDir := tg.path("src/" + importPath)
1236 tg.runGit(repoDir, "remote", "set-url", "origin", "git@github.com:rsc/go-get-issue-11457")
1238 // At this time, custom import path checking compares remotes verbatim (rather than
1239 // just the host and path, skipping scheme and user), so we expect go get -u to fail.
1240 // However, the goal of this test is to verify that gitRemoteRepo correctly parsed
1241 // the SCP-like syntax, and we expect it to appear in the error message.
1242 tg.runFail("get", "-d", "-u", importPath)
1243 want := " is checked out from ssh://git@github.com/rsc/go-get-issue-11457"
1244 if !strings.HasSuffix(strings.TrimSpace(tg.getStderr()), want) {
1245 t.Error("expected clone URL to appear in stderr")
1249 func TestGetGitDefaultBranch(t *testing.T) {
1250 testenv.MustHaveExternalNetwork(t)
1251 if _, err := exec.LookPath("git"); err != nil {
1252 t.Skip("skipping because git binary not found")
1255 tg := testgo(t)
1256 defer tg.cleanup()
1257 tg.parallel()
1258 tg.tempDir("src")
1259 tg.setenv("GOPATH", tg.path("."))
1261 // This repo has two branches, master and another-branch.
1262 // The another-branch is the default that you get from 'git clone'.
1263 // The go get command variants should not override this.
1264 const importPath = "github.com/rsc/go-get-default-branch"
1266 tg.run("get", "-d", importPath)
1267 repoDir := tg.path("src/" + importPath)
1268 tg.runGit(repoDir, "branch", "--contains", "HEAD")
1269 tg.grepStdout(`\* another-branch`, "not on correct default branch")
1271 tg.run("get", "-d", "-u", importPath)
1272 tg.runGit(repoDir, "branch", "--contains", "HEAD")
1273 tg.grepStdout(`\* another-branch`, "not on correct default branch")
1276 func TestErrorMessageForSyntaxErrorInTestGoFileSaysFAIL(t *testing.T) {
1277 tg := testgo(t)
1278 defer tg.cleanup()
1279 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1280 tg.runFail("test", "syntaxerror")
1281 tg.grepStderr("FAIL", "go test did not say FAIL")
1284 func TestWildcardsDoNotLookInUselessDirectories(t *testing.T) {
1285 tg := testgo(t)
1286 defer tg.cleanup()
1287 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1288 tg.runFail("list", "...")
1289 tg.grepBoth("badpkg", "go list ... failure does not mention badpkg")
1290 tg.run("list", "m...")
1293 func TestRelativeImportsGoTest(t *testing.T) {
1294 tg := testgo(t)
1295 defer tg.cleanup()
1296 tg.run("test", "./testdata/testimport")
1299 func TestRelativeImportsGoTestDashI(t *testing.T) {
1300 tg := testgo(t)
1301 defer tg.cleanup()
1302 tg.run("test", "-i", "./testdata/testimport")
1305 func TestRelativeImportsInCommandLinePackage(t *testing.T) {
1306 tg := testgo(t)
1307 defer tg.cleanup()
1308 // TODO: tg.parallel()
1309 files, err := filepath.Glob("./testdata/testimport/*.go")
1310 tg.must(err)
1311 tg.run(append([]string{"test"}, files...)...)
1314 func TestNonCanonicalImportPaths(t *testing.T) {
1315 tg := testgo(t)
1316 defer tg.cleanup()
1317 tg.parallel()
1318 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1319 tg.runFail("build", "canonical/d")
1320 tg.grepStderr("package canonical/d", "did not report canonical/d")
1321 tg.grepStderr("imports canonical/b", "did not report canonical/b")
1322 tg.grepStderr("imports canonical/a/: non-canonical", "did not report canonical/a/")
1325 func TestVersionControlErrorMessageIncludesCorrectDirectory(t *testing.T) {
1326 tg := testgo(t)
1327 defer tg.cleanup()
1328 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/shadow/root1"))
1329 tg.runFail("get", "-u", "foo")
1331 // TODO(iant): We should not have to use strconv.Quote here.
1332 // The code in vcs.go should be changed so that it is not required.
1333 quoted := strconv.Quote(filepath.Join("testdata", "shadow", "root1", "src", "foo"))
1334 quoted = quoted[1 : len(quoted)-1]
1336 tg.grepStderr(regexp.QuoteMeta(quoted), "go get -u error does not mention shadow/root1/src/foo")
1339 func TestInstallFailsWithNoBuildableFiles(t *testing.T) {
1340 tg := testgo(t)
1341 defer tg.cleanup()
1342 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1343 tg.setenv("CGO_ENABLED", "0")
1344 tg.runFail("install", "cgotest")
1345 tg.grepStderr("no buildable Go source files", "go install cgotest did not report 'no buildable Go Source files'")
1348 func TestRelativeGOBINFail(t *testing.T) {
1349 tg := testgo(t)
1350 defer tg.cleanup()
1351 tg.tempFile("triv.go", `package main; func main() {}`)
1352 tg.setenv("GOBIN", ".")
1353 tg.runFail("install")
1354 tg.grepStderr("cannot install, GOBIN must be an absolute path", "go install must fail if $GOBIN is a relative path")
1357 // Test that without $GOBIN set, binaries get installed
1358 // into the GOPATH bin directory.
1359 func TestInstallIntoGOPATH(t *testing.T) {
1360 tg := testgo(t)
1361 defer tg.cleanup()
1362 tg.creatingTemp("testdata/bin/go-cmd-test" + exeSuffix)
1363 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1364 tg.run("install", "go-cmd-test")
1365 tg.wantExecutable("testdata/bin/go-cmd-test"+exeSuffix, "go install go-cmd-test did not write to testdata/bin/go-cmd-test")
1368 // Issue 12407
1369 func TestBuildOutputToDevNull(t *testing.T) {
1370 tg := testgo(t)
1371 defer tg.cleanup()
1372 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1373 tg.run("build", "-o", os.DevNull, "go-cmd-test")
1376 func TestPackageMainTestImportsArchiveNotBinary(t *testing.T) {
1377 tg := testgo(t)
1378 defer tg.cleanup()
1379 tg.parallel()
1380 gobin := filepath.Join(tg.pwd(), "testdata", "bin")
1381 tg.creatingTemp(gobin)
1382 tg.setenv("GOBIN", gobin)
1383 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1384 tg.must(os.Chtimes("./testdata/src/main_test/m.go", time.Now(), time.Now()))
1385 tg.sleep()
1386 tg.run("test", "main_test")
1387 tg.run("install", "main_test")
1388 tg.wantNotStale("main_test", "", "after go install, main listed as stale")
1389 tg.run("test", "main_test")
1392 // The runtime version string takes one of two forms:
1393 // "go1.X[.Y]" for Go releases, and "devel +hash" at tip.
1394 // Determine whether we are in a released copy by
1395 // inspecting the version.
1396 var isGoRelease = strings.HasPrefix(runtime.Version(), "go1")
1398 // Issue 12690
1399 func TestPackageNotStaleWithTrailingSlash(t *testing.T) {
1400 tg := testgo(t)
1401 defer tg.cleanup()
1403 // Make sure the packages below are not stale.
1404 tg.run("install", "runtime", "os", "io")
1406 goroot := runtime.GOROOT()
1407 tg.setenv("GOROOT", goroot+"/")
1409 want := ""
1410 if isGoRelease {
1411 want = "standard package in Go release distribution"
1414 tg.wantNotStale("runtime", want, "with trailing slash in GOROOT, runtime listed as stale")
1415 tg.wantNotStale("os", want, "with trailing slash in GOROOT, os listed as stale")
1416 tg.wantNotStale("io", want, "with trailing slash in GOROOT, io listed as stale")
1419 // With $GOBIN set, binaries get installed to $GOBIN.
1420 func TestInstallIntoGOBIN(t *testing.T) {
1421 tg := testgo(t)
1422 defer tg.cleanup()
1423 gobin := filepath.Join(tg.pwd(), "testdata", "bin1")
1424 tg.creatingTemp(gobin)
1425 tg.setenv("GOBIN", gobin)
1426 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1427 tg.run("install", "go-cmd-test")
1428 tg.wantExecutable("testdata/bin1/go-cmd-test"+exeSuffix, "go install go-cmd-test did not write to testdata/bin1/go-cmd-test")
1431 // Issue 11065
1432 func TestInstallToCurrentDirectoryCreatesExecutable(t *testing.T) {
1433 tg := testgo(t)
1434 defer tg.cleanup()
1435 pkg := filepath.Join(tg.pwd(), "testdata", "src", "go-cmd-test")
1436 tg.creatingTemp(filepath.Join(pkg, "go-cmd-test"+exeSuffix))
1437 tg.setenv("GOBIN", pkg)
1438 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1439 tg.cd(pkg)
1440 tg.run("install")
1441 tg.wantExecutable("go-cmd-test"+exeSuffix, "go install did not write to current directory")
1444 // Without $GOBIN set, installing a program outside $GOPATH should fail
1445 // (there is nowhere to install it).
1446 func TestInstallWithoutDestinationFails(t *testing.T) {
1447 tg := testgo(t)
1448 defer tg.cleanup()
1449 tg.runFail("install", "testdata/src/go-cmd-test/helloworld.go")
1450 tg.grepStderr("no install location for .go files listed on command line", "wrong error")
1453 // With $GOBIN set, should install there.
1454 func TestInstallToGOBINCommandLinePackage(t *testing.T) {
1455 tg := testgo(t)
1456 defer tg.cleanup()
1457 gobin := filepath.Join(tg.pwd(), "testdata", "bin1")
1458 tg.creatingTemp(gobin)
1459 tg.setenv("GOBIN", gobin)
1460 tg.run("install", "testdata/src/go-cmd-test/helloworld.go")
1461 tg.wantExecutable("testdata/bin1/helloworld"+exeSuffix, "go install testdata/src/go-cmd-test/helloworld.go did not write testdata/bin1/helloworld")
1464 func TestGoGetNonPkg(t *testing.T) {
1465 testenv.MustHaveExternalNetwork(t)
1467 tg := testgo(t)
1468 defer tg.cleanup()
1469 tg.tempDir("gobin")
1470 tg.setenv("GOPATH", tg.path("."))
1471 tg.setenv("GOBIN", tg.path("gobin"))
1472 tg.runFail("get", "-d", "golang.org/x/tools")
1473 tg.grepStderr("golang.org/x/tools: no buildable Go source files", "missing error")
1474 tg.runFail("get", "-d", "-u", "golang.org/x/tools")
1475 tg.grepStderr("golang.org/x/tools: no buildable Go source files", "missing error")
1476 tg.runFail("get", "-d", "golang.org/x/tools")
1477 tg.grepStderr("golang.org/x/tools: no buildable Go source files", "missing error")
1480 func TestGoGetTestOnlyPkg(t *testing.T) {
1481 testenv.MustHaveExternalNetwork(t)
1483 tg := testgo(t)
1484 defer tg.cleanup()
1485 tg.tempDir("gopath")
1486 tg.setenv("GOPATH", tg.path("gopath"))
1487 tg.run("get", "golang.org/x/tour/content")
1488 tg.run("get", "-t", "golang.org/x/tour/content")
1491 func TestInstalls(t *testing.T) {
1492 if testing.Short() {
1493 t.Skip("don't install into GOROOT in short mode")
1496 tg := testgo(t)
1497 defer tg.cleanup()
1498 tg.parallel()
1499 tg.tempDir("gobin")
1500 tg.setenv("GOPATH", tg.path("."))
1501 goroot := runtime.GOROOT()
1502 tg.setenv("GOROOT", goroot)
1504 // cmd/fix installs into tool
1505 tg.run("env", "GOOS")
1506 goos := strings.TrimSpace(tg.getStdout())
1507 tg.setenv("GOOS", goos)
1508 tg.run("env", "GOARCH")
1509 goarch := strings.TrimSpace(tg.getStdout())
1510 tg.setenv("GOARCH", goarch)
1511 fixbin := filepath.Join(goroot, "pkg", "tool", goos+"_"+goarch, "fix") + exeSuffix
1512 tg.must(os.RemoveAll(fixbin))
1513 tg.run("install", "cmd/fix")
1514 tg.wantExecutable(fixbin, "did not install cmd/fix to $GOROOT/pkg/tool")
1515 tg.must(os.Remove(fixbin))
1516 tg.setenv("GOBIN", tg.path("gobin"))
1517 tg.run("install", "cmd/fix")
1518 tg.wantExecutable(fixbin, "did not install cmd/fix to $GOROOT/pkg/tool with $GOBIN set")
1519 tg.unsetenv("GOBIN")
1521 // gopath program installs into GOBIN
1522 tg.tempFile("src/progname/p.go", `package main; func main() {}`)
1523 tg.setenv("GOBIN", tg.path("gobin"))
1524 tg.run("install", "progname")
1525 tg.unsetenv("GOBIN")
1526 tg.wantExecutable(tg.path("gobin/progname")+exeSuffix, "did not install progname to $GOBIN/progname")
1528 // gopath program installs into GOPATH/bin
1529 tg.run("install", "progname")
1530 tg.wantExecutable(tg.path("bin/progname")+exeSuffix, "did not install progname to $GOPATH/bin/progname")
1533 func TestRejectRelativeDotPathInGOPATHCommandLinePackage(t *testing.T) {
1534 tg := testgo(t)
1535 defer tg.cleanup()
1536 tg.setenv("GOPATH", ".")
1537 tg.runFail("build", "testdata/src/go-cmd-test/helloworld.go")
1538 tg.grepStderr("GOPATH entry is relative", "expected an error message rejecting relative GOPATH entries")
1541 func TestRejectRelativePathsInGOPATH(t *testing.T) {
1542 tg := testgo(t)
1543 defer tg.cleanup()
1544 sep := string(filepath.ListSeparator)
1545 tg.setenv("GOPATH", sep+filepath.Join(tg.pwd(), "testdata")+sep+".")
1546 tg.runFail("build", "go-cmd-test")
1547 tg.grepStderr("GOPATH entry is relative", "expected an error message rejecting relative GOPATH entries")
1550 func TestRejectRelativePathsInGOPATHCommandLinePackage(t *testing.T) {
1551 tg := testgo(t)
1552 defer tg.cleanup()
1553 tg.setenv("GOPATH", "testdata")
1554 tg.runFail("build", "testdata/src/go-cmd-test/helloworld.go")
1555 tg.grepStderr("GOPATH entry is relative", "expected an error message rejecting relative GOPATH entries")
1558 // Issue 4104.
1559 func TestGoTestWithPackageListedMultipleTimes(t *testing.T) {
1560 tg := testgo(t)
1561 defer tg.cleanup()
1562 tg.parallel()
1563 tg.run("test", "errors", "errors", "errors", "errors", "errors")
1564 if strings.Contains(strings.TrimSpace(tg.getStdout()), "\n") {
1565 t.Error("go test errors errors errors errors errors tested the same package multiple times")
1569 func TestGoListHasAConsistentOrder(t *testing.T) {
1570 tg := testgo(t)
1571 defer tg.cleanup()
1572 tg.parallel()
1573 tg.run("list", "std")
1574 first := tg.getStdout()
1575 tg.run("list", "std")
1576 if first != tg.getStdout() {
1577 t.Error("go list std ordering is inconsistent")
1581 func TestGoListStdDoesNotIncludeCommands(t *testing.T) {
1582 tg := testgo(t)
1583 defer tg.cleanup()
1584 tg.parallel()
1585 tg.run("list", "std")
1586 tg.grepStdoutNot("cmd/", "go list std shows commands")
1589 func TestGoListCmdOnlyShowsCommands(t *testing.T) {
1590 tg := testgo(t)
1591 defer tg.cleanup()
1592 tg.parallel()
1593 tg.run("list", "cmd")
1594 out := strings.TrimSpace(tg.getStdout())
1595 for _, line := range strings.Split(out, "\n") {
1596 if !strings.Contains(line, "cmd/") {
1597 t.Error("go list cmd shows non-commands")
1598 break
1603 func TestGoListDedupsPackages(t *testing.T) {
1604 tg := testgo(t)
1605 defer tg.cleanup()
1606 // TODO: tg.parallel()
1607 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
1608 tg.run("list", "xtestonly", "./testdata/src/xtestonly/...")
1609 got := strings.TrimSpace(tg.getStdout())
1610 const want = "xtestonly"
1611 if got != want {
1612 t.Errorf("got %q; want %q", got, want)
1616 // Issue 4096. Validate the output of unsuccessful go install foo/quxx.
1617 func TestUnsuccessfulGoInstallShouldMentionMissingPackage(t *testing.T) {
1618 tg := testgo(t)
1619 defer tg.cleanup()
1620 tg.parallel()
1621 tg.runFail("install", "foo/quxx")
1622 if tg.grepCountBoth(`cannot find package "foo/quxx" in any of`) != 1 {
1623 t.Error(`go install foo/quxx expected error: .*cannot find package "foo/quxx" in any of`)
1627 func TestGOROOTSearchFailureReporting(t *testing.T) {
1628 tg := testgo(t)
1629 defer tg.cleanup()
1630 tg.parallel()
1631 tg.runFail("install", "foo/quxx")
1632 if tg.grepCountBoth(regexp.QuoteMeta(filepath.Join("foo", "quxx"))+` \(from \$GOROOT\)$`) != 1 {
1633 t.Error(`go install foo/quxx expected error: .*foo/quxx (from $GOROOT)`)
1637 func TestMultipleGOPATHEntriesReportedSeparately(t *testing.T) {
1638 tg := testgo(t)
1639 defer tg.cleanup()
1640 tg.parallel()
1641 sep := string(filepath.ListSeparator)
1642 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata", "a")+sep+filepath.Join(tg.pwd(), "testdata", "b"))
1643 tg.runFail("install", "foo/quxx")
1644 if tg.grepCountBoth(`testdata[/\\].[/\\]src[/\\]foo[/\\]quxx`) != 2 {
1645 t.Error(`go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)\n.*testdata/b/src/foo/quxx`)
1649 // Test (from $GOPATH) annotation is reported for the first GOPATH entry,
1650 func TestMentionGOPATHInFirstGOPATHEntry(t *testing.T) {
1651 tg := testgo(t)
1652 defer tg.cleanup()
1653 tg.parallel()
1654 sep := string(filepath.ListSeparator)
1655 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata", "a")+sep+filepath.Join(tg.pwd(), "testdata", "b"))
1656 tg.runFail("install", "foo/quxx")
1657 if tg.grepCountBoth(regexp.QuoteMeta(filepath.Join("testdata", "a", "src", "foo", "quxx"))+` \(from \$GOPATH\)$`) != 1 {
1658 t.Error(`go install foo/quxx expected error: .*testdata/a/src/foo/quxx (from $GOPATH)`)
1662 // but not on the second.
1663 func TestMentionGOPATHNotOnSecondEntry(t *testing.T) {
1664 tg := testgo(t)
1665 defer tg.cleanup()
1666 tg.parallel()
1667 sep := string(filepath.ListSeparator)
1668 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata", "a")+sep+filepath.Join(tg.pwd(), "testdata", "b"))
1669 tg.runFail("install", "foo/quxx")
1670 if tg.grepCountBoth(regexp.QuoteMeta(filepath.Join("testdata", "b", "src", "foo", "quxx"))+`$`) != 1 {
1671 t.Error(`go install foo/quxx expected error: .*testdata/b/src/foo/quxx`)
1675 func homeEnvName() string {
1676 switch runtime.GOOS {
1677 case "windows":
1678 return "USERPROFILE"
1679 case "plan9":
1680 return "home"
1681 default:
1682 return "HOME"
1686 func TestDefaultGOPATH(t *testing.T) {
1687 tg := testgo(t)
1688 defer tg.cleanup()
1689 tg.parallel()
1690 tg.tempDir("home/go")
1691 tg.setenv(homeEnvName(), tg.path("home"))
1693 tg.run("env", "GOPATH")
1694 tg.grepStdout(regexp.QuoteMeta(tg.path("home/go")), "want GOPATH=$HOME/go")
1696 tg.setenv("GOROOT", tg.path("home/go"))
1697 tg.run("env", "GOPATH")
1698 tg.grepStdoutNot(".", "want unset GOPATH because GOROOT=$HOME/go")
1700 tg.setenv("GOROOT", tg.path("home/go")+"/")
1701 tg.run("env", "GOPATH")
1702 tg.grepStdoutNot(".", "want unset GOPATH because GOROOT=$HOME/go/")
1705 func TestDefaultGOPATHGet(t *testing.T) {
1706 testenv.MustHaveExternalNetwork(t)
1708 tg := testgo(t)
1709 defer tg.cleanup()
1710 tg.setenv("GOPATH", "")
1711 tg.tempDir("home")
1712 tg.setenv(homeEnvName(), tg.path("home"))
1714 // warn for creating directory
1715 tg.run("get", "-v", "github.com/golang/example/hello")
1716 tg.grepStderr("created GOPATH="+regexp.QuoteMeta(tg.path("home/go"))+"; see 'go help gopath'", "did not create GOPATH")
1718 // no warning if directory already exists
1719 tg.must(os.RemoveAll(tg.path("home/go")))
1720 tg.tempDir("home/go")
1721 tg.run("get", "github.com/golang/example/hello")
1722 tg.grepStderrNot(".", "expected no output on standard error")
1724 // error if $HOME/go is a file
1725 tg.must(os.RemoveAll(tg.path("home/go")))
1726 tg.tempFile("home/go", "")
1727 tg.runFail("get", "github.com/golang/example/hello")
1728 tg.grepStderr(`mkdir .*[/\\]go: .*(not a directory|cannot find the path)`, "expected error because $HOME/go is a file")
1731 func TestDefaultGOPATHPrintedSearchList(t *testing.T) {
1732 tg := testgo(t)
1733 defer tg.cleanup()
1734 tg.setenv("GOPATH", "")
1735 tg.tempDir("home")
1736 tg.setenv(homeEnvName(), tg.path("home"))
1738 tg.runFail("install", "github.com/golang/example/hello")
1739 tg.grepStderr(regexp.QuoteMeta(tg.path("home/go/src/github.com/golang/example/hello"))+`.*from \$GOPATH`, "expected default GOPATH")
1742 // Issue 4186. go get cannot be used to download packages to $GOROOT.
1743 // Test that without GOPATH set, go get should fail.
1744 func TestGoGetIntoGOROOT(t *testing.T) {
1745 testenv.MustHaveExternalNetwork(t)
1747 tg := testgo(t)
1748 defer tg.cleanup()
1749 tg.parallel()
1750 tg.tempDir("src")
1752 // Fails because GOROOT=GOPATH
1753 tg.setenv("GOPATH", tg.path("."))
1754 tg.setenv("GOROOT", tg.path("."))
1755 tg.runFail("get", "-d", "github.com/golang/example/hello")
1756 tg.grepStderr("warning: GOPATH set to GOROOT", "go should detect GOPATH=GOROOT")
1757 tg.grepStderr(`\$GOPATH must not be set to \$GOROOT`, "go should detect GOPATH=GOROOT")
1759 // Fails because GOROOT=GOPATH after cleaning.
1760 tg.setenv("GOPATH", tg.path(".")+"/")
1761 tg.setenv("GOROOT", tg.path("."))
1762 tg.runFail("get", "-d", "github.com/golang/example/hello")
1763 tg.grepStderr("warning: GOPATH set to GOROOT", "go should detect GOPATH=GOROOT")
1764 tg.grepStderr(`\$GOPATH must not be set to \$GOROOT`, "go should detect GOPATH=GOROOT")
1766 tg.setenv("GOPATH", tg.path("."))
1767 tg.setenv("GOROOT", tg.path(".")+"/")
1768 tg.runFail("get", "-d", "github.com/golang/example/hello")
1769 tg.grepStderr("warning: GOPATH set to GOROOT", "go should detect GOPATH=GOROOT")
1770 tg.grepStderr(`\$GOPATH must not be set to \$GOROOT`, "go should detect GOPATH=GOROOT")
1772 // Fails because GOROOT=$HOME/go so default GOPATH unset.
1773 tg.tempDir("home/go")
1774 tg.setenv(homeEnvName(), tg.path("home"))
1775 tg.setenv("GOPATH", "")
1776 tg.setenv("GOROOT", tg.path("home/go"))
1777 tg.runFail("get", "-d", "github.com/golang/example/hello")
1778 tg.grepStderr(`\$GOPATH not set`, "expected GOPATH not set")
1780 tg.setenv(homeEnvName(), tg.path("home")+"/")
1781 tg.setenv("GOPATH", "")
1782 tg.setenv("GOROOT", tg.path("home/go"))
1783 tg.runFail("get", "-d", "github.com/golang/example/hello")
1784 tg.grepStderr(`\$GOPATH not set`, "expected GOPATH not set")
1786 tg.setenv(homeEnvName(), tg.path("home"))
1787 tg.setenv("GOPATH", "")
1788 tg.setenv("GOROOT", tg.path("home/go")+"/")
1789 tg.runFail("get", "-d", "github.com/golang/example/hello")
1790 tg.grepStderr(`\$GOPATH not set`, "expected GOPATH not set")
1793 func TestLdflagsArgumentsWithSpacesIssue3941(t *testing.T) {
1794 tg := testgo(t)
1795 defer tg.cleanup()
1796 tg.parallel()
1797 tg.tempFile("main.go", `package main
1798 var extern string
1799 func main() {
1800 println(extern)
1802 tg.run("run", "-ldflags", `-X "main.extern=hello world"`, tg.path("main.go"))
1803 tg.grepStderr("^hello world", `ldflags -X "main.extern=hello world"' failed`)
1806 func TestGoTestCpuprofileLeavesBinaryBehind(t *testing.T) {
1807 tg := testgo(t)
1808 defer tg.cleanup()
1809 // TODO: tg.parallel()
1810 tg.makeTempdir()
1811 tg.cd(tg.path("."))
1812 tg.run("test", "-cpuprofile", "errors.prof", "errors")
1813 tg.wantExecutable("errors.test"+exeSuffix, "go test -cpuprofile did not create errors.test")
1816 func TestGoTestCpuprofileDashOControlsBinaryLocation(t *testing.T) {
1817 tg := testgo(t)
1818 defer tg.cleanup()
1819 // TODO: tg.parallel()
1820 tg.makeTempdir()
1821 tg.cd(tg.path("."))
1822 tg.run("test", "-cpuprofile", "errors.prof", "-o", "myerrors.test"+exeSuffix, "errors")
1823 tg.wantExecutable("myerrors.test"+exeSuffix, "go test -cpuprofile -o myerrors.test did not create myerrors.test")
1826 func TestGoTestMutexprofileLeavesBinaryBehind(t *testing.T) {
1827 tg := testgo(t)
1828 defer tg.cleanup()
1829 // TODO: tg.parallel()
1830 tg.makeTempdir()
1831 tg.cd(tg.path("."))
1832 tg.run("test", "-mutexprofile", "errors.prof", "errors")
1833 tg.wantExecutable("errors.test"+exeSuffix, "go test -mutexprofile did not create errors.test")
1836 func TestGoTestMutexprofileDashOControlsBinaryLocation(t *testing.T) {
1837 tg := testgo(t)
1838 defer tg.cleanup()
1839 // TODO: tg.parallel()
1840 tg.makeTempdir()
1841 tg.cd(tg.path("."))
1842 tg.run("test", "-mutexprofile", "errors.prof", "-o", "myerrors.test"+exeSuffix, "errors")
1843 tg.wantExecutable("myerrors.test"+exeSuffix, "go test -mutexprofile -o myerrors.test did not create myerrors.test")
1846 func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
1847 tg := testgo(t)
1848 defer tg.cleanup()
1849 tg.parallel()
1850 tg.makeTempdir()
1851 tg.run("test", "-c", "-o", tg.path("myerrors.test"+exeSuffix), "errors")
1852 tg.wantExecutable(tg.path("myerrors.test"+exeSuffix), "go test -c -o myerrors.test did not create myerrors.test")
1855 func TestGoTestDashOWritesBinary(t *testing.T) {
1856 tg := testgo(t)
1857 defer tg.cleanup()
1858 tg.parallel()
1859 tg.makeTempdir()
1860 tg.run("test", "-o", tg.path("myerrors.test"+exeSuffix), "errors")
1861 tg.wantExecutable(tg.path("myerrors.test"+exeSuffix), "go test -o myerrors.test did not create myerrors.test")
1864 func TestGoTestDashIDashOWritesBinary(t *testing.T) {
1865 tg := testgo(t)
1866 defer tg.cleanup()
1867 tg.parallel()
1868 tg.makeTempdir()
1869 tg.run("test", "-v", "-i", "-o", tg.path("myerrors.test"+exeSuffix), "errors")
1870 tg.grepBothNot("PASS|FAIL", "test should not have run")
1871 tg.wantExecutable(tg.path("myerrors.test"+exeSuffix), "go test -o myerrors.test did not create myerrors.test")
1874 // Issue 4568.
1875 func TestSymlinksList(t *testing.T) {
1876 switch runtime.GOOS {
1877 case "plan9", "windows":
1878 t.Skipf("skipping symlink test on %s", runtime.GOOS)
1881 tg := testgo(t)
1882 defer tg.cleanup()
1883 // TODO: tg.parallel()
1884 tg.tempDir("src")
1885 tg.must(os.Symlink(tg.path("."), tg.path("src/dir1")))
1886 tg.tempFile("src/dir1/p.go", "package p")
1887 tg.setenv("GOPATH", tg.path("."))
1888 tg.cd(tg.path("src"))
1889 tg.run("list", "-f", "{{.Root}}", "dir1")
1890 if strings.TrimSpace(tg.getStdout()) != tg.path(".") {
1891 t.Error("confused by symlinks")
1895 // Issue 14054.
1896 func TestSymlinksVendor(t *testing.T) {
1897 switch runtime.GOOS {
1898 case "plan9", "windows":
1899 t.Skipf("skipping symlink test on %s", runtime.GOOS)
1902 tg := testgo(t)
1903 defer tg.cleanup()
1904 // TODO: tg.parallel()
1905 tg.tempDir("gopath/src/dir1/vendor/v")
1906 tg.tempFile("gopath/src/dir1/p.go", "package main\nimport _ `v`\nfunc main(){}")
1907 tg.tempFile("gopath/src/dir1/vendor/v/v.go", "package v")
1908 tg.must(os.Symlink(tg.path("gopath/src/dir1"), tg.path("symdir1")))
1909 tg.setenv("GOPATH", tg.path("gopath"))
1910 tg.cd(tg.path("symdir1"))
1911 tg.run("list", "-f", "{{.Root}}", ".")
1912 if strings.TrimSpace(tg.getStdout()) != tg.path("gopath") {
1913 t.Error("list confused by symlinks")
1916 // All of these should succeed, not die in vendor-handling code.
1917 tg.run("run", "p.go")
1918 tg.run("build")
1919 tg.run("install")
1922 // Issue 15201.
1923 func TestSymlinksVendor15201(t *testing.T) {
1924 switch runtime.GOOS {
1925 case "plan9", "windows":
1926 t.Skipf("skipping symlink test on %s", runtime.GOOS)
1929 tg := testgo(t)
1930 defer tg.cleanup()
1932 tg.tempDir("gopath/src/x/y/_vendor/src/x")
1933 tg.must(os.Symlink("../../..", tg.path("gopath/src/x/y/_vendor/src/x/y")))
1934 tg.tempFile("gopath/src/x/y/w/w.go", "package w\nimport \"x/y/z\"\n")
1935 tg.must(os.Symlink("../_vendor/src", tg.path("gopath/src/x/y/w/vendor")))
1936 tg.tempFile("gopath/src/x/y/z/z.go", "package z\n")
1938 tg.setenv("GOPATH", tg.path("gopath/src/x/y/_vendor")+string(filepath.ListSeparator)+tg.path("gopath"))
1939 tg.cd(tg.path("gopath/src"))
1940 tg.run("list", "./...")
1943 func TestSymlinksInternal(t *testing.T) {
1944 switch runtime.GOOS {
1945 case "plan9", "windows":
1946 t.Skipf("skipping symlink test on %s", runtime.GOOS)
1949 tg := testgo(t)
1950 defer tg.cleanup()
1951 tg.tempDir("gopath/src/dir1/internal/v")
1952 tg.tempFile("gopath/src/dir1/p.go", "package main\nimport _ `dir1/internal/v`\nfunc main(){}")
1953 tg.tempFile("gopath/src/dir1/internal/v/v.go", "package v")
1954 tg.must(os.Symlink(tg.path("gopath/src/dir1"), tg.path("symdir1")))
1955 tg.setenv("GOPATH", tg.path("gopath"))
1956 tg.cd(tg.path("symdir1"))
1957 tg.run("list", "-f", "{{.Root}}", ".")
1958 if strings.TrimSpace(tg.getStdout()) != tg.path("gopath") {
1959 t.Error("list confused by symlinks")
1962 // All of these should succeed, not die in internal-handling code.
1963 tg.run("run", "p.go")
1964 tg.run("build")
1965 tg.run("install")
1968 // Issue 4515.
1969 func TestInstallWithTags(t *testing.T) {
1970 tg := testgo(t)
1971 defer tg.cleanup()
1972 tg.parallel()
1973 tg.tempDir("bin")
1974 tg.tempFile("src/example/a/main.go", `package main
1975 func main() {}`)
1976 tg.tempFile("src/example/b/main.go", `// +build mytag
1978 package main
1979 func main() {}`)
1980 tg.setenv("GOPATH", tg.path("."))
1981 tg.run("install", "-tags", "mytag", "example/a", "example/b")
1982 tg.wantExecutable(tg.path("bin/a"+exeSuffix), "go install example/a example/b did not install binaries")
1983 tg.wantExecutable(tg.path("bin/b"+exeSuffix), "go install example/a example/b did not install binaries")
1984 tg.must(os.Remove(tg.path("bin/a" + exeSuffix)))
1985 tg.must(os.Remove(tg.path("bin/b" + exeSuffix)))
1986 tg.run("install", "-tags", "mytag", "example/...")
1987 tg.wantExecutable(tg.path("bin/a"+exeSuffix), "go install example/... did not install binaries")
1988 tg.wantExecutable(tg.path("bin/b"+exeSuffix), "go install example/... did not install binaries")
1989 tg.run("list", "-tags", "mytag", "example/b...")
1990 if strings.TrimSpace(tg.getStdout()) != "example/b" {
1991 t.Error("go list example/b did not find example/b")
1995 // Issue 4773
1996 func TestCaseCollisions(t *testing.T) {
1997 tg := testgo(t)
1998 defer tg.cleanup()
1999 tg.parallel()
2000 tg.tempDir("src/example/a/pkg")
2001 tg.tempDir("src/example/a/Pkg")
2002 tg.tempDir("src/example/b")
2003 tg.setenv("GOPATH", tg.path("."))
2004 tg.tempFile("src/example/a/a.go", `package p
2005 import (
2006 _ "example/a/pkg"
2007 _ "example/a/Pkg"
2009 tg.tempFile("src/example/a/pkg/pkg.go", `package pkg`)
2010 tg.tempFile("src/example/a/Pkg/pkg.go", `package pkg`)
2011 tg.runFail("list", "example/a")
2012 tg.grepStderr("case-insensitive import collision", "go list example/a did not report import collision")
2013 tg.tempFile("src/example/b/file.go", `package b`)
2014 tg.tempFile("src/example/b/FILE.go", `package b`)
2015 f, err := os.Open(tg.path("src/example/b"))
2016 tg.must(err)
2017 names, err := f.Readdirnames(0)
2018 tg.must(err)
2019 tg.check(f.Close())
2020 args := []string{"list"}
2021 if len(names) == 2 {
2022 // case-sensitive file system, let directory read find both files
2023 args = append(args, "example/b")
2024 } else {
2025 // case-insensitive file system, list files explicitly on command line
2026 args = append(args, tg.path("src/example/b/file.go"), tg.path("src/example/b/FILE.go"))
2028 tg.runFail(args...)
2029 tg.grepStderr("case-insensitive file name collision", "go list example/b did not report file name collision")
2032 // Issue 8181.
2033 func TestGoGetDashTIssue8181(t *testing.T) {
2034 testenv.MustHaveExternalNetwork(t)
2036 tg := testgo(t)
2037 defer tg.cleanup()
2038 tg.parallel()
2039 tg.makeTempdir()
2040 tg.setenv("GOPATH", tg.path("."))
2041 tg.run("get", "-v", "-t", "github.com/rsc/go-get-issue-8181/a", "github.com/rsc/go-get-issue-8181/b")
2042 tg.run("list", "...")
2043 tg.grepStdout("x/build/gerrit", "missing expected x/build/gerrit")
2046 func TestIssue11307(t *testing.T) {
2047 // go get -u was not working except in checkout directory
2048 testenv.MustHaveExternalNetwork(t)
2050 tg := testgo(t)
2051 defer tg.cleanup()
2052 tg.parallel()
2053 tg.makeTempdir()
2054 tg.setenv("GOPATH", tg.path("."))
2055 tg.run("get", "github.com/rsc/go-get-issue-11307")
2056 tg.run("get", "-u", "github.com/rsc/go-get-issue-11307") // was failing
2059 func TestShadowingLogic(t *testing.T) {
2060 tg := testgo(t)
2061 defer tg.cleanup()
2062 pwd := tg.pwd()
2063 sep := string(filepath.ListSeparator)
2064 tg.setenv("GOPATH", filepath.Join(pwd, "testdata", "shadow", "root1")+sep+filepath.Join(pwd, "testdata", "shadow", "root2"))
2066 // The math in root1 is not "math" because the standard math is.
2067 tg.run("list", "-f", "({{.ImportPath}}) ({{.ConflictDir}})", "./testdata/shadow/root1/src/math")
2068 pwdForwardSlash := strings.Replace(pwd, string(os.PathSeparator), "/", -1)
2069 if !strings.HasPrefix(pwdForwardSlash, "/") {
2070 pwdForwardSlash = "/" + pwdForwardSlash
2072 // The output will have makeImportValid applies, but we only
2073 // bother to deal with characters we might reasonably see.
2074 for _, r := range " :" {
2075 pwdForwardSlash = strings.Replace(pwdForwardSlash, string(r), "_", -1)
2077 want := "(_" + pwdForwardSlash + "/testdata/shadow/root1/src/math) (" + filepath.Join(runtime.GOROOT(), "src", "math") + ")"
2078 if strings.TrimSpace(tg.getStdout()) != want {
2079 t.Error("shadowed math is not shadowed; looking for", want)
2082 // The foo in root1 is "foo".
2083 tg.run("list", "-f", "({{.ImportPath}}) ({{.ConflictDir}})", "./testdata/shadow/root1/src/foo")
2084 if strings.TrimSpace(tg.getStdout()) != "(foo) ()" {
2085 t.Error("unshadowed foo is shadowed")
2088 // The foo in root2 is not "foo" because the foo in root1 got there first.
2089 tg.run("list", "-f", "({{.ImportPath}}) ({{.ConflictDir}})", "./testdata/shadow/root2/src/foo")
2090 want = "(_" + pwdForwardSlash + "/testdata/shadow/root2/src/foo) (" + filepath.Join(pwd, "testdata", "shadow", "root1", "src", "foo") + ")"
2091 if strings.TrimSpace(tg.getStdout()) != want {
2092 t.Error("shadowed foo is not shadowed; looking for", want)
2095 // The error for go install should mention the conflicting directory.
2096 tg.runFail("install", "./testdata/shadow/root2/src/foo")
2097 want = "go install: no install location for " + filepath.Join(pwd, "testdata", "shadow", "root2", "src", "foo") + ": hidden by " + filepath.Join(pwd, "testdata", "shadow", "root1", "src", "foo")
2098 if strings.TrimSpace(tg.getStderr()) != want {
2099 t.Error("wrong shadowed install error; looking for", want)
2103 // Only succeeds if source order is preserved.
2104 func TestSourceFileNameOrderPreserved(t *testing.T) {
2105 tg := testgo(t)
2106 defer tg.cleanup()
2107 tg.run("test", "testdata/example1_test.go", "testdata/example2_test.go")
2110 // Check that coverage analysis works at all.
2111 // Don't worry about the exact numbers but require not 0.0%.
2112 func checkCoverage(tg *testgoData, data string) {
2113 if regexp.MustCompile(`[^0-9]0\.0%`).MatchString(data) {
2114 tg.t.Error("some coverage results are 0.0%")
2116 tg.t.Log(data)
2119 func TestCoverageRuns(t *testing.T) {
2120 if testing.Short() {
2121 t.Skip("don't build libraries for coverage in short mode")
2123 tg := testgo(t)
2124 defer tg.cleanup()
2125 tg.run("test", "-short", "-coverpkg=strings", "strings", "regexp")
2126 data := tg.getStdout() + tg.getStderr()
2127 tg.run("test", "-short", "-cover", "strings", "math", "regexp")
2128 data += tg.getStdout() + tg.getStderr()
2129 checkCoverage(tg, data)
2132 // Check that coverage analysis uses set mode.
2133 func TestCoverageUsesSetMode(t *testing.T) {
2134 if testing.Short() {
2135 t.Skip("don't build libraries for coverage in short mode")
2137 tg := testgo(t)
2138 defer tg.cleanup()
2139 tg.creatingTemp("testdata/cover.out")
2140 tg.run("test", "-short", "-cover", "encoding/binary", "-coverprofile=testdata/cover.out")
2141 data := tg.getStdout() + tg.getStderr()
2142 if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
2143 t.Error(err)
2144 } else {
2145 if !bytes.Contains(out, []byte("mode: set")) {
2146 t.Error("missing mode: set")
2149 checkCoverage(tg, data)
2152 func TestCoverageUsesAtomicModeForRace(t *testing.T) {
2153 if testing.Short() {
2154 t.Skip("don't build libraries for coverage in short mode")
2156 if !canRace {
2157 t.Skip("skipping because race detector not supported")
2160 tg := testgo(t)
2161 defer tg.cleanup()
2162 tg.creatingTemp("testdata/cover.out")
2163 tg.run("test", "-short", "-race", "-cover", "encoding/binary", "-coverprofile=testdata/cover.out")
2164 data := tg.getStdout() + tg.getStderr()
2165 if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
2166 t.Error(err)
2167 } else {
2168 if !bytes.Contains(out, []byte("mode: atomic")) {
2169 t.Error("missing mode: atomic")
2172 checkCoverage(tg, data)
2175 func TestCoverageUsesActualSettingToOverrideEvenForRace(t *testing.T) {
2176 if testing.Short() {
2177 t.Skip("don't build libraries for coverage in short mode")
2179 if !canRace {
2180 t.Skip("skipping because race detector not supported")
2183 tg := testgo(t)
2184 defer tg.cleanup()
2185 tg.creatingTemp("testdata/cover.out")
2186 tg.run("test", "-short", "-race", "-cover", "encoding/binary", "-covermode=count", "-coverprofile=testdata/cover.out")
2187 data := tg.getStdout() + tg.getStderr()
2188 if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
2189 t.Error(err)
2190 } else {
2191 if !bytes.Contains(out, []byte("mode: count")) {
2192 t.Error("missing mode: count")
2195 checkCoverage(tg, data)
2198 func TestCoverageImportMainLoop(t *testing.T) {
2199 tg := testgo(t)
2200 defer tg.cleanup()
2201 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2202 tg.runFail("test", "importmain/test")
2203 tg.grepStderr("not an importable package", "did not detect import main")
2204 tg.runFail("test", "-cover", "importmain/test")
2205 tg.grepStderr("not an importable package", "did not detect import main")
2208 func TestTestEmpty(t *testing.T) {
2209 if !canRace {
2210 t.Skip("no race detector")
2213 wd, _ := os.Getwd()
2214 testdata := filepath.Join(wd, "testdata")
2216 for _, dir := range []string{"pkg", "test", "xtest", "pkgtest", "pkgxtest", "pkgtestxtest", "testxtest"} {
2217 t.Run(dir, func(t *testing.T) {
2218 tg := testgo(t)
2219 defer tg.cleanup()
2220 tg.setenv("GOPATH", testdata)
2221 tg.cd(filepath.Join(testdata, "src/empty/"+dir))
2222 tg.run("test", "-cover", "-coverpkg=.", "-race")
2224 if testing.Short() {
2225 break
2230 func TestBuildDryRunWithCgo(t *testing.T) {
2231 if !canCgo {
2232 t.Skip("skipping because cgo not enabled")
2235 tg := testgo(t)
2236 defer tg.cleanup()
2237 tg.tempFile("foo.go", `package main
2240 #include <limits.h>
2242 import "C"
2244 func main() {
2245 println(C.INT_MAX)
2247 tg.run("build", "-n", tg.path("foo.go"))
2248 tg.grepStderrNot(`os.Stat .* no such file or directory`, "unexpected stat of archive file")
2251 func TestCoverageWithCgo(t *testing.T) {
2252 if !canCgo {
2253 t.Skip("skipping because cgo not enabled")
2256 for _, dir := range []string{"cgocover", "cgocover2", "cgocover3", "cgocover4"} {
2257 t.Run(dir, func(t *testing.T) {
2258 tg := testgo(t)
2259 tg.parallel()
2260 defer tg.cleanup()
2261 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2262 tg.run("test", "-short", "-cover", dir)
2263 data := tg.getStdout() + tg.getStderr()
2264 checkCoverage(tg, data)
2269 func TestCgoDependsOnSyscall(t *testing.T) {
2270 if testing.Short() {
2271 t.Skip("skipping test that removes $GOROOT/pkg/*_race in short mode")
2273 if !canCgo {
2274 t.Skip("skipping because cgo not enabled")
2276 if !canRace {
2277 t.Skip("skipping because race detector not supported")
2280 tg := testgo(t)
2281 defer tg.cleanup()
2282 files, err := filepath.Glob(filepath.Join(runtime.GOROOT(), "pkg", "*_race"))
2283 tg.must(err)
2284 for _, file := range files {
2285 tg.check(os.RemoveAll(file))
2287 tg.tempFile("src/foo/foo.go", `
2288 package foo
2289 //#include <stdio.h>
2290 import "C"`)
2291 tg.setenv("GOPATH", tg.path("."))
2292 tg.run("build", "-race", "foo")
2295 func TestCgoShowsFullPathNames(t *testing.T) {
2296 if !canCgo {
2297 t.Skip("skipping because cgo not enabled")
2300 tg := testgo(t)
2301 defer tg.cleanup()
2302 tg.parallel()
2303 tg.tempFile("src/x/y/dirname/foo.go", `
2304 package foo
2305 import "C"
2306 func f() {`)
2307 tg.setenv("GOPATH", tg.path("."))
2308 tg.runFail("build", "x/y/dirname")
2309 tg.grepBoth("x/y/dirname", "error did not use full path")
2312 func TestCgoHandlesWlORIGIN(t *testing.T) {
2313 if !canCgo {
2314 t.Skip("skipping because cgo not enabled")
2317 tg := testgo(t)
2318 defer tg.cleanup()
2319 tg.parallel()
2320 tg.tempFile("src/origin/origin.go", `package origin
2321 // #cgo !darwin LDFLAGS: -Wl,-rpath -Wl,$ORIGIN
2322 // void f(void) {}
2323 import "C"
2324 func f() { C.f() }`)
2325 tg.setenv("GOPATH", tg.path("."))
2326 tg.run("build", "origin")
2329 func TestCgoPkgConfig(t *testing.T) {
2330 if !canCgo {
2331 t.Skip("skipping because cgo not enabled")
2333 tg := testgo(t)
2334 defer tg.cleanup()
2335 tg.parallel()
2337 tg.run("env", "PKG_CONFIG")
2338 pkgConfig := strings.TrimSpace(tg.getStdout())
2339 if out, err := exec.Command(pkgConfig, "--atleast-pkgconfig-version", "0.24").CombinedOutput(); err != nil {
2340 t.Skipf("%s --atleast-pkgconfig-version 0.24: %v\n%s", pkgConfig, err, out)
2343 // OpenBSD's pkg-config is strict about whitespace and only
2344 // supports backslash-escaped whitespace. It does not support
2345 // quotes, which the normal freedesktop.org pkg-config does
2346 // support. See http://man.openbsd.org/pkg-config.1
2347 tg.tempFile("foo.pc", `
2348 Name: foo
2349 Description: The foo library
2350 Version: 1.0.0
2351 Cflags: -Dhello=10 -Dworld=+32 -DDEFINED_FROM_PKG_CONFIG=hello\ world
2353 tg.tempFile("foo.go", `package main
2356 #cgo pkg-config: foo
2357 int value() {
2358 return DEFINED_FROM_PKG_CONFIG;
2361 import "C"
2362 import "os"
2364 func main() {
2365 if C.value() != 42 {
2366 println("value() =", C.value(), "wanted 42")
2367 os.Exit(1)
2371 tg.setenv("PKG_CONFIG_PATH", tg.path("."))
2372 tg.run("run", tg.path("foo.go"))
2375 // "go test -c -test.bench=XXX errors" should not hang
2376 func TestIssue6480(t *testing.T) {
2377 tg := testgo(t)
2378 defer tg.cleanup()
2379 // TODO: tg.parallel()
2380 tg.makeTempdir()
2381 tg.cd(tg.path("."))
2382 tg.run("test", "-c", "-test.bench=XXX", "errors")
2385 // cmd/cgo: undefined reference when linking a C-library using gccgo
2386 func TestIssue7573(t *testing.T) {
2387 if !canCgo {
2388 t.Skip("skipping because cgo not enabled")
2390 if _, err := exec.LookPath("gccgo"); err != nil {
2391 t.Skip("skipping because no gccgo compiler found")
2394 tg := testgo(t)
2395 defer tg.cleanup()
2396 tg.parallel()
2397 tg.tempFile("src/cgoref/cgoref.go", `
2398 package main
2399 // #cgo LDFLAGS: -L alibpath -lalib
2400 // void f(void) {}
2401 import "C"
2403 func main() { C.f() }`)
2404 tg.setenv("GOPATH", tg.path("."))
2405 tg.run("build", "-n", "-compiler", "gccgo", "cgoref")
2406 tg.grepStderr(`gccgo.*\-L alibpath \-lalib`, `no Go-inline "#cgo LDFLAGS:" ("-L alibpath -lalib") passed to gccgo linking stage`)
2409 func TestListTemplateContextFunction(t *testing.T) {
2410 t.Parallel()
2411 for _, tt := range []struct {
2412 v string
2413 want string
2415 {"GOARCH", runtime.GOARCH},
2416 {"GOOS", runtime.GOOS},
2417 {"GOROOT", filepath.Clean(runtime.GOROOT())},
2418 {"GOPATH", os.Getenv("GOPATH")},
2419 {"CgoEnabled", ""},
2420 {"UseAllFiles", ""},
2421 {"Compiler", ""},
2422 {"BuildTags", ""},
2423 {"ReleaseTags", ""},
2424 {"InstallSuffix", ""},
2426 tt := tt
2427 t.Run(tt.v, func(t *testing.T) {
2428 tg := testgo(t)
2429 tg.parallel()
2430 defer tg.cleanup()
2431 tmpl := "{{context." + tt.v + "}}"
2432 tg.run("list", "-f", tmpl)
2433 if tt.want == "" {
2434 return
2436 if got := strings.TrimSpace(tg.getStdout()); got != tt.want {
2437 t.Errorf("go list -f %q: got %q; want %q", tmpl, got, tt.want)
2443 // cmd/go: "go test" should fail if package does not build
2444 func TestIssue7108(t *testing.T) {
2445 tg := testgo(t)
2446 defer tg.cleanup()
2447 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2448 tg.runFail("test", "notest")
2451 // cmd/go: go test -a foo does not rebuild regexp.
2452 func TestIssue6844(t *testing.T) {
2453 if testing.Short() {
2454 t.Skip("don't rebuild the standard library in short mode")
2457 tg := testgo(t)
2458 defer tg.cleanup()
2459 tg.creatingTemp("deps.test" + exeSuffix)
2460 tg.run("test", "-x", "-a", "-c", "testdata/dep_test.go")
2461 tg.grepStderr("regexp", "go test -x -a -c testdata/dep-test.go did not rebuild regexp")
2464 func TestBuildDashIInstallsDependencies(t *testing.T) {
2465 tg := testgo(t)
2466 defer tg.cleanup()
2467 tg.parallel()
2468 tg.tempFile("src/x/y/foo/foo.go", `package foo
2469 func F() {}`)
2470 tg.tempFile("src/x/y/bar/bar.go", `package bar
2471 import "x/y/foo"
2472 func F() { foo.F() }`)
2473 tg.setenv("GOPATH", tg.path("."))
2475 checkbar := func(desc string) {
2476 tg.sleep()
2477 tg.must(os.Chtimes(tg.path("src/x/y/foo/foo.go"), time.Now(), time.Now()))
2478 tg.sleep()
2479 tg.run("build", "-v", "-i", "x/y/bar")
2480 tg.grepBoth("x/y/foo", "first build -i "+desc+" did not build x/y/foo")
2481 tg.run("build", "-v", "-i", "x/y/bar")
2482 tg.grepBothNot("x/y/foo", "second build -i "+desc+" built x/y/foo")
2484 checkbar("pkg")
2485 tg.creatingTemp("bar" + exeSuffix)
2486 tg.tempFile("src/x/y/bar/bar.go", `package main
2487 import "x/y/foo"
2488 func main() { foo.F() }`)
2489 checkbar("cmd")
2492 func TestGoBuildInTestOnlyDirectoryFailsWithAGoodError(t *testing.T) {
2493 tg := testgo(t)
2494 defer tg.cleanup()
2495 tg.runFail("build", "./testdata/testonly")
2496 tg.grepStderr("no buildable Go", "go build ./testdata/testonly produced unexpected error")
2499 func TestGoTestDetectsTestOnlyImportCycles(t *testing.T) {
2500 tg := testgo(t)
2501 defer tg.cleanup()
2502 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2503 tg.runFail("test", "-c", "testcycle/p3")
2504 tg.grepStderr("import cycle not allowed in test", "go test testcycle/p3 produced unexpected error")
2506 tg.runFail("test", "-c", "testcycle/q1")
2507 tg.grepStderr("import cycle not allowed in test", "go test testcycle/q1 produced unexpected error")
2510 func TestGoTestFooTestWorks(t *testing.T) {
2511 tg := testgo(t)
2512 defer tg.cleanup()
2513 tg.run("test", "testdata/standalone_test.go")
2516 func TestGoTestFlagsAfterPackage(t *testing.T) {
2517 tg := testgo(t)
2518 defer tg.cleanup()
2519 tg.run("test", "testdata/flag_test.go", "-v", "-args", "-v=7") // Two distinct -v flags.
2520 tg.run("test", "-v", "testdata/flag_test.go", "-args", "-v=7") // Two distinct -v flags.
2523 func TestGoTestXtestonlyWorks(t *testing.T) {
2524 tg := testgo(t)
2525 defer tg.cleanup()
2526 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2527 tg.run("clean", "-i", "xtestonly")
2528 tg.run("test", "xtestonly")
2531 func TestGoTestBuildsAnXtestContainingOnlyNonRunnableExamples(t *testing.T) {
2532 tg := testgo(t)
2533 defer tg.cleanup()
2534 tg.run("test", "-v", "./testdata/norunexample")
2535 tg.grepStdout("File with non-runnable example was built.", "file with non-runnable example was not built")
2538 func TestGoGenerateHandlesSimpleCommand(t *testing.T) {
2539 if runtime.GOOS == "windows" {
2540 t.Skip("skipping because windows has no echo command")
2543 tg := testgo(t)
2544 defer tg.cleanup()
2545 tg.run("generate", "./testdata/generate/test1.go")
2546 tg.grepStdout("Success", "go generate ./testdata/generate/test1.go generated wrong output")
2549 func TestGoGenerateHandlesCommandAlias(t *testing.T) {
2550 if runtime.GOOS == "windows" {
2551 t.Skip("skipping because windows has no echo command")
2554 tg := testgo(t)
2555 defer tg.cleanup()
2556 tg.run("generate", "./testdata/generate/test2.go")
2557 tg.grepStdout("Now is the time for all good men", "go generate ./testdata/generate/test2.go generated wrong output")
2560 func TestGoGenerateVariableSubstitution(t *testing.T) {
2561 if runtime.GOOS == "windows" {
2562 t.Skip("skipping because windows has no echo command")
2565 tg := testgo(t)
2566 defer tg.cleanup()
2567 tg.run("generate", "./testdata/generate/test3.go")
2568 tg.grepStdout(runtime.GOARCH+" test3.go:7 pabc xyzp/test3.go/123", "go generate ./testdata/generate/test3.go generated wrong output")
2571 func TestGoGenerateRunFlag(t *testing.T) {
2572 if runtime.GOOS == "windows" {
2573 t.Skip("skipping because windows has no echo command")
2576 tg := testgo(t)
2577 defer tg.cleanup()
2578 tg.run("generate", "-run", "y.s", "./testdata/generate/test4.go")
2579 tg.grepStdout("yes", "go generate -run yes ./testdata/generate/test4.go did not select yes")
2580 tg.grepStdoutNot("no", "go generate -run yes ./testdata/generate/test4.go selected no")
2583 func TestGoGenerateEnv(t *testing.T) {
2584 switch runtime.GOOS {
2585 case "plan9", "windows":
2586 t.Skipf("skipping because %s does not have the env command", runtime.GOOS)
2588 tg := testgo(t)
2589 defer tg.cleanup()
2590 tg.parallel()
2591 tg.tempFile("env.go", "package main\n\n//go:generate env")
2592 tg.run("generate", tg.path("env.go"))
2593 for _, v := range []string{"GOARCH", "GOOS", "GOFILE", "GOLINE", "GOPACKAGE", "DOLLAR"} {
2594 tg.grepStdout("^"+v+"=", "go generate environment missing "+v)
2598 func TestGoGenerateBadImports(t *testing.T) {
2599 if runtime.GOOS == "windows" {
2600 t.Skip("skipping because windows has no echo command")
2603 // This package has an invalid import causing an import cycle,
2604 // but go generate is supposed to still run.
2605 tg := testgo(t)
2606 defer tg.cleanup()
2607 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2608 tg.run("generate", "gencycle")
2609 tg.grepStdout("hello world", "go generate gencycle did not run generator")
2612 func TestGoGetCustomDomainWildcard(t *testing.T) {
2613 testenv.MustHaveExternalNetwork(t)
2615 tg := testgo(t)
2616 defer tg.cleanup()
2617 tg.makeTempdir()
2618 tg.setenv("GOPATH", tg.path("."))
2619 tg.run("get", "-u", "rsc.io/pdf/...")
2620 tg.wantExecutable(tg.path("bin/pdfpasswd"+exeSuffix), "did not build rsc/io/pdf/pdfpasswd")
2623 func TestGoGetInternalWildcard(t *testing.T) {
2624 testenv.MustHaveExternalNetwork(t)
2626 tg := testgo(t)
2627 defer tg.cleanup()
2628 tg.makeTempdir()
2629 tg.setenv("GOPATH", tg.path("."))
2630 // used to fail with errors about internal packages
2631 tg.run("get", "github.com/rsc/go-get-issue-11960/...")
2634 func TestGoVetWithExternalTests(t *testing.T) {
2635 testenv.MustHaveExternalNetwork(t)
2637 tg := testgo(t)
2638 defer tg.cleanup()
2639 tg.makeTempdir()
2640 tg.run("install", "cmd/vet")
2641 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2642 tg.runFail("vet", "vetpkg")
2643 tg.grepBoth("missing argument for Printf", "go vet vetpkg did not find missing argument for Printf")
2646 func TestGoVetWithTags(t *testing.T) {
2647 testenv.MustHaveExternalNetwork(t)
2649 tg := testgo(t)
2650 defer tg.cleanup()
2651 tg.makeTempdir()
2652 tg.run("install", "cmd/vet")
2653 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2654 tg.runFail("vet", "-tags", "tagtest", "vetpkg")
2655 tg.grepBoth(`c\.go.*wrong number of args for format`, "go get vetpkg did not run scan tagged file")
2658 // Issue 9767.
2659 func TestGoGetRscIoToolstash(t *testing.T) {
2660 testenv.MustHaveExternalNetwork(t)
2662 tg := testgo(t)
2663 defer tg.cleanup()
2664 tg.tempDir("src/rsc.io")
2665 tg.setenv("GOPATH", tg.path("."))
2666 tg.cd(tg.path("src/rsc.io"))
2667 tg.run("get", "./toolstash")
2670 // Issue 13037: Was not parsing <meta> tags in 404 served over HTTPS
2671 func TestGoGetHTTPS404(t *testing.T) {
2672 testenv.MustHaveExternalNetwork(t)
2673 switch runtime.GOOS {
2674 case "darwin", "linux", "freebsd":
2675 default:
2676 t.Skipf("test case does not work on %s", runtime.GOOS)
2679 tg := testgo(t)
2680 defer tg.cleanup()
2681 tg.tempDir("src")
2682 tg.setenv("GOPATH", tg.path("."))
2683 tg.run("get", "bazil.org/fuse/fs/fstestutil")
2686 // Test that you cannot import a main package.
2687 // See golang.org/issue/4210 and golang.org/issue/17475.
2688 func TestImportMain(t *testing.T) {
2689 tg := testgo(t)
2690 tg.parallel()
2691 defer tg.cleanup()
2693 // Importing package main from that package main's test should work.
2694 tg.tempFile("src/x/main.go", `package main
2695 var X int
2696 func main() {}`)
2697 tg.tempFile("src/x/main_test.go", `package main_test
2698 import xmain "x"
2699 import "testing"
2700 var _ = xmain.X
2701 func TestFoo(t *testing.T) {}
2703 tg.setenv("GOPATH", tg.path("."))
2704 tg.creatingTemp("x")
2705 tg.run("build", "x")
2706 tg.run("test", "x")
2708 // Importing package main from another package should fail.
2709 tg.tempFile("src/p1/p.go", `package p1
2710 import xmain "x"
2711 var _ = xmain.X
2713 tg.runFail("build", "p1")
2714 tg.grepStderr("import \"x\" is a program, not an importable package", "did not diagnose package main")
2716 // ... even in that package's test.
2717 tg.tempFile("src/p2/p.go", `package p2
2719 tg.tempFile("src/p2/p_test.go", `package p2
2720 import xmain "x"
2721 import "testing"
2722 var _ = xmain.X
2723 func TestFoo(t *testing.T) {}
2725 tg.run("build", "p2")
2726 tg.runFail("test", "p2")
2727 tg.grepStderr("import \"x\" is a program, not an importable package", "did not diagnose package main")
2729 // ... even if that package's test is an xtest.
2730 tg.tempFile("src/p3/p.go", `package p
2732 tg.tempFile("src/p3/p_test.go", `package p_test
2733 import xmain "x"
2734 import "testing"
2735 var _ = xmain.X
2736 func TestFoo(t *testing.T) {}
2738 tg.run("build", "p3")
2739 tg.runFail("test", "p3")
2740 tg.grepStderr("import \"x\" is a program, not an importable package", "did not diagnose package main")
2742 // ... even if that package is a package main
2743 tg.tempFile("src/p4/p.go", `package main
2744 func main() {}
2746 tg.tempFile("src/p4/p_test.go", `package main
2747 import xmain "x"
2748 import "testing"
2749 var _ = xmain.X
2750 func TestFoo(t *testing.T) {}
2752 tg.creatingTemp("p4" + exeSuffix)
2753 tg.run("build", "p4")
2754 tg.runFail("test", "p4")
2755 tg.grepStderr("import \"x\" is a program, not an importable package", "did not diagnose package main")
2757 // ... even if that package is a package main using an xtest.
2758 tg.tempFile("src/p5/p.go", `package main
2759 func main() {}
2761 tg.tempFile("src/p5/p_test.go", `package main_test
2762 import xmain "x"
2763 import "testing"
2764 var _ = xmain.X
2765 func TestFoo(t *testing.T) {}
2767 tg.creatingTemp("p5" + exeSuffix)
2768 tg.run("build", "p5")
2769 tg.runFail("test", "p5")
2770 tg.grepStderr("import \"x\" is a program, not an importable package", "did not diagnose package main")
2773 // Test that you cannot use a local import in a package
2774 // accessed by a non-local import (found in a GOPATH/GOROOT).
2775 // See golang.org/issue/17475.
2776 func TestImportLocal(t *testing.T) {
2777 tg := testgo(t)
2778 tg.parallel()
2779 defer tg.cleanup()
2781 tg.tempFile("src/dir/x/x.go", `package x
2782 var X int
2784 tg.setenv("GOPATH", tg.path("."))
2785 tg.run("build", "dir/x")
2787 // Ordinary import should work.
2788 tg.tempFile("src/dir/p0/p.go", `package p0
2789 import "dir/x"
2790 var _ = x.X
2792 tg.run("build", "dir/p0")
2794 // Relative import should not.
2795 tg.tempFile("src/dir/p1/p.go", `package p1
2796 import "../x"
2797 var _ = x.X
2799 tg.runFail("build", "dir/p1")
2800 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2802 // ... even in a test.
2803 tg.tempFile("src/dir/p2/p.go", `package p2
2805 tg.tempFile("src/dir/p2/p_test.go", `package p2
2806 import "../x"
2807 import "testing"
2808 var _ = x.X
2809 func TestFoo(t *testing.T) {}
2811 tg.run("build", "dir/p2")
2812 tg.runFail("test", "dir/p2")
2813 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2815 // ... even in an xtest.
2816 tg.tempFile("src/dir/p2/p_test.go", `package p2_test
2817 import "../x"
2818 import "testing"
2819 var _ = x.X
2820 func TestFoo(t *testing.T) {}
2822 tg.run("build", "dir/p2")
2823 tg.runFail("test", "dir/p2")
2824 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2826 // Relative import starting with ./ should not work either.
2827 tg.tempFile("src/dir/d.go", `package dir
2828 import "./x"
2829 var _ = x.X
2831 tg.runFail("build", "dir")
2832 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2834 // ... even in a test.
2835 tg.tempFile("src/dir/d.go", `package dir
2837 tg.tempFile("src/dir/d_test.go", `package dir
2838 import "./x"
2839 import "testing"
2840 var _ = x.X
2841 func TestFoo(t *testing.T) {}
2843 tg.run("build", "dir")
2844 tg.runFail("test", "dir")
2845 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2847 // ... even in an xtest.
2848 tg.tempFile("src/dir/d_test.go", `package dir_test
2849 import "./x"
2850 import "testing"
2851 var _ = x.X
2852 func TestFoo(t *testing.T) {}
2854 tg.run("build", "dir")
2855 tg.runFail("test", "dir")
2856 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2858 // Relative import plain ".." should not work.
2859 tg.tempFile("src/dir/x/y/y.go", `package dir
2860 import ".."
2861 var _ = x.X
2863 tg.runFail("build", "dir/x/y")
2864 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2866 // ... even in a test.
2867 tg.tempFile("src/dir/x/y/y.go", `package y
2869 tg.tempFile("src/dir/x/y/y_test.go", `package y
2870 import ".."
2871 import "testing"
2872 var _ = x.X
2873 func TestFoo(t *testing.T) {}
2875 tg.run("build", "dir/x/y")
2876 tg.runFail("test", "dir/x/y")
2877 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2879 // ... even in an x test.
2880 tg.tempFile("src/dir/x/y/y_test.go", `package y_test
2881 import ".."
2882 import "testing"
2883 var _ = x.X
2884 func TestFoo(t *testing.T) {}
2886 tg.run("build", "dir/x/y")
2887 tg.runFail("test", "dir/x/y")
2888 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2890 // Relative import "." should not work.
2891 tg.tempFile("src/dir/x/xx.go", `package x
2892 import "."
2893 var _ = x.X
2895 tg.runFail("build", "dir/x")
2896 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2898 // ... even in a test.
2899 tg.tempFile("src/dir/x/xx.go", `package x
2901 tg.tempFile("src/dir/x/xx_test.go", `package x
2902 import "."
2903 import "testing"
2904 var _ = x.X
2905 func TestFoo(t *testing.T) {}
2907 tg.run("build", "dir/x")
2908 tg.runFail("test", "dir/x")
2909 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2911 // ... even in an xtest.
2912 tg.tempFile("src/dir/x/xx.go", `package x
2914 tg.tempFile("src/dir/x/xx_test.go", `package x_test
2915 import "."
2916 import "testing"
2917 var _ = x.X
2918 func TestFoo(t *testing.T) {}
2920 tg.run("build", "dir/x")
2921 tg.runFail("test", "dir/x")
2922 tg.grepStderr("local import.*in non-local package", "did not diagnose local import")
2925 func TestGoGetInsecure(t *testing.T) {
2926 testenv.MustHaveExternalNetwork(t)
2928 tg := testgo(t)
2929 defer tg.cleanup()
2930 tg.makeTempdir()
2931 tg.setenv("GOPATH", tg.path("."))
2932 tg.failSSH()
2934 const repo = "insecure.go-get-issue-15410.appspot.com/pkg/p"
2936 // Try go get -d of HTTP-only repo (should fail).
2937 tg.runFail("get", "-d", repo)
2939 // Try again with -insecure (should succeed).
2940 tg.run("get", "-d", "-insecure", repo)
2942 // Try updating without -insecure (should fail).
2943 tg.runFail("get", "-d", "-u", "-f", repo)
2946 func TestGoGetUpdateInsecure(t *testing.T) {
2947 testenv.MustHaveExternalNetwork(t)
2949 tg := testgo(t)
2950 defer tg.cleanup()
2951 tg.makeTempdir()
2952 tg.setenv("GOPATH", tg.path("."))
2954 const repo = "github.com/golang/example"
2956 // Clone the repo via HTTP manually.
2957 cmd := exec.Command("git", "clone", "-q", "http://"+repo, tg.path("src/"+repo))
2958 if out, err := cmd.CombinedOutput(); err != nil {
2959 t.Fatalf("cloning %v repo: %v\n%s", repo, err, out)
2962 // Update without -insecure should fail.
2963 // Update with -insecure should succeed.
2964 // We need -f to ignore import comments.
2965 const pkg = repo + "/hello"
2966 tg.runFail("get", "-d", "-u", "-f", pkg)
2967 tg.run("get", "-d", "-u", "-f", "-insecure", pkg)
2970 func TestGoGetInsecureCustomDomain(t *testing.T) {
2971 testenv.MustHaveExternalNetwork(t)
2973 tg := testgo(t)
2974 defer tg.cleanup()
2975 tg.makeTempdir()
2976 tg.setenv("GOPATH", tg.path("."))
2978 const repo = "insecure.go-get-issue-15410.appspot.com/pkg/p"
2979 tg.runFail("get", "-d", repo)
2980 tg.run("get", "-d", "-insecure", repo)
2983 func TestGoRunDirs(t *testing.T) {
2984 tg := testgo(t)
2985 defer tg.cleanup()
2986 tg.cd("testdata/rundir")
2987 tg.runFail("run", "x.go", "sub/sub.go")
2988 tg.grepStderr("named files must all be in one directory; have ./ and sub/", "wrong output")
2989 tg.runFail("run", "sub/sub.go", "x.go")
2990 tg.grepStderr("named files must all be in one directory; have sub/ and ./", "wrong output")
2993 func TestGoInstallPkgdir(t *testing.T) {
2994 tg := testgo(t)
2995 tg.parallel()
2996 defer tg.cleanup()
2997 tg.makeTempdir()
2998 pkg := tg.path(".")
2999 tg.run("install", "-pkgdir", pkg, "errors")
3000 _, err := os.Stat(filepath.Join(pkg, "errors.a"))
3001 tg.must(err)
3002 _, err = os.Stat(filepath.Join(pkg, "runtime.a"))
3003 tg.must(err)
3006 func TestGoTestRaceInstallCgo(t *testing.T) {
3007 switch sys := runtime.GOOS + "/" + runtime.GOARCH; sys {
3008 case "darwin/amd64", "freebsd/amd64", "linux/amd64", "windows/amd64":
3009 // ok
3010 default:
3011 t.Skip("no race detector on %s", sys)
3014 if !build.Default.CgoEnabled {
3015 t.Skip("no race detector without cgo")
3018 // golang.org/issue/10500.
3019 // This used to install a race-enabled cgo.
3020 tg := testgo(t)
3021 defer tg.cleanup()
3022 tg.run("tool", "-n", "cgo")
3023 cgo := strings.TrimSpace(tg.stdout.String())
3024 old, err := os.Stat(cgo)
3025 tg.must(err)
3026 tg.run("test", "-race", "-i", "runtime/race")
3027 new, err := os.Stat(cgo)
3028 tg.must(err)
3029 if new.ModTime() != old.ModTime() {
3030 t.Fatalf("go test -i runtime/race reinstalled cmd/cgo")
3034 func TestGoTestRaceFailures(t *testing.T) {
3035 if !canRace {
3036 t.Skip("skipping because race detector not supported")
3039 tg := testgo(t)
3040 tg.parallel()
3041 defer tg.cleanup()
3042 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
3044 tg.run("test", "testrace")
3046 tg.runFail("test", "-race", "testrace")
3047 tg.grepStdout("FAIL: TestRace", "TestRace did not fail")
3048 tg.grepBothNot("PASS", "something passed")
3050 tg.runFail("test", "-race", "testrace", "-run", "XXX", "-bench", ".")
3051 tg.grepStdout("FAIL: BenchmarkRace", "BenchmarkRace did not fail")
3052 tg.grepBothNot("PASS", "something passed")
3055 func TestGoTestImportErrorStack(t *testing.T) {
3056 const out = `package testdep/p1 (test)
3057 imports testdep/p2
3058 imports testdep/p3: no buildable Go source files`
3060 tg := testgo(t)
3061 defer tg.cleanup()
3062 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
3063 tg.runFail("test", "testdep/p1")
3064 if !strings.Contains(tg.stderr.String(), out) {
3065 t.Fatalf("did not give full import stack:\n\n%s", tg.stderr.String())
3069 func TestGoGetUpdate(t *testing.T) {
3070 // golang.org/issue/9224.
3071 // The recursive updating was trying to walk to
3072 // former dependencies, not current ones.
3074 testenv.MustHaveExternalNetwork(t)
3076 tg := testgo(t)
3077 defer tg.cleanup()
3078 tg.makeTempdir()
3079 tg.setenv("GOPATH", tg.path("."))
3081 rewind := func() {
3082 tg.run("get", "github.com/rsc/go-get-issue-9224-cmd")
3083 cmd := exec.Command("git", "reset", "--hard", "HEAD~")
3084 cmd.Dir = tg.path("src/github.com/rsc/go-get-issue-9224-lib")
3085 out, err := cmd.CombinedOutput()
3086 if err != nil {
3087 t.Fatalf("git: %v\n%s", err, out)
3091 rewind()
3092 tg.run("get", "-u", "github.com/rsc/go-get-issue-9224-cmd")
3094 // Again with -d -u.
3095 rewind()
3096 tg.run("get", "-d", "-u", "github.com/rsc/go-get-issue-9224-cmd")
3099 func TestGoGetDomainRoot(t *testing.T) {
3100 // golang.org/issue/9357.
3101 // go get foo.io (not foo.io/subdir) was not working consistently.
3103 testenv.MustHaveExternalNetwork(t)
3105 tg := testgo(t)
3106 defer tg.cleanup()
3107 tg.makeTempdir()
3108 tg.setenv("GOPATH", tg.path("."))
3110 // go-get-issue-9357.appspot.com is running
3111 // the code at github.com/rsc/go-get-issue-9357,
3112 // a trivial Go on App Engine app that serves a
3113 // <meta> tag for the domain root.
3114 tg.run("get", "-d", "go-get-issue-9357.appspot.com")
3115 tg.run("get", "go-get-issue-9357.appspot.com")
3116 tg.run("get", "-u", "go-get-issue-9357.appspot.com")
3118 tg.must(os.RemoveAll(tg.path("src/go-get-issue-9357.appspot.com")))
3119 tg.run("get", "go-get-issue-9357.appspot.com")
3121 tg.must(os.RemoveAll(tg.path("src/go-get-issue-9357.appspot.com")))
3122 tg.run("get", "-u", "go-get-issue-9357.appspot.com")
3125 func TestGoInstallShadowedGOPATH(t *testing.T) {
3126 // golang.org/issue/3652.
3127 // go get foo.io (not foo.io/subdir) was not working consistently.
3129 testenv.MustHaveExternalNetwork(t)
3131 tg := testgo(t)
3132 defer tg.cleanup()
3133 tg.makeTempdir()
3134 tg.setenv("GOPATH", tg.path("gopath1")+string(filepath.ListSeparator)+tg.path("gopath2"))
3136 tg.tempDir("gopath1/src/test")
3137 tg.tempDir("gopath2/src/test")
3138 tg.tempFile("gopath2/src/test/main.go", "package main\nfunc main(){}\n")
3140 tg.cd(tg.path("gopath2/src/test"))
3141 tg.runFail("install")
3142 tg.grepStderr("no install location for.*gopath2.src.test: hidden by .*gopath1.src.test", "missing error")
3145 func TestGoBuildGOPATHOrder(t *testing.T) {
3146 // golang.org/issue/14176#issuecomment-179895769
3147 // golang.org/issue/14192
3148 // -I arguments to compiler could end up not in GOPATH order,
3149 // leading to unexpected import resolution in the compiler.
3150 // This is still not a complete fix (see golang.org/issue/14271 and next test)
3151 // but it is clearly OK and enough to fix both of the two reported
3152 // instances of the underlying problem. It will have to do for now.
3154 tg := testgo(t)
3155 defer tg.cleanup()
3156 tg.makeTempdir()
3157 tg.setenv("GOPATH", tg.path("p1")+string(filepath.ListSeparator)+tg.path("p2"))
3159 tg.tempFile("p1/src/foo/foo.go", "package foo\n")
3160 tg.tempFile("p2/src/baz/baz.go", "package baz\n")
3161 tg.tempFile("p2/pkg/"+runtime.GOOS+"_"+runtime.GOARCH+"/foo.a", "bad\n")
3162 tg.tempFile("p1/src/bar/bar.go", `
3163 package bar
3164 import _ "baz"
3165 import _ "foo"
3168 tg.run("install", "-x", "bar")
3171 func TestGoBuildGOPATHOrderBroken(t *testing.T) {
3172 // This test is known not to work.
3173 // See golang.org/issue/14271.
3174 t.Skip("golang.org/issue/14271")
3176 tg := testgo(t)
3177 defer tg.cleanup()
3178 tg.makeTempdir()
3180 tg.tempFile("p1/src/foo/foo.go", "package foo\n")
3181 tg.tempFile("p2/src/baz/baz.go", "package baz\n")
3182 tg.tempFile("p1/pkg/"+runtime.GOOS+"_"+runtime.GOARCH+"/baz.a", "bad\n")
3183 tg.tempFile("p2/pkg/"+runtime.GOOS+"_"+runtime.GOARCH+"/foo.a", "bad\n")
3184 tg.tempFile("p1/src/bar/bar.go", `
3185 package bar
3186 import _ "baz"
3187 import _ "foo"
3190 colon := string(filepath.ListSeparator)
3191 tg.setenv("GOPATH", tg.path("p1")+colon+tg.path("p2"))
3192 tg.run("install", "-x", "bar")
3194 tg.setenv("GOPATH", tg.path("p2")+colon+tg.path("p1"))
3195 tg.run("install", "-x", "bar")
3198 func TestIssue11709(t *testing.T) {
3199 tg := testgo(t)
3200 defer tg.cleanup()
3201 tg.tempFile("run.go", `
3202 package main
3203 import "os"
3204 func main() {
3205 if os.Getenv("TERM") != "" {
3206 os.Exit(1)
3209 tg.unsetenv("TERM")
3210 tg.run("run", tg.path("run.go"))
3213 func TestIssue12096(t *testing.T) {
3214 tg := testgo(t)
3215 defer tg.cleanup()
3216 tg.tempFile("test_test.go", `
3217 package main
3218 import ("os"; "testing")
3219 func TestEnv(t *testing.T) {
3220 if os.Getenv("TERM") != "" {
3221 t.Fatal("TERM is set")
3224 tg.unsetenv("TERM")
3225 tg.run("test", tg.path("test_test.go"))
3228 func TestGoBuildOutput(t *testing.T) {
3229 tg := testgo(t)
3230 defer tg.cleanup()
3232 tg.makeTempdir()
3233 tg.cd(tg.path("."))
3235 nonExeSuffix := ".exe"
3236 if exeSuffix == ".exe" {
3237 nonExeSuffix = ""
3240 tg.tempFile("x.go", "package main\nfunc main(){}\n")
3241 tg.run("build", "x.go")
3242 tg.wantExecutable("x"+exeSuffix, "go build x.go did not write x"+exeSuffix)
3243 tg.must(os.Remove(tg.path("x" + exeSuffix)))
3244 tg.mustNotExist("x" + nonExeSuffix)
3246 tg.run("build", "-o", "myprog", "x.go")
3247 tg.mustNotExist("x")
3248 tg.mustNotExist("x.exe")
3249 tg.wantExecutable("myprog", "go build -o myprog x.go did not write myprog")
3250 tg.mustNotExist("myprog.exe")
3252 tg.tempFile("p.go", "package p\n")
3253 tg.run("build", "p.go")
3254 tg.mustNotExist("p")
3255 tg.mustNotExist("p.a")
3256 tg.mustNotExist("p.o")
3257 tg.mustNotExist("p.exe")
3259 tg.run("build", "-o", "p.a", "p.go")
3260 tg.wantArchive("p.a")
3262 tg.run("build", "cmd/gofmt")
3263 tg.wantExecutable("gofmt"+exeSuffix, "go build cmd/gofmt did not write gofmt"+exeSuffix)
3264 tg.must(os.Remove(tg.path("gofmt" + exeSuffix)))
3265 tg.mustNotExist("gofmt" + nonExeSuffix)
3267 tg.run("build", "-o", "mygofmt", "cmd/gofmt")
3268 tg.wantExecutable("mygofmt", "go build -o mygofmt cmd/gofmt did not write mygofmt")
3269 tg.mustNotExist("mygofmt.exe")
3270 tg.mustNotExist("gofmt")
3271 tg.mustNotExist("gofmt.exe")
3273 tg.run("build", "sync/atomic")
3274 tg.mustNotExist("atomic")
3275 tg.mustNotExist("atomic.exe")
3277 tg.run("build", "-o", "myatomic.a", "sync/atomic")
3278 tg.wantArchive("myatomic.a")
3279 tg.mustNotExist("atomic")
3280 tg.mustNotExist("atomic.a")
3281 tg.mustNotExist("atomic.exe")
3283 tg.runFail("build", "-o", "whatever", "cmd/gofmt", "sync/atomic")
3284 tg.grepStderr("multiple packages", "did not reject -o with multiple packages")
3287 func TestGoBuildARM(t *testing.T) {
3288 if testing.Short() {
3289 t.Skip("skipping cross-compile in short mode")
3292 tg := testgo(t)
3293 defer tg.cleanup()
3295 tg.makeTempdir()
3296 tg.cd(tg.path("."))
3298 tg.setenv("GOARCH", "arm")
3299 tg.setenv("GOOS", "linux")
3300 tg.setenv("GOARM", "5")
3301 tg.tempFile("hello.go", `package main
3302 func main() {}`)
3303 tg.run("build", "hello.go")
3304 tg.grepStderrNot("unable to find math.a", "did not build math.a correctly")
3307 func TestIssue13655(t *testing.T) {
3308 tg := testgo(t)
3309 defer tg.cleanup()
3310 for _, pkg := range []string{"runtime", "runtime/internal/atomic"} {
3311 tg.run("list", "-f", "{{.Deps}}", pkg)
3312 tg.grepStdout("runtime/internal/sys", "did not find required dependency of "+pkg+" on runtime/internal/sys")
3316 // For issue 14337.
3317 func TestParallelTest(t *testing.T) {
3318 tg := testgo(t)
3319 tg.parallel()
3320 defer tg.cleanup()
3321 tg.makeTempdir()
3322 const testSrc = `package package_test
3323 import (
3324 "testing"
3326 func TestTest(t *testing.T) {
3328 tg.tempFile("src/p1/p1_test.go", strings.Replace(testSrc, "package_test", "p1_test", 1))
3329 tg.tempFile("src/p2/p2_test.go", strings.Replace(testSrc, "package_test", "p2_test", 1))
3330 tg.tempFile("src/p3/p3_test.go", strings.Replace(testSrc, "package_test", "p3_test", 1))
3331 tg.tempFile("src/p4/p4_test.go", strings.Replace(testSrc, "package_test", "p4_test", 1))
3332 tg.setenv("GOPATH", tg.path("."))
3333 tg.run("test", "-p=4", "p1", "p2", "p3", "p4")
3336 func TestCgoConsistentResults(t *testing.T) {
3337 if !canCgo {
3338 t.Skip("skipping because cgo not enabled")
3340 switch runtime.GOOS {
3341 case "freebsd":
3342 testenv.SkipFlaky(t, 15405)
3343 case "solaris":
3344 testenv.SkipFlaky(t, 13247)
3347 tg := testgo(t)
3348 defer tg.cleanup()
3349 tg.parallel()
3350 tg.makeTempdir()
3351 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
3352 exe1 := tg.path("cgotest1" + exeSuffix)
3353 exe2 := tg.path("cgotest2" + exeSuffix)
3354 tg.run("build", "-o", exe1, "cgotest")
3355 tg.run("build", "-x", "-o", exe2, "cgotest")
3356 b1, err := ioutil.ReadFile(exe1)
3357 tg.must(err)
3358 b2, err := ioutil.ReadFile(exe2)
3359 tg.must(err)
3361 if !tg.doGrepMatch(`-fdebug-prefix-map=\$WORK`, &tg.stderr) {
3362 t.Skip("skipping because C compiler does not support -fdebug-prefix-map")
3364 if !bytes.Equal(b1, b2) {
3365 t.Error("building cgotest twice did not produce the same output")
3369 // Issue 14444: go get -u .../ duplicate loads errors
3370 func TestGoGetUpdateAllDoesNotTryToLoadDuplicates(t *testing.T) {
3371 testenv.MustHaveExternalNetwork(t)
3373 tg := testgo(t)
3374 defer tg.cleanup()
3375 tg.makeTempdir()
3376 tg.setenv("GOPATH", tg.path("."))
3377 tg.run("get", "-u", ".../")
3378 tg.grepStderrNot("duplicate loads of", "did not remove old packages from cache")
3381 // Issue 17119 more duplicate load errors
3382 func TestIssue17119(t *testing.T) {
3383 testenv.MustHaveExternalNetwork(t)
3385 tg := testgo(t)
3386 defer tg.cleanup()
3387 tg.parallel()
3388 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
3389 tg.runFail("build", "dupload")
3390 tg.grepBothNot("duplicate load|internal error", "internal error")
3393 func TestFatalInBenchmarkCauseNonZeroExitStatus(t *testing.T) {
3394 tg := testgo(t)
3395 defer tg.cleanup()
3396 // TODO: tg.parallel()
3397 tg.runFail("test", "-run", "^$", "-bench", ".", "./testdata/src/benchfatal")
3398 tg.grepBothNot("^ok", "test passed unexpectedly")
3399 tg.grepBoth("FAIL.*benchfatal", "test did not run everything")
3402 func TestBinaryOnlyPackages(t *testing.T) {
3403 tg := testgo(t)
3404 defer tg.cleanup()
3405 tg.parallel()
3406 tg.makeTempdir()
3407 tg.setenv("GOPATH", tg.path("."))
3409 tg.tempFile("src/p1/p1.go", `//go:binary-only-package
3411 package p1
3413 tg.wantStale("p1", "cannot access install target", "p1 is binary-only but has no binary, should be stale")
3414 tg.runFail("install", "p1")
3415 tg.grepStderr("missing or invalid package binary", "did not report attempt to compile binary-only package")
3417 tg.tempFile("src/p1/p1.go", `
3418 package p1
3419 import "fmt"
3420 func F(b bool) { fmt.Printf("hello from p1\n"); if b { F(false) } }
3422 tg.run("install", "p1")
3423 os.Remove(tg.path("src/p1/p1.go"))
3424 tg.mustNotExist(tg.path("src/p1/p1.go"))
3426 tg.tempFile("src/p2/p2.go", `//go:binary-only-packages-are-not-great
3428 package p2
3429 import "p1"
3430 func F() { p1.F(true) }
3432 tg.runFail("install", "p2")
3433 tg.grepStderr("no buildable Go source files", "did not complain about missing sources")
3435 tg.tempFile("src/p1/missing.go", `//go:binary-only-package
3437 package p1
3438 func G()
3440 tg.wantNotStale("p1", "no source code", "should NOT want to rebuild p1 (first)")
3441 tg.run("install", "-x", "p1") // no-op, up to date
3442 tg.grepBothNot("/compile", "should not have run compiler")
3443 tg.run("install", "p2") // does not rebuild p1 (or else p2 will fail)
3444 tg.wantNotStale("p2", "", "should NOT want to rebuild p2")
3446 // changes to the non-source-code do not matter,
3447 // and only one file needs the special comment.
3448 tg.tempFile("src/p1/missing2.go", `
3449 package p1
3450 func H()
3452 tg.wantNotStale("p1", "no source code", "should NOT want to rebuild p1 (second)")
3453 tg.wantNotStale("p2", "", "should NOT want to rebuild p2")
3455 tg.tempFile("src/p3/p3.go", `
3456 package main
3457 import (
3458 "p1"
3459 "p2"
3461 func main() {
3462 p1.F(false)
3463 p2.F()
3466 tg.run("install", "p3")
3468 tg.run("run", tg.path("src/p3/p3.go"))
3469 tg.grepStdout("hello from p1", "did not see message from p1")
3471 tg.tempFile("src/p4/p4.go", `package main`)
3472 tg.tempFile("src/p4/p4not.go", `//go:binary-only-package
3474 // +build asdf
3476 package main
3478 tg.run("list", "-f", "{{.BinaryOnly}}", "p4")
3479 tg.grepStdout("false", "did not see BinaryOnly=false for p4")
3482 // Issue 16050.
3483 func TestAlwaysLinkSysoFiles(t *testing.T) {
3484 tg := testgo(t)
3485 defer tg.cleanup()
3486 tg.parallel()
3487 tg.tempDir("src/syso")
3488 tg.tempFile("src/syso/a.syso", ``)
3489 tg.tempFile("src/syso/b.go", `package syso`)
3490 tg.setenv("GOPATH", tg.path("."))
3492 // We should see the .syso file regardless of the setting of
3493 // CGO_ENABLED.
3495 tg.setenv("CGO_ENABLED", "1")
3496 tg.run("list", "-f", "{{.SysoFiles}}", "syso")
3497 tg.grepStdout("a.syso", "missing syso file with CGO_ENABLED=1")
3499 tg.setenv("CGO_ENABLED", "0")
3500 tg.run("list", "-f", "{{.SysoFiles}}", "syso")
3501 tg.grepStdout("a.syso", "missing syso file with CGO_ENABLED=0")
3504 // Issue 16120.
3505 func TestGenerateUsesBuildContext(t *testing.T) {
3506 if runtime.GOOS == "windows" {
3507 t.Skip("this test won't run under Windows")
3510 tg := testgo(t)
3511 defer tg.cleanup()
3512 tg.parallel()
3513 tg.tempDir("src/gen")
3514 tg.tempFile("src/gen/gen.go", "package gen\n//go:generate echo $GOOS $GOARCH\n")
3515 tg.setenv("GOPATH", tg.path("."))
3517 tg.setenv("GOOS", "linux")
3518 tg.setenv("GOARCH", "amd64")
3519 tg.run("generate", "gen")
3520 tg.grepStdout("linux amd64", "unexpected GOOS/GOARCH combination")
3522 tg.setenv("GOOS", "darwin")
3523 tg.setenv("GOARCH", "386")
3524 tg.run("generate", "gen")
3525 tg.grepStdout("darwin 386", "unexpected GOOS/GOARCH combination")
3528 // Issue 14450: go get -u .../ tried to import not downloaded package
3529 func TestGoGetUpdateWithWildcard(t *testing.T) {
3530 testenv.MustHaveExternalNetwork(t)
3532 tg := testgo(t)
3533 defer tg.cleanup()
3534 tg.parallel()
3535 tg.makeTempdir()
3536 tg.setenv("GOPATH", tg.path("."))
3537 const aPkgImportPath = "github.com/tmwh/go-get-issue-14450/a"
3538 tg.run("get", aPkgImportPath)
3539 tg.run("get", "-u", ".../")
3540 tg.grepStderrNot("cannot find package", "did not update packages given wildcard path")
3542 var expectedPkgPaths = []string{
3543 "src/github.com/tmwh/go-get-issue-14450/b",
3544 "src/github.com/tmwh/go-get-issue-14450-b-dependency/c",
3545 "src/github.com/tmwh/go-get-issue-14450-b-dependency/d",
3548 for _, importPath := range expectedPkgPaths {
3549 _, err := os.Stat(tg.path(importPath))
3550 tg.must(err)
3552 const notExpectedPkgPath = "src/github.com/tmwh/go-get-issue-14450-c-dependency/e"
3553 tg.mustNotExist(tg.path(notExpectedPkgPath))
3556 func TestGoEnv(t *testing.T) {
3557 tg := testgo(t)
3558 tg.parallel()
3559 defer tg.cleanup()
3560 tg.setenv("GOARCH", "arm")
3561 tg.run("env", "GOARCH")
3562 tg.grepStdout("^arm$", "GOARCH not honored")
3564 tg.run("env", "GCCGO")
3565 tg.grepStdout(".", "GCCGO unexpectedly empty")
3567 tg.run("env", "CGO_CFLAGS")
3568 tg.grepStdout(".", "default CGO_CFLAGS unexpectedly empty")
3570 tg.setenv("CGO_CFLAGS", "-foobar")
3571 tg.run("env", "CGO_CFLAGS")
3572 tg.grepStdout("^-foobar$", "CGO_CFLAGS not honored")
3574 tg.setenv("CC", "gcc -fmust -fgo -ffaster")
3575 tg.run("env", "CC")
3576 tg.grepStdout("gcc", "CC not found")
3577 tg.run("env", "GOGCCFLAGS")
3578 tg.grepStdout("-ffaster", "CC arguments not found")
3581 const (
3582 noMatchesPattern = `(?m)^ok.*\[no tests to run\]`
3583 okPattern = `(?m)^ok`
3586 func TestMatchesNoTests(t *testing.T) {
3587 tg := testgo(t)
3588 defer tg.cleanup()
3589 // TODO: tg.parallel()
3590 tg.run("test", "-run", "ThisWillNotMatch", "testdata/standalone_test.go")
3591 tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
3594 func TestMatchesNoTestsDoesNotOverrideBuildFailure(t *testing.T) {
3595 tg := testgo(t)
3596 defer tg.cleanup()
3597 tg.parallel()
3598 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
3599 tg.runFail("test", "-run", "ThisWillNotMatch", "syntaxerror")
3600 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3601 tg.grepBoth("FAIL", "go test did not say FAIL")
3604 func TestMatchesNoBenchmarksIsOK(t *testing.T) {
3605 tg := testgo(t)
3606 defer tg.cleanup()
3607 // TODO: tg.parallel()
3608 tg.run("test", "-run", "^$", "-bench", "ThisWillNotMatch", "testdata/standalone_benchmark_test.go")
3609 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3610 tg.grepBoth(okPattern, "go test did not say ok")
3613 func TestMatchesOnlyExampleIsOK(t *testing.T) {
3614 tg := testgo(t)
3615 defer tg.cleanup()
3616 // TODO: tg.parallel()
3617 tg.run("test", "-run", "Example", "testdata/example1_test.go")
3618 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3619 tg.grepBoth(okPattern, "go test did not say ok")
3622 func TestMatchesOnlyBenchmarkIsOK(t *testing.T) {
3623 tg := testgo(t)
3624 defer tg.cleanup()
3625 // TODO: tg.parallel()
3626 tg.run("test", "-run", "^$", "-bench", ".", "testdata/standalone_benchmark_test.go")
3627 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3628 tg.grepBoth(okPattern, "go test did not say ok")
3631 func TestMatchesOnlyTestIsOK(t *testing.T) {
3632 tg := testgo(t)
3633 defer tg.cleanup()
3634 // TODO: tg.parallel()
3635 tg.run("test", "-run", "Test", "testdata/standalone_test.go")
3636 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3637 tg.grepBoth(okPattern, "go test did not say ok")
3640 func TestMatchesNoTestsWithSubtests(t *testing.T) {
3641 tg := testgo(t)
3642 defer tg.cleanup()
3643 tg.run("test", "-run", "ThisWillNotMatch", "testdata/standalone_sub_test.go")
3644 tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
3647 func TestMatchesNoSubtestsMatch(t *testing.T) {
3648 tg := testgo(t)
3649 defer tg.cleanup()
3650 tg.run("test", "-run", "Test/ThisWillNotMatch", "testdata/standalone_sub_test.go")
3651 tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
3654 func TestMatchesNoSubtestsDoesNotOverrideFailure(t *testing.T) {
3655 tg := testgo(t)
3656 defer tg.cleanup()
3657 tg.runFail("test", "-run", "TestThatFails/ThisWillNotMatch", "testdata/standalone_fail_sub_test.go")
3658 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3659 tg.grepBoth("FAIL", "go test did not say FAIL")
3662 func TestMatchesOnlySubtestIsOK(t *testing.T) {
3663 tg := testgo(t)
3664 defer tg.cleanup()
3665 tg.run("test", "-run", "Test/Sub", "testdata/standalone_sub_test.go")
3666 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3667 tg.grepBoth(okPattern, "go test did not say ok")
3670 func TestMatchesNoSubtestsParallel(t *testing.T) {
3671 tg := testgo(t)
3672 defer tg.cleanup()
3673 tg.run("test", "-run", "Test/Sub/ThisWillNotMatch", "testdata/standalone_parallel_sub_test.go")
3674 tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
3677 func TestMatchesOnlySubtestParallelIsOK(t *testing.T) {
3678 tg := testgo(t)
3679 defer tg.cleanup()
3680 tg.run("test", "-run", "Test/Sub/Nested", "testdata/standalone_parallel_sub_test.go")
3681 tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
3682 tg.grepBoth(okPattern, "go test did not say ok")
3685 // Issue 18845
3686 func TestBenchTimeout(t *testing.T) {
3687 tg := testgo(t)
3688 defer tg.cleanup()
3689 tg.run("test", "-bench", ".", "-timeout", "750ms", "testdata/timeoutbench_test.go")
3692 func TestLinkXImportPathEscape(t *testing.T) {
3693 // golang.org/issue/16710
3694 tg := testgo(t)
3695 defer tg.cleanup()
3696 tg.parallel()
3697 tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
3698 exe := "./linkx" + exeSuffix
3699 tg.creatingTemp(exe)
3700 tg.run("build", "-o", exe, "-ldflags", "-X=my.pkg.Text=linkXworked", "my.pkg/main")
3701 out, err := exec.Command(exe).CombinedOutput()
3702 if err != nil {
3703 tg.t.Fatal(err)
3705 if string(out) != "linkXworked\n" {
3706 tg.t.Log(string(out))
3707 tg.t.Fatal(`incorrect output: expected "linkXworked\n"`)
3711 // Issue 18044.
3712 func TestLdBindNow(t *testing.T) {
3713 tg := testgo(t)
3714 defer tg.cleanup()
3715 tg.parallel()
3716 tg.setenv("LD_BIND_NOW", "1")
3717 tg.run("help")
3720 // Issue 18225.
3721 // This is really a cmd/asm issue but this is a convenient place to test it.
3722 func TestConcurrentAsm(t *testing.T) {
3723 tg := testgo(t)
3724 defer tg.cleanup()
3725 tg.parallel()
3726 asm := `DATA ·constants<>+0x0(SB)/8,$0
3727 GLOBL ·constants<>(SB),8,$8
3729 tg.tempFile("go/src/p/a.s", asm)
3730 tg.tempFile("go/src/p/b.s", asm)
3731 tg.tempFile("go/src/p/p.go", `package p`)
3732 tg.setenv("GOPATH", tg.path("go"))
3733 tg.run("build", "p")
3736 // Issue 18778.
3737 func TestDotDotDotOutsideGOPATH(t *testing.T) {
3738 tg := testgo(t)
3739 defer tg.cleanup()
3741 tg.tempFile("pkgs/a.go", `package x`)
3742 tg.tempFile("pkgs/a_test.go", `package x_test
3743 import "testing"
3744 func TestX(t *testing.T) {}`)
3746 tg.tempFile("pkgs/a/a.go", `package a`)
3747 tg.tempFile("pkgs/a/a_test.go", `package a_test
3748 import "testing"
3749 func TestA(t *testing.T) {}`)
3751 tg.cd(tg.path("pkgs"))
3752 tg.run("build", "./...")
3753 tg.run("test", "./...")
3754 tg.run("list", "./...")
3755 tg.grepStdout("pkgs$", "expected package not listed")
3756 tg.grepStdout("pkgs/a", "expected package not listed")