1
2
3
4
5 package vcstest_test
6
7 import (
8 "cmd/go/internal/vcweb"
9 "errors"
10 "flag"
11 "fmt"
12 "io"
13 "io/fs"
14 "log"
15 "net"
16 "net/http"
17 "net/http/httptest"
18 "os"
19 "os/exec"
20 "path/filepath"
21 "strings"
22 "testing"
23 "time"
24 )
25
26 var (
27 dir = flag.String("dir", "../../../testdata/vcstest", "directory containing scripts to serve")
28 host = flag.String("host", "localhost", "hostname on which to serve HTTP")
29 port = flag.Int("port", -1, "port on which to serve HTTP; if nonnegative, skips running tests")
30 )
31
32 func TestMain(m *testing.M) {
33 flag.Parse()
34
35 if *port >= 0 {
36 err := serveStandalone(*host, *port)
37 if err != nil {
38 log.Fatal(err)
39 }
40 os.Exit(0)
41 }
42
43 m.Run()
44 }
45
46
47 func serveStandalone(host string, port int) (err error) {
48 scriptDir, err := filepath.Abs(*dir)
49 if err != nil {
50 return err
51 }
52 work, err := os.MkdirTemp("", "vcweb")
53 if err != nil {
54 return err
55 }
56 defer func() {
57 if rmErr := os.RemoveAll(work); err == nil {
58 err = rmErr
59 }
60 }()
61
62 log.Printf("running scripts in %s", work)
63
64 v, err := vcweb.NewServer(scriptDir, work, log.Default())
65 if err != nil {
66 return err
67 }
68
69 l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
70 if err != nil {
71 return err
72 }
73 log.Printf("serving on http://%s:%d/", host, l.Addr().(*net.TCPAddr).Port)
74
75 return http.Serve(l, v)
76 }
77
78
79
80 func TestScripts(t *testing.T) {
81 scriptDir, err := filepath.Abs(*dir)
82 if err != nil {
83 t.Fatal(err)
84 }
85 s, err := vcweb.NewServer(scriptDir, t.TempDir(), log.Default())
86 if err != nil {
87 t.Fatal(err)
88 }
89 srv := httptest.NewServer(s)
90
91
92
93
94
95 t.Run("overview", func(t *testing.T) {
96 t.Parallel()
97
98 time.Sleep(1 * time.Millisecond)
99
100 resp, err := http.Get(srv.URL)
101 if err == nil {
102 io.Copy(io.Discard, resp.Body)
103 resp.Body.Close()
104 } else {
105 t.Error(err)
106 }
107 })
108
109 t.Cleanup(func() {
110
111
112
113
114
115 resp, err := http.Get(srv.URL)
116 if err == nil {
117 var body []byte
118 body, err = io.ReadAll(resp.Body)
119 if err == nil && testing.Verbose() {
120 t.Logf("GET %s:\n%s", srv.URL, body)
121 }
122 resp.Body.Close()
123 }
124 if err != nil {
125 t.Error(err)
126 }
127
128 srv.Close()
129 })
130
131 err = filepath.WalkDir(scriptDir, func(path string, d fs.DirEntry, err error) error {
132 if err != nil || d.IsDir() {
133 return err
134 }
135
136 rel, err := filepath.Rel(scriptDir, path)
137 if err != nil {
138 return err
139 }
140 if rel == "README" {
141 return nil
142 }
143
144 t.Run(filepath.ToSlash(rel), func(t *testing.T) {
145 t.Parallel()
146
147 buf := new(strings.Builder)
148 logger := log.New(buf, "", log.LstdFlags)
149
150
151
152
153 err := s.HandleScript(rel, logger, func(http.Handler) {})
154 if buf.Len() > 0 {
155 t.Log(buf)
156 }
157 if err != nil {
158 if notInstalled := (vcweb.ServerNotInstalledError{}); errors.As(err, ¬Installed) || errors.Is(err, exec.ErrNotFound) {
159 t.Skip(err)
160 }
161 t.Error(err)
162 }
163 })
164 return nil
165 })
166
167 if err != nil {
168 t.Error(err)
169 }
170 }
171
View as plain text