forked from golang/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_test.go
82 lines (73 loc) · 2.03 KB
/
content_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package website_test
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
// Keep golang.org/x/tour in our go.mod require list for use during test.
_ "golang.org/x/tour/wc"
)
// Test that all the .go files inside the _content/tour directory build
// and execute (without checking for output correctness).
// Files that contain the build tag "nobuild" are not built.
// Files that contain the build tag "norun" are not executed.
func TestTourContent(t *testing.T) {
if _, err := exec.LookPath("go"); err != nil {
t.Skipf("skipping because 'go' executable not available: %v", err)
}
err := filepath.Walk(filepath.Join("_content", "tour"), func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) != ".go" {
return nil
}
t.Run(path, func(t *testing.T) {
t.Parallel()
if err := testSnippet(filepath.ToSlash(path), t.TempDir()); err != nil {
t.Errorf("%v: %v", path, err)
}
})
return nil
})
if err != nil {
t.Error(err)
}
}
func testSnippet(path, scratch string) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}
build := string(bytes.SplitN(b, []byte{'\n'}, 2)[0])
if !strings.HasPrefix(build, "//go:build ") {
return errors.New("first line is not a go:build comment")
}
if !strings.Contains(build, "OMIT") {
return errors.New(`build comment does not contain "OMIT"`)
}
if strings.Contains(build, "nobuild") {
return nil
}
bin := filepath.Join(scratch, filepath.Base(path)+".exe")
out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput()
if err != nil {
return fmt.Errorf("build error: %v\noutput:\n%s", err, out)
}
defer os.Remove(bin)
if strings.Contains(build, "norun") {
return nil
}
out, err = exec.Command(bin).CombinedOutput()
if err != nil {
return fmt.Errorf("%v\nOutput:\n%s", err, out)
}
return nil
}