Skip to content

Instantly share code, notes, and snippets.

Created June 30, 2016 07:16
Show Gist options
  • Save anonymous/6eb80fef86a2887df68fd12335855c22 to your computer and use it in GitHub Desktop.
Save anonymous/6eb80fef86a2887df68fd12335855c22 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::path::Path;
#[cfg(test)]
thread_local! (pub static FAIL: AtomicBool = AtomicBool::new (false));
#[cfg(not(test))]
#[inline]
fn testable_rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> std::io::Result<()> {
std::fs::rename(from, to)
}
#[cfg(test)]
fn testable_rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> std::io::Result<()> {
let fail = FAIL.with(|f| f.load(Ordering::Relaxed));
if fail {
return Err(std::io::Error::new(ErrorKind::NotFound, "ups"));
}
std::fs::rename(from, to)
}
#[test]
fn test_something_that_uses_rename() {
FAIL.with(|f| f.store(true, Ordering::Relaxed));
// ...
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment