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] try_exists()8async fn try_exists() { 9 let dir = tempdir().unwrap(); 10 11 let existing_path = dir.path().join("foo.txt"); 12 fs::write(&existing_path, b"Hello File!").await.unwrap(); 13 let nonexisting_path = dir.path().join("bar.txt"); 14 15 assert!(fs::try_exists(existing_path).await.unwrap()); 16 assert!(!fs::try_exists(nonexisting_path).await.unwrap()); 17 // FreeBSD root user always has permission to stat. 18 #[cfg(all(unix, not(target_os = "freebsd")))] 19 { 20 use std::os::unix::prelude::PermissionsExt; 21 let permission_denied_directory_path = dir.path().join("baz"); 22 fs::create_dir(&permission_denied_directory_path) 23 .await 24 .unwrap(); 25 let permission_denied_file_path = permission_denied_directory_path.join("baz.txt"); 26 fs::write(&permission_denied_file_path, b"Hello File!") 27 .await 28 .unwrap(); 29 let mut perms = tokio::fs::metadata(&permission_denied_directory_path) 30 .await 31 .unwrap() 32 .permissions(); 33 34 perms.set_mode(0o244); 35 fs::set_permissions(&permission_denied_directory_path, perms) 36 .await 37 .unwrap(); 38 let permission_denied_result = fs::try_exists(permission_denied_file_path).await; 39 assert_eq!( 40 permission_denied_result.err().unwrap().kind(), 41 std::io::ErrorKind::PermissionDenied 42 ); 43 } 44 } 45