Files
tokio/tokio-fs/tests/link.rs
T

82 lines
1.8 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
2019-07-11 11:05:49 -05:00
#![feature(async_await)]
2018-11-16 14:50:06 -08:00
use std::fs;
use std::io::prelude::*;
use std::io::BufReader;
use tempfile::tempdir;
2018-11-16 14:50:06 -08:00
use tokio_fs::*;
mod pool;
#[test]
fn test_hard_link() {
let dir = tempdir().unwrap();
2018-11-16 14:50:06 -08:00
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();
}
2019-07-11 11:05:49 -05:00
let dst_2 = dst.clone();
pool::run(async move {
assert!(hard_link(src, dst_2.clone()).await.is_ok());
Ok(())
});
2018-11-16 14:50:06 -08:00
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().unwrap();
2018-11-16 14:50:06 -08:00
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();
}
2019-07-11 11:05:49 -05:00
let src_2 = src.clone();
let dst_2 = dst.clone();
pool::run(async move {
assert!(os::unix::symlink(src_2.clone(), dst_2.clone())
.await
.is_ok());
Ok(())
});
2018-11-16 14:50:06 -08:00
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");
2019-07-11 11:05:49 -05:00
pool::run(async move {
let read = read_link(dst.clone()).await.unwrap();
assert!(read == src);
let symlink_meta = symlink_metadata(dst.clone()).await.unwrap();
assert!(symlink_meta.file_type().is_symlink());
Ok(())
});
2018-11-16 14:50:06 -08:00
}