Skip to content

Commit

Permalink
chore: Add test for overwriting files
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Sep 8, 2024
1 parent 830f8fe commit 645889b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use std::fs::create_dir_all;
use std::fs::File;
use std::io::read_to_string;
#[cfg(unix)]
use std::os::unix::fs::{symlink, PermissionsExt};

Expand Down Expand Up @@ -75,6 +76,39 @@ fn copy_subdir() {
std::fs::remove_dir_all("source").unwrap();
}

#[test]
fn copy_overwrite() {
use std::fs::File;
use std::io::Write;

let source_dir = "overwrite_source";
let dest_dir = "overwrite_dest";

std::env::set_var("RUST_LOG", "debug");
let _ = env_logger::try_init();
create_dir_all(source_dir).unwrap();
create_dir_all(dest_dir).unwrap();
File::create(format!("{source_dir}/a.txt")).unwrap();
let mut file_b = File::create(format!("{source_dir}/b.txt")).unwrap();

let contents = "Contents changed";
// Copy once, both files are empty
CopyBuilder::new(source_dir, dest_dir).run().unwrap();
// write something to file b so we can check if we overwrite it
write!(file_b, "{contents}").unwrap();
// perform a second copy
CopyBuilder::new(source_dir, dest_dir)
.overwrite(true)
.run()
.unwrap();
// make sure the contents of b are now changed
let s = read_to_string(File::open(format!("{dest_dir}/b.txt")).unwrap()).unwrap();
assert!(s == contents, "Destination was not overwritten");

std::fs::remove_dir_all(source_dir).unwrap();
std::fs::remove_dir_all(dest_dir).unwrap();
}

#[test]
fn copy_exclude() {
std::env::set_var("RUST_LOG", "DEBUG");
Expand Down

0 comments on commit 645889b

Please sign in to comment.