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

Conversation

rogpeppe
Copy link
Contributor

Note: WORK IN PROGRESS

From an idea from Brad Fitzpatrick, this makes it possible to show source code in test failures even when the source code isn't currently available.

Still to do: the quicktest-generate command, which would generate code looking something like the following:

package {{.Pkg}}

import (
	"embed"
	"testing"

	qt "github.com/frankban/quicktest"
)

//go:embed *_test.go
var _quicktestFiles embed.FS

func init() {
	qt.RegisterSource({{.Package}}, _quicktestFiles)
}

From [an idea](https://twitter.com/bradfitz/status/1387817724634492928) from Brad Fitzpatrick, this makes it possible to show source code in test failures even when the source code isn't currently available.

Still to do: the `quicktest-generate` command, which would generate code looking something like the following:

```
package {{.Pkg}}

import (
	"embed"
	"testing"

	qt "github.com/frankban/quicktest"
)

//go:embed *_test.go
var _quicktestFiles embed.FS

func init() {
	qt.RegisterSource({{.Package}}, _quicktestFiles)
}
```
@@ -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".

@@ -141,22 +143,50 @@ func writeStack(w io.Writer) {
}
}

func fileToPackage(fn string) string {
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.

}
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 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.

// //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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants