Debian Codegraph @

third_party/rust/tokio/tests/fs_open_options.rs

raw
1 #![warn(rust_2018_idioms)]
2 #![cfg(all(feature = "full", not(target_os = "wasi")))] // WASI does not support all fs operations
3
4 use std::io::Write;
5 use tempfile::NamedTempFile;
6 use tokio::fs::OpenOptions;
7 use tokio::io::AsyncReadExt;
8
9 const HELLO: &[u8] = b"hello world...";
10
11 #[tokio::test]
12 async fn open_with_open_options_and_read() {
13 let mut tempfile = NamedTempFile::new().unwrap();
14 tempfile.write_all(HELLO).unwrap();
15
16 let mut file = OpenOptions::new().read(true).open(tempfile).await.unwrap();
17
18 let mut buf = [0; 1024];
19 let n = file.read(&mut buf).await.unwrap();
20
21 assert_eq!(n, HELLO.len());
22 assert_eq!(&buf[..n], HELLO);
23 }
24
25 #[tokio::test]
26 async fn open_options_write() {
27 // TESTING HACK: use Debug output to check the stored data
28 assert!(format!("{:?}", OpenOptions::new().write(true)).contains("write: true"));
29 }
30
31 #[tokio::test]
32 async fn open_options_append() {
33 // TESTING HACK: use Debug output to check the stored data
34 assert!(format!("{:?}", OpenOptions::new().append(true)).contains("append: true"));
35 }
36
37 #[tokio::test]
38 async fn open_options_truncate() {
39 // TESTING HACK: use Debug output to check the stored data
40 assert!(format!("{:?}", OpenOptions::new().truncate(true)).contains("truncate: true"));
41 }
42
43 #[tokio::test]
44 async fn open_options_create() {
45 // TESTING HACK: use Debug output to check the stored data
46 assert!(format!("{:?}", OpenOptions::new().create(true)).contains("create: true"));
47 }
48
49 #[tokio::test]
50 async fn open_options_create_new() {
51 // TESTING HACK: use Debug output to check the stored data
52 assert!(format!("{:?}", OpenOptions::new().create_new(true)).contains("create_new: true"));
53 }
54
55 #[tokio::test]
56 #[cfg(unix)]
57 async fn open_options_mode() {
58 let mode = format!("{:?}", OpenOptions::new().mode(0o644));
59 // TESTING HACK: use Debug output to check the stored data
60 assert!(
61 mode.contains("mode: 420 ") || mode.contains("mode: 0o000644 "),
62 "mode is: {}",
63 mode
64 );
65 }
66
67 #[tokio::test]
68 #[cfg(target_os = "linux")]
69 async fn open_options_custom_flags_linux() {
70 // TESTING HACK: use Debug output to check the stored data
71 assert!(
72 format!("{:?}", OpenOptions::new().custom_flags(libc::O_TRUNC))
73 .contains("custom_flags: 512,")
74 );
75 }
76
77 #[tokio::test]
78 #[cfg(any(target_os = "freebsd", target_os = "macos"))]
79 async fn open_options_custom_flags_bsd_family() {
80 // TESTING HACK: use Debug output to check the stored data
81 assert!(
82 format!("{:?}", OpenOptions::new().custom_flags(libc::O_NOFOLLOW))
83 .contains("custom_flags: 256,")
84 );
85 }