WIP: add support for registering source files#100
Open
rogpeppe wants to merge 1 commit intofrankban:masterfrom
Open
WIP: add support for registering source files#100rogpeppe wants to merge 1 commit intofrankban:masterfrom
rogpeppe wants to merge 1 commit intofrankban:masterfrom
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) } ```
dolmen
suggested changes
Jun 8, 2023
| } | ||
| } | ||
|
|
||
| func fileToPackage(fn string) string { |
Contributor
There was a problem hiding this comment.
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 { |
| } | ||
| data := registeredSourceForPackage(pkg, file) | ||
| if data == nil { | ||
| data1, err := ioutil.ReadFile(file) |
| if f == nil { | ||
| var err error | ||
| f, err = parser.ParseFile(sg.fset, file, nil, parser.ParseComments) | ||
| var registeredSourceForPackage = func(pkg, path string) []byte { |
Contributor
There was a problem hiding this comment.
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. |
Contributor
There was a problem hiding this comment.
Add that go1.16+ is required.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-generatecommand, which would generate code looking something like the following: