Skip to content

Commit

Permalink
Test Go Library Examples
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 committed Apr 1, 2024
1 parent 2a487ee commit 153b785
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 6 additions & 6 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,7 @@ import (
func main() {
zctx := zed.NewContext()
reader := zsonio.NewReader(os.Stdin, zctx)
reader := zsonio.NewReader(zctx, os.Stdin)
for {
val, err := reader.Read()
if err != nil {
Expand All @@ -65,7 +65,7 @@ func main() {
}
s := val.Deref("s")
if s == nil {
s = zctx.Missing()
s = zctx.Missing().Ptr()
}
fmt.Println(zson.String(s))
}
Expand Down Expand Up @@ -105,7 +105,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 +129,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 @@ -149,7 +149,7 @@ func main() {
}
s := val.Deref("s")
if s == nil {
s = zctx.Missing()
s = zctx.Missing().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 153b785

Please sign in to comment.