Writing a mockable Filesystem trait in Rust without RefCell
pyk.sh·17h·
Discuss: Hacker News
🦀Rust Verification
Preview
Report Post

December 15, 2025

I’ve been working on the CLI for my smart contract fuzzer. Its main job is to find contract files, compile them and generate Rust bindings. This involves a lot of reading and writing to the disk.

Testing this is annoying. I didn’t want to mess with temp files or clean up directories after every test. I wanted to mock the filesystem to run everything in memory. My first attempt worked but the code was hard to read. Here is how I refactored it to be cleaner.

To do this I defined a Filesystem trait. This allows me to swap between a RealFilesystem that uses std::fs for the actual app and a TestFilesystem that uses a HashMap for my tests.

My first attempt at this abstraction worked but it felt wrong. It forced me to write code that was hard to reason a…

Similar Posts

Loading similar posts...