extern crate futures;
extern crate tempdir;
extern crate tokio_fs;

use futures::Future;
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() {
    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())) });
}
