-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: add more tests for filesystem functionality (#5493)
- Loading branch information
Chris Brody
authored
Feb 26, 2023
1 parent
ca9f7ee
commit cadcd5d
Showing
13 changed files
with
462 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(tokio_wasi)))] // WASI does not support all fs operations | ||
|
||
use tokio::fs; | ||
|
||
#[tokio::test] | ||
#[cfg(unix)] | ||
async fn canonicalize_root_dir_unix() { | ||
assert_eq!(fs::canonicalize("/.").await.unwrap().to_str().unwrap(), "/"); | ||
} | ||
|
||
#[tokio::test] | ||
#[cfg(windows)] | ||
async fn canonicalize_root_dir_windows() { | ||
// 2-step let bindings due to Rust memory semantics | ||
let dir_path = fs::canonicalize("C:\\.\\").await.unwrap(); | ||
|
||
let dir_name = dir_path.to_str().unwrap(); | ||
|
||
assert!(dir_name.starts_with("\\\\")); | ||
assert!(dir_name.ends_with("C:\\")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "full", not(tokio_wasi)))] // WASI does not support all fs operations | ||
|
||
use std::io::Write; | ||
use tempfile::NamedTempFile; | ||
use tokio::fs::OpenOptions; | ||
use tokio::io::AsyncReadExt; | ||
|
||
const HELLO: &[u8] = b"hello world..."; | ||
|
||
#[tokio::test] | ||
async fn open_with_open_options_and_read() { | ||
let mut tempfile = NamedTempFile::new().unwrap(); | ||
tempfile.write_all(HELLO).unwrap(); | ||
|
||
let mut file = OpenOptions::new().read(true).open(tempfile).await.unwrap(); | ||
|
||
let mut buf = [0; 1024]; | ||
let n = file.read(&mut buf).await.unwrap(); | ||
|
||
assert_eq!(n, HELLO.len()); | ||
assert_eq!(&buf[..n], HELLO); | ||
} | ||
|
||
#[tokio::test] | ||
async fn open_options_write() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!(format!("{:?}", OpenOptions::new().write(true)).contains("write: true")); | ||
} | ||
|
||
#[tokio::test] | ||
async fn open_options_append() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!(format!("{:?}", OpenOptions::new().append(true)).contains("append: true")); | ||
} | ||
|
||
#[tokio::test] | ||
async fn open_options_truncate() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!(format!("{:?}", OpenOptions::new().truncate(true)).contains("truncate: true")); | ||
} | ||
|
||
#[tokio::test] | ||
async fn open_options_create() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!(format!("{:?}", OpenOptions::new().create(true)).contains("create: true")); | ||
} | ||
|
||
#[tokio::test] | ||
async fn open_options_create_new() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!(format!("{:?}", OpenOptions::new().create_new(true)).contains("create_new: true")); | ||
} | ||
|
||
#[tokio::test] | ||
#[cfg(unix)] | ||
async fn open_options_mode() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!(format!("{:?}", OpenOptions::new().mode(0o644)).contains("mode: 420 ")); | ||
} | ||
|
||
#[tokio::test] | ||
#[cfg(target_os = "linux")] | ||
async fn open_options_custom_flags_linux() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!( | ||
format!("{:?}", OpenOptions::new().custom_flags(libc::O_TRUNC)) | ||
.contains("custom_flags: 512,") | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
#[cfg(any(target_os = "freebsd", target_os = "macos"))] | ||
async fn open_options_custom_flags_bsd_family() { | ||
// TESTING HACK: use Debug output to check the stored data | ||
assert!( | ||
format!("{:?}", OpenOptions::new().custom_flags(libc::O_NOFOLLOW)) | ||
.contains("custom_flags: 256,") | ||
); | ||
} |
Oops, something went wrong.