Skip to content

Commit

Permalink
feat: postcompile (#217)
Browse files Browse the repository at this point in the history
postcompile is a crate for compiling rust code at runtime in unit tests. Its very useful when writing proc-macro snapshot tests to test the output of the generated code.
  • Loading branch information
TroyKomodo authored Dec 5, 2024
1 parent d211215 commit 03c9934
Show file tree
Hide file tree
Showing 11 changed files with 948 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"crates/metrics",
"crates/metrics/derive",
"crates/metrics/examples",
"crates/postcompile",
"crates/pprof",
"crates/pprof/examples",
"crates/settings",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This repository houses a collection of crates, purpose-built libraries designed
- 📈 **[scuffle-pprof](./crates/pprof)**: Helper crate for adding pprof support to your application.
- ⚙️ **[scuffle-settings](./crates/settings)**: Tools for managing configuration from environment variables or config files.
- 📶 **[scuffle-signal](./crates/signal)**: Ergonomic async signal handling.
- 📦 **[postcompile](./crates/postcompile)**: A macro for compiling Rust code at runtime. Useful for snapshot testing.

## 📦 Apps

Expand Down
28 changes: 28 additions & 0 deletions crates/postcompile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "postcompile"
version = "0.0.4"
edition = "2021"
build = "build.rs"
repository = "https://github.com/scufflecloud/scuffle"
authors = ["Scuffle <[email protected]>"]
readme = "README.md"
documentation = "https://docs.rs/postcompile"
license = "MIT OR Apache-2.0"
description = "Helper crate for post-compiling Rust code."
keywords = ["postcompile", "snapshot", "test", "proc-macro"]

[dependencies]
serde_json = "1.0"
cargo_metadata = "0.19.1"
cargo-platform = "0.1"
target-triple = "0.1"
serde_derive = "1.0"
serde = "1.0"
prettyplease = { version = "0.2", optional = true }
syn = { version = "2", features = ["full"] }

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(trybuild_no_target)', 'cfg(postcompile_no_target)'] }

[features]
prettyplease = ["dep:prettyplease"]
1 change: 1 addition & 0 deletions crates/postcompile/LICENSE.Apache-2.0
1 change: 1 addition & 0 deletions crates/postcompile/LICENSE.MIT
74 changes: 74 additions & 0 deletions crates/postcompile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# postcompile

> [!WARNING]
> This crate is under active development and may not be stable.
[![crates.io](https://img.shields.io/crates/v/postcompile.svg)](https://crates.io/crates/postcompile) [![docs.rs](https://img.shields.io/docsrs/postcompile)](https://docs.rs/postcompile)

---

A crate which allows you to compile Rust code at runtime (hence the name `postcompile`).

What that means is that you can provide the input to `rustc` and then get back the expanded output, compiler errors, warnings, etc.

This is particularly useful when making snapshot tests of proc-macros, look below for an example with the `insta` crate.

## Usage

```rs
#[test]
fn some_cool_test() {
insta::assert_snapshot!(postcompile::compile! {
#![allow(unused)]

#[derive(Debug, Clone)]
struct Test {
a: u32,
b: i32,
}

const TEST: Test = Test { a: 1, b: 3 };
});
}

#[test]
fn some_cool_test_extern() {
insta::assert_snapshot!(postcompile::compile_str!(include_str!("some_file.rs")));
}
```

## Features

- Cached builds: This crate reuses the cargo build cache of the original crate so that only the contents of the macro are compiled & not any additional dependencies.
- Coverage: This crate works with [`cargo-llvm-cov`](https://crates.io/crates/cargo-llvm-cov) out of the box, which allows you to instrument the proc-macro expansion.

## Alternatives

- [`compiletest_rs`](https://crates.io/crates/compiletest_rs): This crate is used by the Rust compiler team to test the compiler itself. Not really useful for proc-macros.
- [`trybuild`](https://crates.io/crates/trybuild): This crate is an all-in-one solution for testing proc-macros, with built in snapshot testing.
- [`ui_test`](https://crates.io/crates/ui_test): Similar to `trybuild` with a slightly different API & used by the Rust compiler team to test the compiler itself.

### Differences

The other libraries are focused on testing & have built in test harnesses. This crate takes a step back and allows you to compile without a testing harness. This has the advantage of being more flexible, and allows you to use whatever testing framework you want.

In the examples above I showcase how to use this crate with the `insta` crate for snapshot testing.

## Status

This crate is currently under development and is not yet stable, unit tests are not yet fully implemented.

Unit tests are not yet fully implemented. Use at your own risk.

## Limitations

Please note that this crate does not work inside a running compiler process (inside a proc-macro) without hacky workarounds and complete build-cache invalidation.

This is because `cargo` holds a lock on the build directory and that if we were to compile inside a proc-macro we would recursively invoke the compiler.

## License

This project is licensed under the [MIT](./LICENSE.MIT) or [Apache-2.0](./LICENSE.Apache-2.0) license.
You can choose between one of them if you use this work.

`SPDX-License-Identifier: MIT OR Apache-2.0`
3 changes: 3 additions & 0 deletions crates/postcompile/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// This is a dummy build script, it does nothing.
/// But we need to get the `OUT_DIR` from the environment so that we can determine where the target directory is.
fn main() {}
Loading

0 comments on commit 03c9934

Please sign in to comment.