Make test temp files in the Cargo target directory, if known
This commit is contained in:
parent
36c49d703a
commit
4a4916920f
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user