Skip to content

Commit 77fdf89

Browse files
committed
add ci for golang code blocks in markdown
1 parent 63b3b5f commit 77fdf89

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

.github/workflows/test.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
8+
pull_request:
9+
10+
schedule:
11+
- cron: '17 5 * * *'
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/setup-go@v2
19+
with:
20+
go-version: 1.18
21+
22+
- uses: actions/checkout@v2
23+
24+
- run: |
25+
go get -u
26+
go mod tidy
27+
go test

check_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"regexp"
8+
"strings"
9+
"testing"
10+
11+
_ "github.com/go-rod/rod"
12+
"github.com/go-rod/rod/lib/utils"
13+
"github.com/ysmood/got"
14+
)
15+
16+
func TestCheckCode(t *testing.T) {
17+
g := got.T(t)
18+
19+
g.E(os.RemoveAll("tmp"))
20+
21+
g.E(filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
22+
g.E(err)
23+
24+
if info.IsDir() || path[0] == '.' || strings.HasPrefix(path, "i18n") || filepath.Ext(path) != ".md" {
25+
return nil
26+
}
27+
28+
b, err := os.ReadFile(path)
29+
g.E(err)
30+
return checkGoCode(g, path, string(b))
31+
}))
32+
33+
defer func() {
34+
if e := recover(); e != nil {
35+
t.Error(e)
36+
}
37+
}()
38+
39+
utils.ExecLine(false, "go run golang.org/x/tools/cmd/goimports@latest -w tmp")
40+
utils.ExecLine(false, "go run github.com/ysmood/golangci-lint@latest")
41+
}
42+
43+
var count = 0
44+
45+
func checkGoCode(g got.G, path, body string) error {
46+
reg := regexp.MustCompile("(?s)```go\r?\n(.+?)```")
47+
48+
for _, m := range reg.FindAllStringSubmatch(body, -1) {
49+
code := strings.TrimSpace(m[1])
50+
if strings.HasPrefix(code, "package ") {
51+
} else if strings.Contains(code, "func ") {
52+
code = "package main\n" + vars(code) + code
53+
} else {
54+
code = "package main\n" + vars(code) + "func main() {\n" + code + "\n}"
55+
}
56+
p := filepath.FromSlash(fmt.Sprintf("tmp/code-check/c%03d/main.go", count))
57+
count++
58+
59+
code = "// Source markdown file: " + path + "\n\n" + code
60+
61+
g.WriteFile(p, []byte(code))
62+
}
63+
64+
return nil
65+
}
66+
67+
func vars(code string) string {
68+
vars := ""
69+
if strings.Contains(code, "page.") && !strings.Contains(code, "page :=") {
70+
vars += "var page *rod.Page\n"
71+
}
72+
if strings.Contains(code, "browser.") && !strings.Contains(code, "browser :=") {
73+
vars += "var browser *rod.Browser\n"
74+
}
75+
return vars
76+
}

go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
module github.com/go-rod/go-rod.github.io
22

33
go 1.17
4+
5+
require (
6+
github.com/go-rod/rod v0.107.3
7+
github.com/ysmood/got v0.31.2
8+
)
9+
10+
require (
11+
github.com/ysmood/goob v0.4.0 // indirect
12+
github.com/ysmood/gson v0.7.1 // indirect
13+
github.com/ysmood/leakless v0.7.0 // indirect
14+
)

go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/go-rod/rod v0.107.3 h1:s+auNxQW4CTV2DhZ8S6ooQ7CXED4xLJBZPvZTDFA4CA=
2+
github.com/go-rod/rod v0.107.3/go.mod h1:4SqYRUrcc4dSr9iT36YRZ4hdUAPg3A0O8RhxAMh0eCQ=
3+
github.com/ysmood/goob v0.4.0 h1:HsxXhyLBeGzWXnqVKtmT9qM7EuVs/XOgkX7T6r1o1AQ=
4+
github.com/ysmood/goob v0.4.0/go.mod h1:u6yx7ZhS4Exf2MwciFr6nIM8knHQIE22lFpWHnfql18=
5+
github.com/ysmood/got v0.31.2 h1:+aNBkkXrVqZ0UJfeiOfuGbKq3kiJarjNl371Nxz6zLA=
6+
github.com/ysmood/got v0.31.2/go.mod h1:pE1l4LOwOBhQg6A/8IAatkGp7uZjnalzrZolnlhhMgY=
7+
github.com/ysmood/gotrace v0.6.0 h1:SyI1d4jclswLhg7SWTL6os3L1WOKeNn/ZtzVQF8QmdY=
8+
github.com/ysmood/gotrace v0.6.0/go.mod h1:TzhIG7nHDry5//eYZDYcTzuJLYQIkykJzCRIo4/dzQM=
9+
github.com/ysmood/gson v0.7.1 h1:zKL2MTGtynxdBdlZjyGsvEOZ7dkxaY5TH6QhAbTgz0Q=
10+
github.com/ysmood/gson v0.7.1/go.mod h1:3Kzs5zDl21g5F/BlLTNcuAGAYLKt2lV5G8D1zF3RNmg=
11+
github.com/ysmood/leakless v0.7.0 h1:XCGdaPExyoreoQd+H5qgxM3ReNbSPFsEXpSKwbXbwQw=
12+
github.com/ysmood/leakless v0.7.0/go.mod h1:R8iAXPRaG97QJwqxs74RdwzcRHT1SWCGTNqY8q0JvMQ=

0 commit comments

Comments
 (0)