Skip to content

Commit

Permalink
Refactor tests using fs
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Mar 29, 2021
1 parent 7af2f8f commit c7851c7
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 156 deletions.
164 changes: 64 additions & 100 deletions src/common/transfer/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,40 +128,21 @@ mod tests {
use assert_fs::{NamedTempFile, TempDir};
use fs_extra::error::ErrorKind;
use ntest::*;
use test_case::test_case;

mod file_type {
use super::*;

#[test]
fn file() {
let file = NamedTempFile::new("a").unwrap();
file.touch().unwrap();
assert_eq!(FileType::from(file.path()), FileType::File);
}

#[test]
fn dir() {
assert_eq!(
FileType::from(TempDir::new().unwrap().path()),
FileType::Dir
);
}

#[test]
fn unknown() {
assert_eq!(
FileType::from(NamedTempFile::new("a").unwrap().path()),
FileType::Unknown
);
}
#[test_case(temp_dir().path(), FileType::Dir ; "dir")]
#[test_case(touch(temp_file("a")).path(), FileType::File ; "file")]
#[test_case(temp_file("b").path(), FileType::Unknown ; "unknown")]
fn file_type(path: &Path, file_type: FileType) {
assert_eq!(FileType::from(path), file_type);
}

mod transfer_path {
use super::*;

#[test]
fn path_not_found() {
let src_file = NamedTempFile::new("a").unwrap();
let src_file = temp_file("a");

assert_eq!(
transfer_path(src_file.path(), &Path::new("b"), TransferMode::Move) // Mode is irrelevant
Expand All @@ -180,10 +161,8 @@ mod tests {

#[test]
fn overwrite_dir_with_file() {
let src_file = NamedTempFile::new("a").unwrap();
let dst_dir = TempDir::new().unwrap();

src_file.touch().unwrap();
let src_file = touch(temp_file("a"));
let dst_dir = temp_dir();

assert_eq!(
transfer_path(src_file.path(), dst_dir.path(), TransferMode::Move) // Mode is irrelevant
Expand All @@ -204,10 +183,8 @@ mod tests {

#[test]
fn overwrite_file_with_dir() {
let src_dir = TempDir::new().unwrap();
let dst_file = NamedTempFile::new("a").unwrap();

dst_file.touch().unwrap();
let src_dir = temp_dir();
let dst_file = touch(temp_file("a"));

assert_eq!(
transfer_path(src_dir.path(), dst_file.path(), TransferMode::Move) // Mode is irrelevant
Expand All @@ -228,10 +205,8 @@ mod tests {

#[test]
fn rename_file() {
let src_file = NamedTempFile::new("a").unwrap();
let dst_file = NamedTempFile::new("b").unwrap();

src_file.write_str("1").unwrap();
let src_file = write(temp_file("a"), "1");
let dst_file = temp_file("b");

assert_eq!(
transfer_path(src_file.path(), dst_file.path(), TransferMode::Move)
Expand All @@ -245,9 +220,7 @@ mod tests {

#[test]
fn rename_file_to_itself() {
let src_file = NamedTempFile::new("a").unwrap();

src_file.write_str("1").unwrap();
let src_file = write(temp_file("a"), "1");

assert_eq!(
transfer_path(src_file.path(), src_file.path(), TransferMode::Move)
Expand All @@ -260,11 +233,8 @@ mod tests {

#[test]
fn move_file_to_other() {
let src_file = NamedTempFile::new("a").unwrap();
let dst_file = NamedTempFile::new("b").unwrap();

src_file.write_str("1").unwrap();
dst_file.touch().unwrap();
let src_file = write(temp_file("a"), "1");
let dst_file = write(temp_file("b"), "2");

assert_eq!(
transfer_path(src_file.path(), dst_file.path(), TransferMode::Move)
Expand All @@ -278,10 +248,8 @@ mod tests {

#[test]
fn copy_file() {
let src_file = NamedTempFile::new("a").unwrap();
let dst_file = NamedTempFile::new("b").unwrap();

src_file.write_str("1").unwrap();
let src_file = write(temp_file("a"), "1");
let dst_file = temp_file("b");

assert_eq!(
transfer_path(src_file.path(), dst_file.path(), TransferMode::Copy)
Expand All @@ -296,9 +264,7 @@ mod tests {
#[test]
#[timeout(5000)] // fs_extra::file::copy freezes for same src/dst path
fn copy_file_to_itself() {
let src_file = NamedTempFile::new("a").unwrap();

src_file.write_str("1").unwrap();
let src_file = write(temp_file("a"), "1");

assert_eq!(
transfer_path(src_file.path(), src_file.path(), TransferMode::Copy)
Expand All @@ -311,11 +277,8 @@ mod tests {

#[test]
fn copy_file_to_other() {
let src_file = NamedTempFile::new("a").unwrap();
let dst_file = NamedTempFile::new("b").unwrap();

src_file.write_str("1").unwrap();
dst_file.write_str("2").unwrap();
let src_file = write(temp_file("a"), "1");
let dst_file = write(temp_file("b"), "2");

assert_eq!(
transfer_path(src_file.path(), dst_file.path(), TransferMode::Copy)
Expand All @@ -329,17 +292,14 @@ mod tests {

#[test]
fn rename_dir() {
let root_dir = TempDir::new().unwrap();
let root_dir = temp_dir();

let src_dir = root_dir.child("a");
let src_file = src_dir.child("c");
let src_dir = mkdir(root_dir.child("a"));
let src_file = write(src_dir.child("c"), "1");

let dst_dir = root_dir.child("b");
let dst_file = dst_dir.child("c");

src_dir.create_dir_all().unwrap();
src_file.write_str("1").unwrap();

assert_eq!(
transfer_path(src_dir.path(), dst_dir.path(), TransferMode::Move)
.map_err(unpack_fse_error),
Expand All @@ -355,10 +315,8 @@ mod tests {

#[test]
fn rename_dir_to_itself() {
let src_dir = TempDir::new().unwrap();
let src_file = src_dir.child("a");

src_file.write_str("1").unwrap();
let src_dir = temp_dir();
let src_file = write(src_dir.child("a"), "1");

assert_eq!(
transfer_path(src_dir.path(), src_dir.path(), TransferMode::Move)
Expand All @@ -372,19 +330,13 @@ mod tests {

#[test]
fn move_dir_to_other() {
let root_dir = TempDir::new().unwrap();

let src_dir = root_dir.child("a");
let src_file = src_dir.child("c");

let dst_dir = root_dir.child("b");
let dst_file = dst_dir.child("c");
let root_dir = temp_dir();

src_dir.create_dir_all().unwrap();
src_file.write_str("1").unwrap();
let src_dir = mkdir(root_dir.child("a"));
let src_file = write(src_dir.child("c"), "1");

dst_dir.create_dir_all().unwrap();
dst_file.write_str("2").unwrap();
let dst_dir = mkdir(root_dir.child("b"));
let dst_file = write(dst_dir.child("c"), "2");

assert_eq!(
transfer_path(src_dir.path(), dst_dir.path(), TransferMode::Move)
Expand All @@ -401,17 +353,14 @@ mod tests {

#[test]
fn copy_dir() {
let root_dir = TempDir::new().unwrap();
let root_dir = temp_dir();

let src_dir = root_dir.child("a");
let src_file = src_dir.child("c");
let src_dir = mkdir(root_dir.child("a"));
let src_file = write(src_dir.child("c"), "1");

let dst_dir = root_dir.child("b");
let dst_file = dst_dir.child("c");

src_dir.create_dir_all().unwrap();
src_file.write_str("1").unwrap();

assert_eq!(
transfer_path(src_dir.path(), dst_dir.path(), TransferMode::Copy)
.map_err(unpack_fse_error),
Expand All @@ -428,10 +377,8 @@ mod tests {
#[test]
#[timeout(5000)] // fs_extra::dir::copy freezes for same src/dst path
fn copy_dir_to_itself() {
let src_dir = TempDir::new().unwrap();
let src_file = src_dir.child("a");

src_file.write_str("1").unwrap();
let src_dir = temp_dir();
let src_file = write(src_dir.child("a"), "1");

assert_eq!(
transfer_path(src_dir.path(), src_dir.path(), TransferMode::Copy)
Expand All @@ -445,19 +392,13 @@ mod tests {

#[test]
fn copy_dir_to_other() {
let root_dir = TempDir::new().unwrap();

let src_dir = root_dir.child("a");
let src_file = src_dir.child("c");

let dst_dir = root_dir.child("b");
let dst_file = dst_dir.child("c");
let root_dir = temp_dir();

src_dir.create_dir_all().unwrap();
src_file.write_str("1").unwrap();
let src_dir = mkdir(root_dir.child("a"));
let src_file = write(src_dir.child("c"), "1");

dst_dir.create_dir_all().unwrap();
dst_file.write_str("2").unwrap();
let dst_dir = mkdir(root_dir.child("b"));
let dst_file = write(dst_dir.child("c"), "2");

assert_eq!(
transfer_path(src_dir.path(), dst_dir.path(), TransferMode::Copy)
Expand All @@ -479,4 +420,27 @@ mod tests {
assert_eq!(DIR_COPY_OPTIONS.skip_exist, FILE_COPY_OPTIONS.skip_exist);
assert_eq!(DIR_COPY_OPTIONS.buffer_size, FILE_COPY_OPTIONS.buffer_size);
}

fn temp_dir() -> TempDir {
TempDir::new().unwrap()
}

fn temp_file(name: &str) -> NamedTempFile {
NamedTempFile::new(name).unwrap()
}

fn mkdir<P: PathCreateDir>(path: P) -> P {
path.create_dir_all().unwrap();
path
}

fn touch<F: FileTouch>(file: F) -> F {
file.touch().unwrap();
file
}

fn write<F: FileWriteStr>(file: F, data: &str) -> F {
file.write_str(data).unwrap();
file
}
}
Loading

0 comments on commit c7851c7

Please sign in to comment.