Skip to content

Commit

Permalink
Test Go Library Examples (#5095)
Browse files Browse the repository at this point in the history
Add markdown test to run go vet on go library examples. Fix existing go
library examples.

Closes #4703
  • Loading branch information
mattnibs authored Jun 20, 2024
1 parent 36a18e9 commit c014c91
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
18 changes: 10 additions & 8 deletions docs/libraries/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ go get github.com/brimdata/zed
### ZSON Reader

Read ZSON from stdin, dereference field `s`, and print results:
```
```mdtest-go-example
package main
import (
Expand All @@ -54,7 +54,8 @@ import (
func main() {
zctx := zed.NewContext()
reader := zsonio.NewReader(os.Stdin, zctx)
reader := zsonio.NewReader(zctx, os.Stdin)
arena := zed.NewArena()
for {
val, err := reader.Read()
if err != nil {
Expand All @@ -63,9 +64,9 @@ func main() {
if val == nil {
return
}
s := val.Deref("s")
s := val.Deref(arena, "s")
if s == nil {
s = zctx.Missing()
s = zctx.Missing(arena).Ptr()
}
fmt.Println(zson.String(s))
}
Expand Down Expand Up @@ -105,7 +106,7 @@ zed create -lake scratch Demo
echo '{s:"hello, world"}{x:1}{s:"good bye"}' | zed load -lake scratch -use Demo -
```
Now replace `main.go` with this code:
```
```mdtest-go-example
package main
import (
Expand All @@ -129,7 +130,7 @@ func main() {
log.Fatalln(err)
}
ctx := context.TODO()
lake, err := api.OpenLake(ctx, uri.String())
lake, err := api.OpenLake(ctx, nil, uri.String())
if err != nil {
log.Fatalln(err)
}
Expand All @@ -139,6 +140,7 @@ func main() {
}
defer reader.Close()
zctx := zed.NewContext()
arena := zed.NewArena()
for {
val, err := reader.Read()
if err != nil {
Expand All @@ -147,9 +149,9 @@ func main() {
if val == nil {
return
}
s := val.Deref("s")
s := val.Deref(arena, "s")
if s == nil {
s = zctx.Missing()
s = zctx.Missing(arena).Ptr()
}
fmt.Println(zson.String(s))
}
Expand Down
5 changes: 5 additions & 0 deletions mdtest/mdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ func parseMarkdown(source []byte) (map[string]string, []*Test, error) {
Line: fcbLineNumber(commandFCB, source),
})
commandFCB = nil
case "mdtest-go-example":
tests = append(tests, &Test{
GoExample: fcbLines(fcb, source),
Line: fcbLineNumber(fcb, source),
})
}
return ast.WalkContinue, nil
})
Expand Down
36 changes: 30 additions & 6 deletions mdtest/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@ package mdtest
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pmezard/go-difflib/difflib"
)

// Test represents a single test in a Markdown file.
type Test struct {
Command string
Dir string
Expected string
Fails bool
Head bool
Line int
Command string
Dir string
Expected string
Fails bool
Head bool
Line int
GoExample string
}

// Run runs the test, returning nil on success.
func (t *Test) Run() error {
if t.GoExample != "" {
return t.vetGoExample()
}
c := exec.Command("bash", "-e", "-o", "pipefail")
c.Dir = t.Dir
c.Stdin = strings.NewReader(t.Command)
Expand Down Expand Up @@ -57,3 +63,21 @@ func (t *Test) Run() error {
}
return nil
}

func (t *Test) vetGoExample() error {
dir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "main.go")
if err := os.WriteFile(path, []byte(t.GoExample), 0666); err != nil {
return err
}
_, err = exec.Command("go", "vet", path).Output()
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return fmt.Errorf("could not vet go example: %s", string(exitErr.Stderr))
}
return err
}

0 comments on commit c014c91

Please sign in to comment.