use std::{fs, path::Path};
use binsync::{CachingChunkProvider, Syncer};
extern crate binsync;
mod common;
#[test]
fn test_empty_destination() {
let context = common::TestContext::new();
context.write_file("in/test.bin", 1048576); context.write_file("in/test2.bin", 1048576);
let from = context.path("in");
let to = context.path("out");
let manifest = binsync::generate_manifest(&from).unwrap();
let from_path = Path::new(&from);
let provider = CachingChunkProvider::new(from_path);
let to_path = Path::new(&to);
let mut syncer = Syncer::new(to_path, provider, manifest);
syncer.sync().unwrap();
assert!(context.compare_hashes("in/test.bin", "out/test.bin"));
assert!(context.compare_hashes("in/test2.bin", "out/test2.bin"));
}
#[test]
fn test_empty_destination_complex() {
let context = common::TestContext::new();
context.write_file("in/foo/bar/baz/test.bin", 1048576);
binsync::sync(&context.path("in"), &context.path("out")).unwrap();
assert!(context.compare_hashes("in/foo/bar/baz/test.bin", "out/foo/bar/baz/test.bin"));
}
#[test]
fn test_rand_destination() {
let context = common::TestContext::new();
context.write_file("in/test.bin", 1048576); context.write_file("out/test.bin", 1048677);
binsync::sync(&context.path("in"), &context.path("out")).unwrap();
assert!(context.compare_hashes("in/test.bin", "out/test.bin"));
}
#[test]
fn test_copy_destination() {
let context = common::TestContext::new();
context.write_file("in/test.bin", 1048576); fs::copy(context.path("in/test.bin"), context.path("out/test.bin")).unwrap();
let from = context.path("in");
let to = context.path("out");
let manifest = binsync::generate_manifest(&from).unwrap();
let from_path = Path::new(&from);
let provider = CachingChunkProvider::new(from_path);
let to_path = Path::new(&to);
let mut syncer = Syncer::new(to_path, provider, manifest);
let plan = syncer.plan().unwrap();
assert_eq!(0, plan.operations.len());
syncer.sync_from_plan(&plan).unwrap();
assert!(context.compare_hashes("in/test.bin", "out/test.bin"));
}
#[test]
fn test_copy_destination_padded() {
let context = common::TestContext::new();
context.write_file("in/test.bin", 1048576); fs::copy(context.path("in/test.bin"), context.path("out/test.bin")).unwrap();
let mut data = fs::read(context.path("in/test.bin")).unwrap();
let mut copy = data.clone();
let mut copy2 = copy.split_off(copy.len() / 2);
copy2.append(&mut data);
copy2.append(&mut copy);
fs::write(context.path("in/test.bin"), copy2).unwrap();
binsync::sync(&context.path("in"), &context.path("out")).unwrap();
assert!(context.compare_hashes("in/test.bin", "out/test.bin"));
}