1 #![warn(rust_2018_idioms)] 2 #![cfg(all(feature = "full", not(target_os = "wasi")))] // WASI does not support all fs operations 3 4 use tempfile::tempdir; 5 use tokio::fs; 6 7 #[tokio::test] 8 #[cfg_attr(miri, ignore)] // No `chmod` in miri. try_exists()9async fn try_exists() { 10 let dir = tempdir().unwrap(); 11 12 let existing_path = dir.path().join("foo.txt"); 13 fs::write(&existing_path, b"Hello File!").await.unwrap(); 14 let nonexisting_path = dir.path().join("bar.txt"); 15 16 assert!(fs::try_exists(existing_path).await.unwrap()); 17 assert!(!fs::try_exists(nonexisting_path).await.unwrap()); 18 // FreeBSD root user always has permission to stat. 19 #[cfg(all(unix, not(target_os = "freebsd")))] 20 { 21 use std::os::unix::prelude::PermissionsExt; 22 let permission_denied_directory_path = dir.path().join("baz"); 23 fs::create_dir(&permission_denied_directory_path) 24 .await 25 .unwrap(); 26 let permission_denied_file_path = permission_denied_directory_path.join("baz.txt"); 27 fs::write(&permission_denied_file_path, b"Hello File!") 28 .await 29 .unwrap(); 30 let mut perms = tokio::fs::metadata(&permission_denied_directory_path) 31 .await 32 .unwrap() 33 .permissions(); 34 35 perms.set_mode(0o244); 36 fs::set_permissions(&permission_denied_directory_path, perms) 37 .await 38 .unwrap(); 39 let permission_denied_result = fs::try_exists(permission_denied_file_path).await; 40 assert_eq!( 41 permission_denied_result.err().unwrap().kind(), 42 std::io::ErrorKind::PermissionDenied 43 ); 44 } 45 } 46