use std::env; use std::ffi::{OsStr, OsString}; use std::fs::File; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use tempfile::{tempdir, Builder, NamedTempFile, TempPath};
#[test] fn test_prefix() { let tmpfile = NamedTempFile::with_prefix("prefix").unwrap(); let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.starts_with("prefix"));
}
{ // Try opening it at the new path. letmut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap(); letmut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
std::fs::remove_file(&persist_path).unwrap();
}
#[test] fn test_persist_noclobber() { letmut tmpfile = NamedTempFile::new().unwrap(); let old_path = tmpfile.path().to_path_buf(); let persist_target = NamedTempFile::new().unwrap(); let persist_path = persist_target.path().to_path_buf();
write!(tmpfile, "abcde").unwrap();
assert!(exists(&old_path));
{
tmpfile = tmpfile.persist_noclobber(&persist_path).unwrap_err().into();
assert!(exists(&old_path));
std::fs::remove_file(&persist_path).unwrap();
drop(persist_target);
}
tmpfile.persist_noclobber(&persist_path).unwrap(); // Try opening it at the new path. letmut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap(); letmut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
std::fs::remove_file(&persist_path).unwrap();
}
#[test] fn test_customnamed() { let tmpfile = Builder::new()
.prefix("tmp")
.suffix(&".rs")
.rand_bytes(12)
.tempfile()
.unwrap(); let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.starts_with("tmp"));
assert!(name.ends_with(".rs"));
assert_eq!(name.len(), 18);
}
{ // Try opening it at the new path. letmut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap(); letmut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
// Try opening it at the new path. letmut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap(); letmut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
std::fs::remove_file(&persist_path).unwrap();
}
#[test] fn temp_path_from_existing() { let tmp_dir = tempdir().unwrap(); let tmp_file_path_1 = tmp_dir.path().join("testfile1"); let tmp_file_path_2 = tmp_dir.path().join("testfile2");
File::create(&tmp_file_path_1).unwrap();
assert!(tmp_file_path_1.exists(), "Test file 1 hasn't been created");
File::create(&tmp_file_path_2).unwrap();
assert!(tmp_file_path_2.exists(), "Test file 2 hasn't been created");
let tmp_path = TempPath::from_path(&tmp_file_path_1);
assert!(
tmp_file_path_1.exists(), "Test file has been deleted before dropping TempPath"
);
drop(tmp_path);
assert!(
!tmp_file_path_1.exists(), "Test file exists after dropping TempPath"
);
assert!(
tmp_file_path_2.exists(), "Test file 2 has been deleted before dropping TempDir"
);
}
#[test] #[allow(unreachable_code)] fn temp_path_from_argument_types() { // This just has to compile return;
// Show that an FnMut can be used. let tmpfile = Builder::new()
.make(|path| {
count += 1;
File::create(path)
})
.unwrap();
assert!(tmpfile.path().is_file());
}
#[cfg(unix)] #[test] fn test_make_uds() { use std::os::unix::net::UnixListener;
let temp_sock = Builder::new()
.prefix("tmp")
.suffix(".sock")
.rand_bytes(12)
.make(|path| UnixListener::bind(path))
.unwrap();
assert!(temp_sock.path().exists());
}
#[cfg(unix)] #[test] fn test_make_uds_conflict() { use std::os::unix::net::UnixListener; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc;
// Check that retries happen correctly by racing N different threads.
const NTHREADS: usize = 20;
// The number of times our callback was called. let tries = Arc::new(AtomicUsize::new(0));
letmut threads = Vec::with_capacity(NTHREADS);
for _ in0..NTHREADS { let tries = tries.clone();
threads.push(std::thread::spawn(move || { // Ensure that every thread uses the same seed so we are guaranteed // to retry. Note that fastrand seeds are thread-local.
fastrand::seed(42);
// Join all threads, but don't drop the temp file yet. Otherwise, we won't // get a deterministic number of `tries`. let sockets: Vec<_> = threads
.into_iter()
.map(|thread| thread.join().unwrap().unwrap())
.collect();
// Number of tries is exactly equal to (n*(n+1))/2.
assert_eq!(
tries.load(Ordering::Relaxed),
(NTHREADS * (NTHREADS + 1)) / 2
);
for socket in sockets {
assert!(socket.path().exists());
}
}
// Don't really need to run this. Only care if it compiles. iflet Ok(file) = File::open("i_do_not_exist") { letmut f; let _x = {
f = Foo::new(file);
&mut f
};
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.