Make test temp files in the Cargo target directory, if known

This commit is contained in:
Nick Cameron 2018-05-03 11:14:01 +12:00
parent 36c49d703a
commit 4a4916920f

View File

@ -859,23 +859,27 @@ fn get_code_blocks() -> Vec<ConfigCodeBlock> {
} }
struct TempFile { struct TempFile {
file_name: &'static str, path: PathBuf,
} }
fn make_temp_file(file_name: &'static str) -> TempFile { fn make_temp_file(file_name: &'static str) -> TempFile {
use std::env::var;
use std::fs::File; 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"; let content = "fn main() {}\n";
file.write_all(content.as_bytes()) file.write_all(content.as_bytes())
.expect("Couldn't write temp file"); .expect("Couldn't write temp file");
TempFile { file_name } TempFile { path }
} }
impl Drop for TempFile { impl Drop for TempFile {
fn drop(&mut self) { fn drop(&mut self) {
use std::fs::remove_file; 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");
} }
} }