Skip to content

Commit 4a49169

Browse files
committed
Make test temp files in the Cargo target directory, if known
1 parent 36c49d7 commit 4a49169

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/test/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,23 +859,27 @@ fn configuration_snippet_tests() {
859859
}
860860

861861
struct TempFile {
862-
file_name: &'static str,
862+
path: PathBuf,
863863
}
864864

865865
fn make_temp_file(file_name: &'static str) -> TempFile {
866+
use std::env::var;
866867
use std::fs::File;
867868

868-
let mut file = File::create(file_name).expect("Couldn't create temp file");
869+
let target_dir = var("CARGO_TARGET_DIR").unwrap_or_else(|_| ".".to_owned());
870+
let path = Path::new(&target_dir).join(file_name);
871+
872+
let mut file = File::create(&path).expect("Couldn't create temp file");
869873
let content = "fn main() {}\n";
870874
file.write_all(content.as_bytes())
871875
.expect("Couldn't write temp file");
872-
TempFile { file_name }
876+
TempFile { path }
873877
}
874878

875879
impl Drop for TempFile {
876880
fn drop(&mut self) {
877881
use std::fs::remove_file;
878-
remove_file(self.file_name).expect("Couldn't delete temp file");
882+
remove_file(&self.path).expect("Couldn't delete temp file");
879883
}
880884
}
881885

0 commit comments

Comments
 (0)