From 4a4916920fed8221251d1942d01f17ce5f5a0fa0 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 3 May 2018 11:14:01 +1200 Subject: [PATCH] Make test temp files in the Cargo target directory, if known --- src/test/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index 4a02bf73e1a..ba0c75a1834 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -859,23 +859,27 @@ fn get_code_blocks() -> Vec { } struct TempFile { - file_name: &'static str, + path: PathBuf, } fn make_temp_file(file_name: &'static str) -> TempFile { + use std::env::var; use std::fs::File; - let mut file = File::create(file_name).expect("Couldn't create temp file"); + let target_dir = var("CARGO_TARGET_DIR").unwrap_or_else(|_| ".".to_owned()); + let path = Path::new(&target_dir).join(file_name); + + let mut file = File::create(&path).expect("Couldn't create temp file"); let content = "fn main() {}\n"; file.write_all(content.as_bytes()) .expect("Couldn't write temp file"); - TempFile { file_name } + TempFile { path } } impl Drop for TempFile { fn drop(&mut self) { use std::fs::remove_file; - remove_file(self.file_name).expect("Couldn't delete temp file"); + remove_file(&self.path).expect("Couldn't delete temp file"); } }