-
Notifications
You must be signed in to change notification settings - Fork 27
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
base: master
Are you sure you want to change the base?
Conversation
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
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: