Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add support for registering source files #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"go/printer"
"go/token"
"io"
"io/ioutil"
"log"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -127,7 +129,7 @@ func writeStack(w io.Writer) {
}
fmt.Fprint(w, prefixf(prefix, "%s:%d", frame.File, frame.Line))
if strings.HasSuffix(frame.File, ".go") {
stmt, err := sg.Get(frame.File, frame.Line)
stmt, err := sg.Get(fileToPackage(frame.Function), frame.File, frame.Line)
if err != nil {
fmt.Fprint(w, prefixf(prefix+prefix, "<%s>", err))
} else {
Expand All @@ -141,22 +143,50 @@ func writeStack(w io.Writer) {
}
}

func fileToPackage(fn string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to functionToPackage as the input is a "package path-qualified function name".

if i := strings.LastIndex(fn, "."); i >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use LastIndexByte.

return fn[0:i]
}
return ""
}

type stmtGetter struct {
fset *token.FileSet
files map[string]*ast.File
config *printer.Config
}

// Get returns the lines of code of the statement at the given file and line.
func (sg *stmtGetter) Get(file string, line int) (string, error) {
f := sg.files[file]
if f == nil {
var err error
f, err = parser.ParseFile(sg.fset, file, nil, parser.ParseComments)
var registeredSourceForPackage = func(pkg, path string) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be defined instead as a true func(pkg, path string) []byte (not a variable) in a sourceregister.go file with //+build !go1.16.

return nil
}

func (sg *stmtGetter) parseFile(pkg, file string) (*ast.File, error) {
if f := sg.files[file]; f != nil {
return f, nil
}
data := registeredSourceForPackage(pkg, file)
if data == nil {
data1, err := ioutil.ReadFile(file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use os.ReadFile.

if err != nil {
return "", fmt.Errorf("cannot parse source file: %s", err)
return nil, err
}
sg.files[file] = f
data = data1
} else {
log.Printf("got registered source for package %q, file %q", pkg, file)
}
f, err := parser.ParseFile(sg.fset, file, data, parser.ParseComments)
if err != nil {
return nil, err
}
sg.files[file] = f
return f, nil
}

// Get returns the lines of code of the statement at the given file and line.
func (sg *stmtGetter) Get(pkg string, file string, line int) (string, error) {
f, err := sg.parseFile(pkg, file)
if err != nil {
return "", fmt.Errorf("cannot parse source file: %s", err)
}
var stmt string
ast.Inspect(f, func(n ast.Node) bool {
Expand Down
46 changes: 46 additions & 0 deletions sourceregister_1.16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//+build go1.16

package quicktest

import (
"embed"
"fmt"
"path/filepath"
"sync"
)

var (
sourceRegisterMu sync.Mutex
sourceRegister = make(map[string]embed.FS)
)

func init() {
registeredSourceForPackage = func(pkg, path string) []byte {
sourceRegisterMu.Lock()
defer sourceRegisterMu.Unlock()
fs, ok := sourceRegister[pkg]
if !ok {
return nil
}
data, _ := fs.ReadFile(filepath.Base(path))
return data
}
}

// RegisterSource registers Go source files for the given package.
//
// You shouldn't usually need to call this function directly - instead
// use a "go generate" directive as follows:
//
// //go:generate quicktest-generate
//
// and use the "go generate" command to generate the small
// amount of boilerplate required.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add that go1.16+ is required.

func RegisterSource(pkg string, files embed.FS) {
sourceRegisterMu.Lock()
defer sourceRegisterMu.Unlock()
if _, ok := sourceRegister[pkg]; ok {
panic(fmt.Errorf("package source for %q registered more than once", pkg))
}
sourceRegister[pkg] = files
}