feat: updated AssertWithTemplate to generate fixtures with variables when using the -update flag#44
Merged
sebdah merged 4 commits intosebdah:masterfrom Jul 9, 2025
Conversation
…when using the -update flag
Contributor
Author
|
@sebdah Sorry for the ping. Any thoughts on the open PRs? Would love to bring this into the go-task/task workflow if possible. |
Owner
|
Thanks for this, I'll take a look today/tomorrow. |
sebdah
requested changes
Jul 5, 2025
|
|
||
| // Meta takes any data structure and returns a map of the data structure's | ||
| // values to their paths. This allows us to replace values in the golden file | ||
| // with template variables that match the values. These values are |
| // Meta takes any data structure and returns a map of the data structure's | ||
| // values to their paths. This allows us to replace values in the golden file | ||
| // with template variables that match the values. These values are | ||
| func Meta(a interface{}) map[string]string { |
sebdah
approved these changes
Jul 9, 2025
Owner
sebdah
left a comment
There was a problem hiding this comment.
Awesome - thanks for addressing
Merged
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.
Fixes #14 and resolves the problems mentioned in go-task/task#2265.
Currently, if you have a golden file containing variables, when you run
go test . -update, the variables will be updated with the values given in the tests. This means that subsequent tests can fail depending on the data that is being passed in.This PR updates the behavior of the
AssertWithTemplatefunction so that it can generate new variables in the golden file based on the data type being passed in. This works by analysing the data using thereflectpackage and creating a map of values to their paths in the data type. The generated paths are based on go templating syntax so that when we update the golden file, we can perform abytes.ReplaceAllon the data.Example
go test -update ./...Generates:Notes
AssertWithTemplate?map[string]string{"A": "a"}will cause all instances of the letter "a" to be replaced. This could be undesirable. I would argue the tester should just choose more specific strings and the problem goes away.