`file_exists!` Compile time check macro would be useful
⚓ Rust 📅 2025-10-24 👤 surdeus 👁️ 3When writing unit tests which loads some "fixture" from a file, e.g. a .json file it is incredible helpful to use include_str! to assert at compile time that the file exists. However, it increases the size of the test binary, right? so maybe that is not wanted.
I want a macro which checks for the presence of a file at compile time, but I want to load the actual contents of the file at runtime. I think no such macro exists to day?
And I want to to this inline in my unit test, i.e. no build script.
So I wanted to check if other would find this useful? Maybe called file_exists
Actually it would be neat with another macro - read_file! which would use file_exists! at compile time and use fs::read_to_string at runtime:
- at compile time checks for the existence of the file
- at runtime loads it contents
#[test]
fn test_deserialize_user() {
// This verifies the existince of the file at compile time, and reads its contents at runtime.
let fixture = read_file!("user.json");
let deserialization_result = serde_json::from_str(fixture)
assert!(deserialization_result.is_ok());
}
is that possible - if written part of std? Would anyone else beside me find it useful?
4 posts - 2 participants
🏷️ Rust_feed