summaryrefslogtreecommitdiffstats
path: root/tokio-fs/tests/link.rs
blob: 33364d9851a08c1d3a5528daec70e98c285389fc (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
#![deny(warnings, rust_2018_idioms)]

use std::fs;
use std::io::prelude::*;
use std::io::BufReader;
use tempdir::TempDir;
use tokio_fs::*;

mod pool;

#[test]
fn test_hard_link() {
    let dir = TempDir::new("base").unwrap();
    let src = dir.path().join("src.txt");
    let dst = dir.path().join("dst.txt");

    {
        let mut file = fs::File::create(&src).unwrap();
        file.write_all(b"hello").unwrap();
    }

    pool::run({ hard_link(src, dst.clone()) });

    let mut content = String::new();

    {
        let file = fs::File::open(dst).unwrap();
        let mut reader = BufReader::new(file);
        reader.read_to_string(&mut content).unwrap();
    }

    assert!(content == "hello");
}

#[cfg(unix)]
#[test]
fn test_symlink() {
    use futures::Future;

    let dir = TempDir::new("base").unwrap();
    let src = dir.path().join("src.txt");
    let dst = dir.path().join("dst.txt");

    {
        let mut file = fs::File::create(&src).unwrap();
        file.write_all(b"hello").unwrap();
    }

    pool::run({ os::unix::symlink(src.clone(), dst.clone()) });

    let mut content = String::new();

    {
        let file = fs::File::open(dst.clone()).unwrap();
        let mut reader = BufReader::new(file);
        reader.read_to_string(&mut content).unwrap();
    }

    assert!(content == "hello");

    pool::run({ read_link(dst.clone()).map(move |x| assert!(x == src)) });
    pool::run({ symlink_metadata(dst.clone()).map(move |x| assert!(x.file_type().is_symlink())) });
}