I went to an awful lot of trouble to apply roughly the same technique to test some code generation code [0]. Required pulling in a random dependency and hacking around for a while to make the output look right. Your solution is much slicker!
What you ended up with has two major advantages: ① it integrates with the Cargo test harness, so that Rust developers’ expectations will be met; and ② it does it in one compiler invocation. The test harness I describe would be quite slow for your sort of situation.
I’m not completely sold on using a separate crate and build.rs how you have, but it looks like it’ll yield a good usable result. A couple of related things you might be interested in: compiletest_rs, trybuild.
[0]: https://github.com/couchand/wayfinder/blob/1dc58a1130bc17941...