❮ Testing with temporary directory passed as an environment variable (test in a single thread RUST_TEST_THREADS)
❯
- RefCell
- thread_local
Testing with temorary directory passed in a thread-local variable
examples/testing/tempfile-with-thread-local/Cargo.toml
[package] name = "tempfile-with-thread-local" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] rand = "0.8.5" tempfile = "3.9.0"
examples/testing/tempfile-with-thread-local/src/main.rs
use std::fs::File; use std::io::Write; use std::cell::RefCell; thread_local! { pub static RESULT_PATH: RefCell<String> = const { RefCell::new(String::new()) }; } fn main() {} #[allow(dead_code)] fn add(x: i32, y: i32) -> i32 { let time: u64 = rand::random(); let time = time % 3; std::thread::sleep(std::time::Duration::from_secs(time)); RESULT_PATH.with_borrow(|file_path| { let mut file = File::create(file_path).unwrap(); println!("add({x}, {y}) file {file_path}"); writeln!(&mut file, "{}", x + y).unwrap(); }); x + y } #[test] fn test_add_2_3_5() { println!("{:?}", std::thread::current().id()); let tmp_dir = tempfile::tempdir().unwrap(); println!("tmp_dir: {:?}", tmp_dir); let file_path = tmp_dir.path().join("result.txt"); println!("2+3 - file_path {:?}", file_path); let result_path = file_path.as_os_str().to_str().unwrap(); RESULT_PATH.with_borrow_mut(|v| v.push_str(result_path)); let result = add(2, 3); assert_eq!(result, 5); let result = std::fs::read_to_string(file_path).unwrap(); assert_eq!(result, "5\n"); } #[test] fn test_add_4_4_8() { println!("{:?}", std::thread::current().id()); let tmp_dir = tempfile::tempdir().unwrap(); println!("tmp_dir: {:?}", tmp_dir); let file_path = tmp_dir.path().join("result.txt"); println!("4+4 - file_path {:?}", file_path); let result_path = file_path.as_os_str().to_str().unwrap(); RESULT_PATH.with_borrow_mut(|v| v.push_str(result_path)); let result = add(4, 4); assert_eq!(result, 8); let result = std::fs::read_to_string(file_path).unwrap(); assert_eq!(result, "8\n"); }