]> Cypherpunks.ru repositories - gostls13.git/blob - misc/reboot/reboot_test.go
cmd/dist: drop host test support
[gostls13.git] / misc / reboot / reboot_test.go
1 // Copyright 2019 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.
4
5 // Package reboot_test verifies that the current GOROOT can be used to bootstrap
6 // itself.
7 package reboot_test
8
9 import (
10         "fmt"
11         "io"
12         "os"
13         "os/exec"
14         "path/filepath"
15         "runtime"
16         "strings"
17         "testing"
18         "time"
19 )
20
21 func TestRepeatBootstrap(t *testing.T) {
22         if testing.Short() {
23                 t.Skipf("skipping test that rebuilds the entire toolchain")
24         }
25
26         realGoroot, err := filepath.Abs(filepath.Join("..", ".."))
27         if err != nil {
28                 t.Fatal(err)
29         }
30
31         // To ensure that bootstrapping doesn't unexpectedly depend
32         // on the Go repo's git metadata, add a fake (unreadable) git
33         // directory above the simulated GOROOT.
34         // This mimics the configuration one much have when
35         // building from distro-packaged source code
36         // (see https://go.dev/issue/54852).
37         parent := t.TempDir()
38         dotGit := filepath.Join(parent, ".git")
39         if err := os.Mkdir(dotGit, 000); err != nil {
40                 t.Fatal(err)
41         }
42
43         overlayStart := time.Now()
44
45         goroot := filepath.Join(parent, "goroot")
46
47         gorootSrc := filepath.Join(goroot, "src")
48         if err := overlayDir(gorootSrc, filepath.Join(realGoroot, "src")); err != nil {
49                 t.Fatal(err)
50         }
51
52         gorootLib := filepath.Join(goroot, "lib")
53         if err := overlayDir(gorootLib, filepath.Join(realGoroot, "lib")); err != nil {
54                 t.Fatal(err)
55         }
56
57         t.Logf("GOROOT overlay set up in %s", time.Since(overlayStart))
58
59         if err := os.WriteFile(filepath.Join(goroot, "VERSION"), []byte(runtime.Version()), 0666); err != nil {
60                 t.Fatal(err)
61         }
62
63         var makeScript string
64         switch runtime.GOOS {
65         case "windows":
66                 makeScript = "make.bat"
67         case "plan9":
68                 makeScript = "make.rc"
69         default:
70                 makeScript = "make.bash"
71         }
72
73         var stdout strings.Builder
74         cmd := exec.Command(filepath.Join(goroot, "src", makeScript))
75         cmd.Dir = gorootSrc
76         cmd.Env = append(cmd.Environ(), "GOROOT=", "GOROOT_FINAL=", "GOROOT_BOOTSTRAP="+realGoroot)
77         cmd.Stderr = os.Stderr
78         cmd.Stdout = io.MultiWriter(os.Stdout, &stdout)
79         if err := cmd.Run(); err != nil {
80                 t.Fatal(err)
81         }
82
83         // Test that go.dev/issue/42563 hasn't regressed.
84         t.Run("PATH reminder", func(t *testing.T) {
85                 var want string
86                 switch gorootBin := filepath.Join(goroot, "bin"); runtime.GOOS {
87                 default:
88                         want = fmt.Sprintf("*** You need to add %s to your PATH.", gorootBin)
89                 case "plan9":
90                         want = fmt.Sprintf("*** You need to bind %s before /bin.", gorootBin)
91                 }
92                 if got := stdout.String(); !strings.Contains(got, want) {
93                         t.Errorf("reminder %q is missing from %s stdout:\n%s", want, makeScript, got)
94                 }
95         })
96 }