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

Accepting Changes Automatically #590

Open
mbechto opened this issue Nov 8, 2024 · 3 comments
Open

Accepting Changes Automatically #590

mbechto opened this issue Nov 8, 2024 · 3 comments

Comments

@mbechto
Copy link

mbechto commented Nov 8, 2024

Hi,

I am using ApprovalTests.Java for a while now and was wondering whether you would be open to a feature request (or tell me how I can improve my development workflow).

Having done some Rust recently, I am used to the cargo-insta snapshot testing library. One thing I like about insta is, that it displays the diff directly in the terminal where cargo build is executed and you are presented with the option to accept or reject the changes with a key-press (y/n).

With ApprovalTests the diff view - in my case in IntelliJ - opens, which is fine in most cases. But with an increasing number of changed files and diffs, this causes a cascade of many diff windows during each change.

It may be just how I like to do it, but in my daily workflow I like to review all changes in git before I commit. So to me, clicking all those diff windows feels tedious - where I would just press "y" in insta.

I thought about automating this process by scripting it, something along the lines of for f in *.received.* ; do mv "$f" "${f/.received/.approved}"; done but this feels a little awkward.

Proposal: Add a convenience feature to accept all changes automatically.

Implementation idea: A system property e.g. provided at build time:

mvn verify -Dapprovaltests.acceptAllChanges=true

or environment variable

APPROVALTESTS_ACCEPT_ALL_CHANGES=true mvn verify

that causes ApprovalTests to override the *.approved.* files with their changes without prompting.

@LarsEckart
Copy link
Contributor

LarsEckart commented Nov 9, 2024

hello, I think we have exactly what you are looking for. You'd want to use a different Reporter. Iirc the class name would be AutoApproveReporter. You can apply that either

  • via annotation on the test classes or methods, @UseReporter(AutoApproveReporter.class),
  • or pass it with theOptions, Approvals.verify(actual, new Options().withReporter(new AutoApproveReporter()),
  • use the following reporter: @UseReporter(EnvironmentVariableReporter.class) (as above, can also be through actions) and then when running export APPROVAL_TESTS_USE_REPORTER=AutoApproveReporter and then mvn verify. The -D won't work as env variables are different from system properties and we don't have support for that (yet).

Replying from iPad here currently, I'll have a proper look later and will update my comment where I didn't remember the syntax correctly anymore.

Lars

@LarsEckart
Copy link
Contributor

Updated my answer, let me know if there are still issues or if it's not quite what you have in mind.

Grüße aus Estland ;)

PS: oddly enough, the 2nd options with Options wont even report that a test failed, it approves the file and mrks the test as passing, the other 2 options dont. Guess that is something we explore the next time @isidore and I work together (beginning of December). So let's not close this issue yet, even if it's answered and maybe solves your problem.

@mbechto
Copy link
Author

mbechto commented Nov 14, 2024

I guess that works, thanks for the insightful response @LarsEckart! 🙂

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

No branches or pull requests

2 participants