I'm in a awkward situation where I need to use the crate I'm writing to generate its test files. Those tests are then expected to be run as usual using cargo test. What is the best way to do that using cargo?
I cannot generate the tests using a build.rs script because I need the crate already compiled, and as far as I know there is no post-build script run by cargo (or is it? I haven't found any open PR/RFC about that). I could create a binary whose purpose is to generate the test files and run it manually, but it would pollute my bin folder where I already have a main binary executable, and I don't want to have it installed using cargo install.
For now I'm using an example to build the tests using cargo example, but it is not really ideal. So I am now wondering if someone out there has a better idea...
Here's one possible approach, that might, or might not work for you.
Write a test-generation as a test, which generates code into the source directory. If the generated code is the same as the code on disc, this tests passes. If the on-disk files differ, the test overwrites them and then fails.
Then, the first cargo test might fail if new tests should be re-generated. The second cargo test will pass. We do something close in rust-analyzer:
I like the idea. I actually have two libraries having this problem so I'm glad I asked the question here because from the two proposed solutions I think the workspace one is better for one project and your idea better for the other. Thanks!