chore: checkpoint before Python removal

This commit is contained in:
2026-03-26 22:33:59 +00:00
parent 683cec9307
commit e568ddf82a
29972 changed files with 11269302 additions and 2 deletions

16
vendor/tempfile/tests/env.rs vendored Normal file
View File

@@ -0,0 +1,16 @@
#![deny(rust_2018_idioms)]
use std::path::Path;
#[test]
fn test_override_temp_dir() {
#[cfg(not(target_os = "wasi"))]
assert_eq!(tempfile::env::temp_dir(), std::env::temp_dir());
let new_tmp = Path::new("/tmp/override");
tempfile::env::override_temp_dir(new_tmp).unwrap();
assert_eq!(tempfile::env::temp_dir(), new_tmp);
let new_tmp2 = Path::new("/tmp/override2");
tempfile::env::override_temp_dir(new_tmp2).expect_err("override should only be possible once");
}

694
vendor/tempfile/tests/namedtempfile.rs vendored Normal file
View File

@@ -0,0 +1,694 @@
#![deny(rust_2018_idioms)]
use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use tempfile::{env, tempdir, Builder, NamedTempFile, TempPath};
struct CWDGuard {
#[allow(unused)]
guard: std::sync::MutexGuard<'static, ()>,
old_cwd: PathBuf,
}
impl Drop for CWDGuard {
fn drop(&mut self) {
let _ = std::env::set_current_dir(&self.old_cwd);
}
}
static CWD_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn cwd_lock() -> CWDGuard {
let guard = CWD_LOCK.lock().unwrap();
let old_cwd = std::env::current_dir().unwrap();
CWDGuard { guard, old_cwd }
}
fn exists<P: AsRef<Path>>(path: P) -> bool {
std::fs::metadata(path.as_ref()).is_ok()
}
/// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
/// directory instead as the base directory for temp files.
fn configure_wasi_temp_dir() {
if cfg!(target_os = "wasi") {
let _ = tempfile::env::override_temp_dir(Path::new("/tmp"));
}
}
#[test]
fn test_prefix() {
configure_wasi_temp_dir();
let tmpfile = NamedTempFile::with_prefix("prefix").unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.starts_with("prefix"));
}
#[test]
fn test_suffix() {
configure_wasi_temp_dir();
let tmpfile = NamedTempFile::with_suffix("suffix").unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.ends_with("suffix"));
}
#[test]
fn test_basic() {
configure_wasi_temp_dir();
let mut tmpfile = NamedTempFile::new().unwrap();
write!(tmpfile, "abcde").unwrap();
tmpfile.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
tmpfile.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
#[test]
fn test_deleted() {
configure_wasi_temp_dir();
let tmpfile = NamedTempFile::new().unwrap();
let path = tmpfile.path().to_path_buf();
assert!(exists(&path));
drop(tmpfile);
assert!(!exists(&path));
}
#[test]
fn test_persist() {
configure_wasi_temp_dir();
let mut tmpfile = NamedTempFile::new().unwrap();
let old_path = tmpfile.path().to_path_buf();
let persist_path = env::temp_dir().join("persisted_temporary_file");
write!(tmpfile, "abcde").unwrap();
{
assert!(exists(&old_path));
let mut f = tmpfile.persist(&persist_path).unwrap();
assert!(!exists(&old_path));
// Check original file
f.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
{
// Try opening it at the new path.
let mut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
let mut 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() {
configure_wasi_temp_dir();
let mut 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.
let mut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
let mut 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() {
configure_wasi_temp_dir();
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);
}
#[test]
fn test_append() {
configure_wasi_temp_dir();
let mut tmpfile = Builder::new().append(true).tempfile().unwrap();
tmpfile.write_all(b"a").unwrap();
tmpfile.seek(SeekFrom::Start(0)).unwrap();
tmpfile.write_all(b"b").unwrap();
tmpfile.seek(SeekFrom::Start(0)).unwrap();
let mut buf = vec![0u8; 1];
tmpfile.read_exact(&mut buf).unwrap();
assert_eq!(buf, b"a");
}
#[test]
fn test_reopen() {
configure_wasi_temp_dir();
let source = NamedTempFile::new().unwrap();
let mut first = source.reopen().unwrap();
let mut second = source.reopen().unwrap();
drop(source);
write!(first, "abcde").expect("write failed");
let mut buf = String::new();
second.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
#[test]
fn test_into_file() {
configure_wasi_temp_dir();
let mut file = NamedTempFile::new().unwrap();
let path = file.path().to_owned();
write!(file, "abcde").expect("write failed");
assert!(path.exists());
let mut file = file.into_file();
assert!(!path.exists());
file.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
#[test]
fn test_immut() {
configure_wasi_temp_dir();
let tmpfile = NamedTempFile::new().unwrap();
(&tmpfile).write_all(b"abcde").unwrap();
(&tmpfile).seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
(&tmpfile).read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
#[test]
fn test_temppath() {
configure_wasi_temp_dir();
let mut tmpfile = NamedTempFile::new().unwrap();
write!(tmpfile, "abcde").unwrap();
let path = tmpfile.into_temp_path();
assert!(path.is_file());
}
#[test]
fn test_temppath_persist() {
configure_wasi_temp_dir();
let mut tmpfile = NamedTempFile::new().unwrap();
write!(tmpfile, "abcde").unwrap();
let tmppath = tmpfile.into_temp_path();
let old_path = tmppath.to_path_buf();
let persist_path = env::temp_dir().join("persisted_temppath_file");
{
assert!(exists(&old_path));
tmppath.persist(&persist_path).unwrap();
assert!(!exists(&old_path));
}
{
// Try opening it at the new path.
let mut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
std::fs::remove_file(&persist_path).unwrap();
}
#[test]
fn test_temppath_persist_noclobber() {
configure_wasi_temp_dir();
let mut tmpfile = NamedTempFile::new().unwrap();
write!(tmpfile, "abcde").unwrap();
let mut tmppath = tmpfile.into_temp_path();
let old_path = tmppath.to_path_buf();
let persist_target = NamedTempFile::new().unwrap();
let persist_path = persist_target.path().to_path_buf();
assert!(exists(&old_path));
{
tmppath = tmppath.persist_noclobber(&persist_path).unwrap_err().into();
assert!(exists(&old_path));
std::fs::remove_file(&persist_path).unwrap();
drop(persist_target);
}
tmppath.persist_noclobber(&persist_path).unwrap();
// Try opening it at the new path.
let mut f = File::open(&persist_path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
let mut 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() {
configure_wasi_temp_dir();
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::try_from_path(&tmp_file_path_1).unwrap();
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;
TempPath::try_from_path("").unwrap();
TempPath::try_from_path(String::new()).unwrap();
TempPath::try_from_path(OsStr::new("")).unwrap();
TempPath::try_from_path(OsString::new()).unwrap();
TempPath::try_from_path(Path::new("")).unwrap();
TempPath::try_from_path(PathBuf::new()).unwrap();
TempPath::try_from_path(PathBuf::new().into_boxed_path()).unwrap();
}
// This test only works on platforms where we can safely delete the current
// working directory.
#[test]
#[cfg(not(any(target_os = "redox", target_os = "wasi", windows)))]
fn test_temp_path_resolve_missing_cwd() {
configure_wasi_temp_dir();
let _guard = cwd_lock();
// Intentionally delete the current working directory
let tmpdir = tempdir().unwrap();
std::env::set_current_dir(&tmpdir).expect("failed to change to the temporary directory");
tmpdir.close().unwrap();
#[allow(deprecated)]
let path = TempPath::from_path("foo");
assert_eq!(&*path, Path::new("foo"));
TempPath::try_from_path("foo").expect_err("should have failed to make path absolute file");
}
#[test]
fn test_temp_path_resolve_existing_cwd() {
configure_wasi_temp_dir();
let _guard = cwd_lock();
let tmpdir = tempdir().unwrap();
std::env::set_current_dir(&tmpdir).expect("failed to change to directory");
let cwd = if cfg!(target_os = "macos") {
// MacOS has absolute paths and ABSOLUTE paths. `cd /var/tmp/...` actually changes to
// /private/var/tmp...
std::env::current_dir().expect("failed to get the current directory")
} else {
tmpdir.path().to_owned()
};
#[allow(deprecated)]
let path = TempPath::from_path("foo");
assert_eq!(&*path, cwd.join("foo"));
#[allow(deprecated)]
let path = TempPath::from_path("");
assert_eq!(&*path, Path::new(""));
TempPath::try_from_path("").expect_err("empty paths should fail");
}
#[test]
fn test_write_after_close() {
configure_wasi_temp_dir();
let path = NamedTempFile::new().unwrap().into_temp_path();
File::create(path).unwrap().write_all(b"test").unwrap();
}
#[test]
fn test_change_dir() {
configure_wasi_temp_dir();
let _guard = cwd_lock();
let dir_a = tempdir().unwrap();
let dir_b = tempdir().unwrap();
std::env::set_current_dir(&dir_a).expect("failed to change to directory A");
let tmpfile = NamedTempFile::new_in(".").unwrap();
let path = std::env::current_dir().unwrap().join(tmpfile.path());
std::env::set_current_dir(&dir_b).expect("failed to change to directory B");
drop(tmpfile);
assert!(!exists(path));
drop(dir_a);
drop(dir_b);
}
#[test]
fn test_change_dir_make() {
configure_wasi_temp_dir();
let _guard = cwd_lock();
let dir_a = tempdir().unwrap();
let dir_b = tempdir().unwrap();
std::env::set_current_dir(&dir_a).expect("failed to change to directory A");
let tmpfile = Builder::new().make_in(".", |p| File::create(p)).unwrap();
let path = std::env::current_dir().unwrap().join(tmpfile.path());
std::env::set_current_dir(&dir_b).expect("failed to change to directory B");
drop(tmpfile);
assert!(!exists(path));
drop(dir_a);
drop(dir_b);
}
#[test]
fn test_into_parts() {
configure_wasi_temp_dir();
let mut file = NamedTempFile::new().unwrap();
write!(file, "abcd").expect("write failed");
let (mut file, temp_path) = file.into_parts();
let path = temp_path.to_path_buf();
assert!(path.exists());
drop(temp_path);
assert!(!path.exists());
write!(file, "efgh").expect("write failed");
file.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
assert_eq!("abcdefgh", buf);
}
#[test]
fn test_from_parts() {
configure_wasi_temp_dir();
let mut file = NamedTempFile::new().unwrap();
write!(file, "abcd").expect("write failed");
let (file, temp_path) = file.into_parts();
let file = NamedTempFile::from_parts(file, temp_path);
assert!(file.path().exists());
}
#[test]
fn test_keep() {
configure_wasi_temp_dir();
let mut tmpfile = NamedTempFile::new().unwrap();
write!(tmpfile, "abcde").unwrap();
let (mut f, temp_path) = tmpfile.into_parts();
let path;
{
assert!(exists(&temp_path));
path = temp_path.keep().unwrap();
assert!(exists(&path));
// Check original file
f.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
{
// Try opening it again.
let mut f = File::open(&path).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
std::fs::remove_file(&path).unwrap();
}
#[test]
fn test_disable_cleanup() {
configure_wasi_temp_dir();
// Case 0: never mark as "disable cleanup"
// Case 1: enable "disable cleanup" in the builder, don't touch it after.
// Case 2: enable "disable cleanup" in the builder, turn it off after.
// Case 3: don't enable disable cleanup in the builder, turn it on after.
for case in 0..4 {
let in_builder = case & 1 > 0;
let toggle = case & 2 > 0;
let mut tmpfile = Builder::new()
.disable_cleanup(in_builder)
.tempfile()
.unwrap();
write!(tmpfile, "abcde").unwrap();
if toggle {
tmpfile.disable_cleanup(!in_builder);
}
let path = tmpfile.path().to_owned();
drop(tmpfile);
if in_builder ^ toggle {
// Try opening it again.
let mut f = File::open(&path).unwrap();
let mut buf = String::new();
f.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
std::fs::remove_file(&path).unwrap();
} else {
assert!(!path.exists(), "tempfile wasn't deleted");
}
}
}
#[test]
fn test_make() {
configure_wasi_temp_dir();
let tmpfile = Builder::new().make(|path| File::create(path)).unwrap();
assert!(tmpfile.path().is_file());
}
#[test]
fn test_make_in() {
configure_wasi_temp_dir();
let tmp_dir = tempdir().unwrap();
let tmpfile = Builder::new()
.make_in(tmp_dir.path(), |path| File::create(path))
.unwrap();
assert!(tmpfile.path().is_file());
assert_eq!(tmpfile.path().parent(), Some(tmp_dir.path()));
}
#[test]
fn test_make_fnmut() {
configure_wasi_temp_dir();
let mut count = 0;
// 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());
}
// This works(ish) on redox, but it's really slow.
#[cfg(all(unix, not(target_os = "redox")))]
#[test]
fn test_make_uds_conflict() {
use std::io::ErrorKind;
use std::os::unix::net::UnixListener;
let sockets = std::iter::repeat_with(|| {
Builder::new()
.prefix("tmp")
.suffix(".sock")
.rand_bytes(1)
.make(|path| UnixListener::bind(path))
})
.take_while(|r| match r {
Ok(_) => true,
Err(e) if matches!(e.kind(), ErrorKind::AddrInUse | ErrorKind::AlreadyExists) => false,
Err(e) => panic!("unexpected error {e}"),
})
.collect::<Result<Vec<_>, _>>()
.unwrap();
// Number of sockets we can create. Depends on whether or not the filesystem is case sensitive.
#[cfg(target_os = "macos")]
const NUM_FILES: usize = 36;
#[cfg(not(target_os = "macos"))]
const NUM_FILES: usize = 62;
assert_eq!(sockets.len(), NUM_FILES);
for socket in sockets {
assert!(socket.path().exists());
}
}
/// Make sure we re-seed with system randomness if we run into a conflict.
#[test]
fn test_reseed() {
configure_wasi_temp_dir();
// Deterministic seed.
fastrand::seed(42);
// I need to create 5 conflicts but I can't just make 5 temporary files because we fork the RNG
// each time we create a file.
let mut attempts = 0;
let mut files: Vec<_> = Vec::new();
let _ = Builder::new().make(|path| -> io::Result<File> {
if attempts == 5 {
return Err(io::Error::new(io::ErrorKind::Other, "stop!"));
}
attempts += 1;
let f = File::options()
.write(true)
.create_new(true)
.open(path)
.unwrap();
files.push(NamedTempFile::from_parts(
f,
TempPath::try_from_path(path).unwrap(),
));
Err(io::Error::new(io::ErrorKind::AlreadyExists, "fake!"))
});
assert_eq!(5, attempts);
attempts = 0;
// Re-seed to cause a conflict.
fastrand::seed(42);
let _f = Builder::new()
.make(|path| {
attempts += 1;
File::options().write(true).create_new(true).open(path)
})
.unwrap();
// We expect exactly three conflict before we re-seed with system randomness.
assert_eq!(4, attempts);
}
// Issue #224.
#[test]
fn test_overly_generic_bounds() {
pub struct Foo<T>(T);
impl<T> Foo<T>
where
T: Sync + Send + 'static,
for<'a> &'a T: Write + Read,
{
pub fn new(foo: T) -> Self {
Self(foo)
}
}
// Don't really need to run this. Only care if it compiles.
if let Ok(file) = File::open("i_do_not_exist") {
let mut f;
let _x = {
f = Foo::new(file);
&mut f
};
}
}

377
vendor/tempfile/tests/spooled.rs vendored Normal file
View File

@@ -0,0 +1,377 @@
#![deny(rust_2018_idioms)]
use std::io::{Read, Seek, SeekFrom, Write};
use tempfile::{env, spooled_tempfile, spooled_tempfile_in, SpooledTempFile};
/// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
/// directory instead as the base directory for temp files.
fn configure_wasi_temp_dir() {
if cfg!(target_os = "wasi") {
let _ = tempfile::env::override_temp_dir(std::path::Path::new("/tmp"));
}
}
#[test]
fn test_automatic_rollover() {
configure_wasi_temp_dir();
let mut t = spooled_tempfile(10);
let mut buf = Vec::new();
assert!(!t.is_rolled());
assert_eq!(t.stream_position().unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
assert_eq!(buf.as_slice(), b"");
buf.clear();
assert_eq!(t.write(b"abcde").unwrap(), 5);
assert!(!t.is_rolled());
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 5);
assert_eq!(buf.as_slice(), b"abcde");
assert_eq!(t.write(b"fghijklmno").unwrap(), 10);
assert_eq!(t.stream_position().unwrap(), 15);
assert!(t.is_rolled());
}
#[test]
fn test_custom_dir() {
configure_wasi_temp_dir();
{
let mut t = spooled_tempfile_in(10, env::temp_dir());
t.roll()
.expect("failed to roll temp file in a specified directory");
}
{
let mut t = spooled_tempfile_in(10, "/does-not-exist/");
t.roll()
.expect_err("should fail to roll the temporary file into a nonexistent tempdir");
}
}
#[test]
fn test_explicit_rollover() {
configure_wasi_temp_dir();
let mut t = SpooledTempFile::new(100);
assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
assert_eq!(t.stream_position().unwrap(), 26);
assert!(!t.is_rolled());
// roll over explicitly
assert!(t.roll().is_ok());
assert!(t.is_rolled());
assert_eq!(t.stream_position().unwrap(), 26);
let mut buf = Vec::new();
assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
assert_eq!(buf.as_slice(), b"");
buf.clear();
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 26);
assert_eq!(buf.as_slice(), b"abcdefghijklmnopqrstuvwxyz");
assert_eq!(t.stream_position().unwrap(), 26);
}
// called by test_seek_{buffer, file}
// assumes t is empty and offset is 0 to start
fn test_seek(t: &mut SpooledTempFile) {
configure_wasi_temp_dir();
assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
assert_eq!(t.stream_position().unwrap(), 26); // tell()
assert_eq!(t.seek(SeekFrom::Current(-1)).unwrap(), 25);
assert_eq!(t.seek(SeekFrom::Current(1)).unwrap(), 26);
assert_eq!(t.seek(SeekFrom::Current(1)).unwrap(), 27);
assert_eq!(t.seek(SeekFrom::Current(-27)).unwrap(), 0);
assert!(t.seek(SeekFrom::Current(-1)).is_err());
assert!(t.seek(SeekFrom::Current(-1245)).is_err());
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.seek(SeekFrom::Start(1)).unwrap(), 1);
assert_eq!(t.seek(SeekFrom::Start(26)).unwrap(), 26);
assert_eq!(t.seek(SeekFrom::Start(27)).unwrap(), 27);
// // these are build errors
// assert!(t.seek(SeekFrom::Start(-1)).is_err());
// assert!(t.seek(SeekFrom::Start(-1000)).is_err());
assert_eq!(t.seek(SeekFrom::End(0)).unwrap(), 26);
assert_eq!(t.seek(SeekFrom::End(-1)).unwrap(), 25);
assert_eq!(t.seek(SeekFrom::End(-26)).unwrap(), 0);
assert!(t.seek(SeekFrom::End(-27)).is_err());
assert!(t.seek(SeekFrom::End(-99)).is_err());
assert_eq!(t.seek(SeekFrom::End(1)).unwrap(), 27);
assert_eq!(t.seek(SeekFrom::End(1)).unwrap(), 27);
}
#[test]
fn test_seek_buffer() {
configure_wasi_temp_dir();
let mut t = spooled_tempfile(100);
test_seek(&mut t);
}
#[test]
fn test_seek_file() {
configure_wasi_temp_dir();
let mut t = SpooledTempFile::new(10);
test_seek(&mut t);
}
fn test_seek_read(t: &mut SpooledTempFile) {
configure_wasi_temp_dir();
assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
let mut buf = Vec::new();
// we're at the end
assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
assert_eq!(buf.as_slice(), b"");
buf.clear();
// seek to start, read whole thing
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 26);
assert_eq!(buf.as_slice(), b"abcdefghijklmnopqrstuvwxyz");
buf.clear();
// now we're at the end again
assert_eq!(t.stream_position().unwrap(), 26); // tell()
assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
assert_eq!(buf.as_slice(), b"");
buf.clear();
// seek to somewhere in the middle, read a bit
assert_eq!(t.seek(SeekFrom::Start(5)).unwrap(), 5);
let mut buf = [0; 5];
assert!(t.read_exact(&mut buf).is_ok());
assert_eq!(buf, *b"fghij");
// read again from current spot
assert_eq!(t.stream_position().unwrap(), 10); // tell()
assert!(t.read_exact(&mut buf).is_ok());
assert_eq!(buf, *b"klmno");
let mut buf = [0; 15];
// partial read
assert_eq!(t.read(&mut buf).unwrap(), 11);
assert_eq!(buf[0..11], *b"pqrstuvwxyz");
// try to read off the end: UnexpectedEof
assert!(t.read_exact(&mut buf).is_err());
}
#[test]
fn test_seek_read_buffer() {
configure_wasi_temp_dir();
let mut t = spooled_tempfile(100);
test_seek_read(&mut t);
}
#[test]
fn test_seek_read_file() {
configure_wasi_temp_dir();
let mut t = SpooledTempFile::new(10);
test_seek_read(&mut t);
}
fn test_overwrite_middle(t: &mut SpooledTempFile) {
configure_wasi_temp_dir();
assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
assert_eq!(t.seek(SeekFrom::Start(10)).unwrap(), 10);
assert_eq!(t.write(b"0123456789").unwrap(), 10);
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
let mut buf = Vec::new();
assert_eq!(t.read_to_end(&mut buf).unwrap(), 26);
assert_eq!(buf.as_slice(), b"abcdefghij0123456789uvwxyz");
}
#[test]
fn test_overwrite_middle_of_buffer() {
let mut t = spooled_tempfile(100);
test_overwrite_middle(&mut t);
}
#[test]
fn test_overwrite_middle_of_file() {
let mut t = SpooledTempFile::new(10);
test_overwrite_middle(&mut t);
}
#[test]
fn test_overwrite_and_extend_buffer() {
let mut t = spooled_tempfile(100);
assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
assert_eq!(t.seek(SeekFrom::End(-5)).unwrap(), 21);
assert_eq!(t.write(b"0123456789").unwrap(), 10);
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
let mut buf = Vec::new();
assert_eq!(t.read_to_end(&mut buf).unwrap(), 31);
assert_eq!(buf.as_slice(), b"abcdefghijklmnopqrstu0123456789");
assert!(!t.is_rolled());
}
#[test]
fn test_overwrite_and_extend_rollover() {
configure_wasi_temp_dir();
let mut t = SpooledTempFile::new(20);
assert_eq!(t.write(b"abcdefghijklmno").unwrap(), 15);
assert!(!t.is_rolled());
assert_eq!(t.seek(SeekFrom::End(-5)).unwrap(), 10);
assert_eq!(t.stream_position().unwrap(), 10); // tell()
assert!(!t.is_rolled());
assert_eq!(t.write(b"0123456789)!@#$%^&*(").unwrap(), 20);
assert!(t.is_rolled());
assert_eq!(t.stream_position().unwrap(), 30); // tell()
let mut buf = Vec::new();
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 30);
assert_eq!(buf.as_slice(), b"abcdefghij0123456789)!@#$%^&*(");
}
fn test_sparse(t: &mut SpooledTempFile) {
assert_eq!(t.write(b"abcde").unwrap(), 5);
assert_eq!(t.seek(SeekFrom::Current(5)).unwrap(), 10);
assert_eq!(t.write(b"klmno").unwrap(), 5);
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
let mut buf = Vec::new();
assert_eq!(t.read_to_end(&mut buf).unwrap(), 15);
assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0klmno");
}
#[test]
fn test_sparse_buffer() {
let mut t = spooled_tempfile(100);
test_sparse(&mut t);
}
#[test]
fn test_sparse_file() {
configure_wasi_temp_dir();
let mut t = SpooledTempFile::new(1);
test_sparse(&mut t);
}
#[test]
fn test_sparse_write_rollover() {
configure_wasi_temp_dir();
let mut t = spooled_tempfile(10);
assert_eq!(t.write(b"abcde").unwrap(), 5);
assert!(!t.is_rolled());
assert_eq!(t.seek(SeekFrom::Current(5)).unwrap(), 10);
assert!(!t.is_rolled());
assert_eq!(t.write(b"klmno").unwrap(), 5);
assert!(t.is_rolled());
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
let mut buf = Vec::new();
assert_eq!(t.read_to_end(&mut buf).unwrap(), 15);
assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0klmno");
}
fn test_set_len(t: &mut SpooledTempFile) {
let mut buf: Vec<u8> = Vec::new();
assert_eq!(t.write(b"abcdefghijklmnopqrstuvwxyz").unwrap(), 26);
// truncate to 10 bytes
assert!(t.set_len(10).is_ok());
// position should not have moved
assert_eq!(t.stream_position().unwrap(), 26); // tell()
assert_eq!(t.read_to_end(&mut buf).unwrap(), 0);
assert_eq!(buf.as_slice(), b"");
assert_eq!(t.stream_position().unwrap(), 26); // tell()
buf.clear();
// read whole thing
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 10);
assert_eq!(buf.as_slice(), b"abcdefghij");
buf.clear();
// set_len to expand beyond the end
assert!(t.set_len(40).is_ok());
assert_eq!(t.stream_position().unwrap(), 10); // tell()
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 40);
assert_eq!(
buf.as_slice(),
&b"abcdefghij\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"[..]
);
}
#[test]
fn test_set_len_buffer() {
let mut t = spooled_tempfile(100);
test_set_len(&mut t);
}
#[test]
fn test_set_len_file() {
let mut t = spooled_tempfile(100);
test_set_len(&mut t);
}
#[test]
fn test_set_len_rollover() {
configure_wasi_temp_dir();
let mut buf: Vec<u8> = Vec::new();
let mut t = spooled_tempfile(10);
assert_eq!(t.write(b"abcde").unwrap(), 5);
assert!(!t.is_rolled());
assert_eq!(t.stream_position().unwrap(), 5); // tell()
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 5);
assert_eq!(buf.as_slice(), b"abcde");
assert_eq!(t.stream_position().unwrap(), 5); // tell()
buf.clear();
assert!(t.set_len(20).is_ok());
assert!(t.is_rolled());
assert_eq!(t.stream_position().unwrap(), 5); // tell()
assert_eq!(t.seek(SeekFrom::Start(0)).unwrap(), 0);
assert_eq!(t.read_to_end(&mut buf).unwrap(), 20);
assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
}
#[test]
fn test_write_overflow() {
configure_wasi_temp_dir();
let mut t = spooled_tempfile(10);
t.seek(SeekFrom::Start(u64::MAX)).unwrap();
assert!(t.write(b"abcde").is_err());
}
#[cfg(target_pointer_width = "32")]
#[test]
fn test_set_len_truncation() {
configure_wasi_temp_dir();
let mut t = spooled_tempfile(100);
assert!(t.set_len(usize::MAX as u64 + 5).is_ok());
assert!(t.is_rolled());
}

196
vendor/tempfile/tests/tempdir.rs vendored Normal file
View File

@@ -0,0 +1,196 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![deny(rust_2018_idioms)]
use std::fs;
use std::path::Path;
use tempfile::{Builder, TempDir};
/// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
/// directory instead as the base directory for temp files.
fn configure_wasi_temp_dir() {
if cfg!(target_os = "wasi") {
let _ = tempfile::env::override_temp_dir(std::path::Path::new("/tmp"));
}
}
#[test]
fn test_tempdir() {
configure_wasi_temp_dir();
let path = {
let p = Builder::new().prefix("foobar").tempdir().unwrap();
let p = p.path();
assert!(p.to_str().unwrap().contains("foobar"));
p.to_path_buf()
};
assert!(!path.exists());
}
#[test]
fn test_prefix() {
configure_wasi_temp_dir();
let tmpfile = TempDir::with_prefix("prefix").unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.starts_with("prefix"));
}
#[test]
fn test_suffix() {
configure_wasi_temp_dir();
let tmpfile = TempDir::with_suffix("suffix").unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.ends_with("suffix"));
}
#[test]
fn test_customnamed() {
configure_wasi_temp_dir();
let tmpfile = Builder::new()
.prefix("prefix")
.suffix("suffix")
.rand_bytes(12)
.tempdir()
.unwrap();
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
assert!(name.starts_with("prefix"));
assert!(name.ends_with("suffix"));
assert_eq!(name.len(), 24);
}
#[test]
#[cfg_attr(target_os = "wasi", ignore = "thread::spawn is not supported")]
fn test_rm_tempdir_threading() {
configure_wasi_temp_dir();
use std::sync::mpsc::channel;
use std::thread;
let (tx, rx) = channel();
let f = move || {
let tmp = TempDir::new().unwrap();
tx.send(tmp.path().to_path_buf()).unwrap();
panic!("panic to unwind past `tmp`");
};
let _ = thread::spawn(f).join();
let path = rx.recv().unwrap();
assert!(!path.exists());
let tmp = TempDir::new().unwrap();
let path = tmp.path().to_path_buf();
let f = move || {
let _tmp = tmp;
panic!("panic to unwind past `tmp`");
};
let _ = thread::spawn(f).join();
assert!(!path.exists());
let path;
{
let f = move || TempDir::new().unwrap();
let tmp = thread::spawn(f).join().unwrap();
path = tmp.path().to_path_buf();
assert!(path.exists());
}
assert!(!path.exists());
}
#[test]
fn test_tempdir_keep() {
configure_wasi_temp_dir();
let path = {
let tmp = TempDir::new().unwrap();
tmp.keep()
};
assert!(path.exists());
fs::remove_dir_all(&path).unwrap();
assert!(!path.exists());
}
#[test]
fn test_tmpdir_close() {
configure_wasi_temp_dir();
let tmp = TempDir::new().unwrap();
let path = tmp.path().to_path_buf();
assert!(path.exists());
tmp.close().unwrap();
assert!(!path.exists());
}
#[test]
#[cfg_attr(target_os = "wasi", ignore = "unwinding is not supported")]
fn dont_double_panic() {
configure_wasi_temp_dir();
use std::thread;
let r: Result<(), _> = thread::spawn(move || {
let tmpdir = TempDir::new().unwrap();
// Remove the temporary directory so that TempDir sees
// an error on drop
fs::remove_dir(tmpdir.path()).unwrap();
// Panic. If TempDir panics *again* due to the rmdir
// error then the process will abort.
panic!();
})
.join();
assert!(r.is_err());
}
#[test]
fn pass_as_asref_path() {
configure_wasi_temp_dir();
let tempdir = TempDir::new().unwrap();
takes_asref_path(&tempdir);
fn takes_asref_path<T: AsRef<Path>>(path: T) {
let path = path.as_ref();
assert!(path.exists());
}
}
#[test]
fn test_disable_cleanup() {
configure_wasi_temp_dir();
// Case 0: never mark as "disable cleanup"
// Case 1: enable "disable cleanup" in the builder, don't touch it after.
// Case 2: enable "disable cleanup" in the builder, turn it off after.
// Case 3: don't enable disable cleanup in the builder, turn it on after.
for case in 0..4 {
let in_builder = case & 1 > 0;
let toggle = case & 2 > 0;
let mut tmpdir = Builder::new()
.disable_cleanup(in_builder)
.tempdir()
.unwrap();
if toggle {
tmpdir.disable_cleanup(!in_builder);
}
let path = tmpdir.path().to_owned();
drop(tmpdir);
if in_builder ^ toggle {
assert!(path.exists());
fs::remove_dir(path).unwrap();
} else {
assert!(!path.exists(), "tempdir wasn't deleted");
}
}
}

78
vendor/tempfile/tests/tempfile.rs vendored Normal file
View File

@@ -0,0 +1,78 @@
#![deny(rust_2018_idioms)]
use std::fs;
use std::io::{Read, Seek, SeekFrom, Write};
#[cfg(target_os = "linux")]
use std::{
sync::mpsc::{sync_channel, TryRecvError},
thread,
};
#[test]
fn test_basic() {
// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
// directory instead as the base directory for temp files.
#[cfg(target_os = "wasi")]
let _ = tempfile::env::override_temp_dir(std::path::Path::new("/tmp"));
let mut tmpfile = tempfile::tempfile().unwrap();
write!(tmpfile, "abcde").unwrap();
tmpfile.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
tmpfile.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
#[test]
fn test_cleanup() {
// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
// directory instead as the base directory for temp files.
#[cfg(target_os = "wasi")]
let _ = tempfile::env::override_temp_dir(std::path::Path::new("/tmp"));
let tmpdir = tempfile::tempdir().unwrap();
{
let mut tmpfile = tempfile::tempfile_in(&tmpdir).unwrap();
write!(tmpfile, "abcde").unwrap();
}
let num_files = fs::read_dir(&tmpdir).unwrap().count();
assert!(num_files == 0);
}
// Only run this test on Linux. MacOS doesn't like us creating so many files, apparently.
#[cfg(target_os = "linux")]
#[test]
fn test_pathological_cleaner() {
let tmpdir = tempfile::tempdir().unwrap();
let (tx, rx) = sync_channel(0);
let cleaner_thread = thread::spawn(move || {
let tmp_path = rx.recv().unwrap();
while rx.try_recv() == Err(TryRecvError::Empty) {
let files = fs::read_dir(&tmp_path).unwrap();
for f in files {
// skip errors
if f.is_err() {
continue;
}
let f = f.unwrap();
let _ = fs::remove_file(f.path());
}
}
});
// block until cleaner_thread makes progress
tx.send(tmpdir.path().to_owned()).unwrap();
// need 40-400 iterations to encounter race with cleaner on original system
for _ in 0..10000 {
let mut tmpfile = tempfile::tempfile_in(&tmpdir).unwrap();
write!(tmpfile, "abcde").unwrap();
tmpfile.seek(SeekFrom::Start(0)).unwrap();
let mut buf = String::new();
tmpfile.read_to_string(&mut buf).unwrap();
assert_eq!("abcde", buf);
}
// close the channel to make cleaner_thread exit
drop(tx);
cleaner_thread.join().expect("The cleaner thread failed");
}