summaryrefslogtreecommitdiffstats
path: root/vendor/tokio/tests/fs_open_options.rs
blob: e8d8b51b39f109403f611097e372db02836bd701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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,")
    );
}