summaryrefslogtreecommitdiffstats
path: root/vendor/tokio/tests/fs_file.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/tokio/tests/fs_file.rs
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tokio/tests/fs_file.rs')
-rw-r--r--vendor/tokio/tests/fs_file.rs126
1 files changed, 119 insertions, 7 deletions
diff --git a/vendor/tokio/tests/fs_file.rs b/vendor/tokio/tests/fs_file.rs
index bf2f1d7b5..94d872a57 100644
--- a/vendor/tokio/tests/fs_file.rs
+++ b/vendor/tokio/tests/fs_file.rs
@@ -1,12 +1,11 @@
#![warn(rust_2018_idioms)]
-#![cfg(feature = "full")]
-
-use tokio::fs::File;
-use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
-use tokio_test::task;
+#![cfg(all(feature = "full", not(tokio_wasi)))] // WASI does not support all fs operations
use std::io::prelude::*;
use tempfile::NamedTempFile;
+use tokio::fs::File;
+use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom};
+use tokio_test::task;
const HELLO: &[u8] = b"hello world...";
@@ -51,6 +50,19 @@ async fn basic_write_and_shutdown() {
}
#[tokio::test]
+async fn rewind_seek_position() {
+ let tempfile = tempfile();
+
+ let mut file = File::create(tempfile.path()).await.unwrap();
+
+ file.seek(SeekFrom::Current(10)).await.unwrap();
+
+ file.rewind().await.unwrap();
+
+ assert_eq!(file.stream_position().await.unwrap(), 0);
+}
+
+#[tokio::test]
async fn coop() {
let mut tempfile = tempfile();
tempfile.write_all(HELLO).unwrap();
@@ -61,7 +73,7 @@ async fn coop() {
let mut buf = [0; 1024];
loop {
- file.read(&mut buf).await.unwrap();
+ let _ = file.read(&mut buf).await.unwrap();
file.seek(std::io::SeekFrom::Start(0)).await.unwrap();
}
});
@@ -75,13 +87,93 @@ async fn coop() {
panic!("did not yield");
}
+#[tokio::test]
+async fn write_to_clone() {
+ let tempfile = tempfile();
+
+ let file = File::create(tempfile.path()).await.unwrap();
+ let mut clone = file.try_clone().await.unwrap();
+
+ clone.write_all(HELLO).await.unwrap();
+ clone.flush().await.unwrap();
+
+ let contents = std::fs::read(tempfile.path()).unwrap();
+ assert_eq!(contents, HELLO);
+}
+
+#[tokio::test]
+async fn write_into_std() {
+ let tempfile = tempfile();
+
+ let file = File::create(tempfile.path()).await.unwrap();
+ let mut std_file = file.into_std().await;
+
+ std_file.write_all(HELLO).unwrap();
+
+ let contents = std::fs::read(tempfile.path()).unwrap();
+ assert_eq!(contents, HELLO);
+}
+
+#[tokio::test]
+async fn write_into_std_immediate() {
+ let tempfile = tempfile();
+
+ let file = File::create(tempfile.path()).await.unwrap();
+ let mut std_file = file.try_into_std().unwrap();
+
+ std_file.write_all(HELLO).unwrap();
+
+ let contents = std::fs::read(tempfile.path()).unwrap();
+ assert_eq!(contents, HELLO);
+}
+
+#[tokio::test]
+async fn read_file_from_std() {
+ let mut tempfile = tempfile();
+ tempfile.write_all(HELLO).unwrap();
+
+ let std_file = std::fs::File::open(tempfile.path()).unwrap();
+ let mut file = File::from(std_file);
+
+ let mut buf = [0; 1024];
+ let n = file.read(&mut buf).await.unwrap();
+ assert_eq!(n, HELLO.len());
+ assert_eq!(&buf[..n], HELLO);
+}
+
fn tempfile() -> NamedTempFile {
NamedTempFile::new().unwrap()
}
#[tokio::test]
#[cfg(unix)]
-async fn unix_fd() {
+async fn file_debug_fmt() {
+ let tempfile = tempfile();
+
+ let file = File::open(tempfile.path()).await.unwrap();
+
+ assert_eq!(
+ &format!("{:?}", file)[0..33],
+ "tokio::fs::File { std: File { fd:"
+ );
+}
+
+#[tokio::test]
+#[cfg(windows)]
+async fn file_debug_fmt() {
+ let tempfile = tempfile();
+
+ let file = File::open(tempfile.path()).await.unwrap();
+
+ assert_eq!(
+ &format!("{:?}", file)[0..37],
+ "tokio::fs::File { std: File { handle:"
+ );
+}
+
+#[tokio::test]
+#[cfg(unix)]
+async fn unix_fd_is_valid() {
use std::os::unix::io::AsRawFd;
let tempfile = tempfile();
@@ -90,6 +182,26 @@ async fn unix_fd() {
}
#[tokio::test]
+#[cfg(unix)]
+async fn read_file_from_unix_fd() {
+ use std::os::unix::io::{FromRawFd, IntoRawFd};
+
+ let mut tempfile = tempfile();
+ tempfile.write_all(HELLO).unwrap();
+
+ let file1 = File::open(tempfile.path()).await.unwrap();
+ let raw_fd = file1.into_std().await.into_raw_fd();
+ assert!(raw_fd > 0);
+
+ let mut file2 = unsafe { File::from_raw_fd(raw_fd) };
+
+ let mut buf = [0; 1024];
+ let n = file2.read(&mut buf).await.unwrap();
+ assert_eq!(n, HELLO.len());
+ assert_eq!(&buf[..n], HELLO);
+}
+
+#[tokio::test]
#[cfg(windows)]
async fn windows_handle() {
use std::os::windows::io::AsRawHandle;