Skip to content

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)
}
```
}
}

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

}

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