From 54112bd8b0273948cfc4f8f718b2655096321be8 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Sun, 11 Jul 2021 08:34:18 +0100 Subject: Added some tests to the dotup crate. --- Cargo.lock | 90 ++++++++++++++++++++++++++++ dotup/Cargo.toml | 3 + dotup/tests/integration_tests.rs | 126 +++++++++++++++++++++++++++++++++++++++ dotup/tests/testing_depot.toml | 7 +++ 4 files changed, 226 insertions(+) create mode 100644 dotup/tests/integration_tests.rs create mode 100644 dotup/tests/testing_depot.toml diff --git a/Cargo.lock b/Cargo.lock index a981e03..a44b60b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,7 @@ dependencies = [ "log", "serde", "slotmap", + "tempfile", "thiserror", "toml", ] @@ -129,6 +130,17 @@ dependencies = [ "yansi", ] +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "glob" version = "0.3.0" @@ -221,6 +233,12 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -263,6 +281,55 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core", +] + +[[package]] +name = "redox_syscall" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.5.4" @@ -280,6 +347,15 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "serde" version = "1.0.126" @@ -326,6 +402,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if", + "libc", + "rand", + "redox_syscall", + "remove_dir_all", + "winapi", +] + [[package]] name = "termcolor" version = "1.1.2" diff --git a/dotup/Cargo.toml b/dotup/Cargo.toml index 2545e94..8acfe85 100644 --- a/dotup/Cargo.toml +++ b/dotup/Cargo.toml @@ -9,3 +9,6 @@ serde = { version = "*", features = ["derive"] } thiserror = "*" toml = "*" slotmap = "*" + +[dev-dependencies] +tempfile = "3.2" diff --git a/dotup/tests/integration_tests.rs b/dotup/tests/integration_tests.rs new file mode 100644 index 0000000..ea64136 --- /dev/null +++ b/dotup/tests/integration_tests.rs @@ -0,0 +1,126 @@ +use std::path::{Path, PathBuf}; + +use dotup::{ArchiveLink, Depot, DepotConfig, LinkCreateParams}; +use tempfile::TempDir; + +const TESTING_DEPOT_NAME: &str = "depot.toml"; +const TESTING_DEPOT_CONTENTS: &str = include_str!("testing_depot.toml"); + +fn create_empty_file(path: impl AsRef) { + let path = path.as_ref(); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).unwrap(); + } + std::fs::write(path, "").unwrap(); +} + +fn prepare_empty_temp_dir() -> TempDir { + TempDir::new().unwrap() +} + +fn prepare_temp_dir() -> TempDir { + let dir = TempDir::new().unwrap(); + std::fs::write(dir.path().join(TESTING_DEPOT_NAME), TESTING_DEPOT_CONTENTS).unwrap(); + create_empty_file(dir.path().join("o1/file1.txt")); + create_empty_file(dir.path().join("o2/file2.txt")); + dir +} + +fn read_depot(dir: &TempDir) -> Depot { + let archive_path = dir.path().join(TESTING_DEPOT_NAME); + Depot::new(DepotConfig { + archive: dotup::archive_read(&archive_path).unwrap(), + archive_path, + }) + .unwrap() +} + +#[test] +fn test_archive_deserialize() { + let archive = dotup::archive_deserialize(&TESTING_DEPOT_CONTENTS).unwrap(); + + let link1 = ArchiveLink { + origin: PathBuf::from("o1/file1.txt"), + destination: PathBuf::from("d1/file.txt"), + }; + let link2 = ArchiveLink { + origin: PathBuf::from("o2/file2.txt"), + destination: PathBuf::from("d2/d2/file.txt"), + }; + + assert_eq!(2, archive.links.len()); + assert!(archive.links.contains(&link1)); + assert!(archive.links.contains(&link2)); +} + +#[test] +fn test_archive_exists() { + let empty_dir = prepare_empty_temp_dir(); + let dir = prepare_temp_dir(); + + assert!(!dotup::archive_exists( + empty_dir.path().join(TESTING_DEPOT_NAME) + )); + assert!(dotup::archive_exists(dir.path().join(TESTING_DEPOT_NAME))); +} + +#[test] +fn test_depot_create() { + let empty_dir = prepare_empty_temp_dir(); + let dir = prepare_temp_dir(); + + let d1 = Depot::new(DepotConfig { + archive: Default::default(), + archive_path: empty_dir.path().join(TESTING_DEPOT_NAME), + }); + assert!(d1.is_err()); + + let archive_path = dir.path().join(TESTING_DEPOT_NAME); + let d2 = Depot::new(DepotConfig { + archive: dotup::archive_read(&archive_path).unwrap(), + archive_path, + }); + assert!(d2.is_ok()); +} + +#[test] +fn test_depot_create_link() { + let dir = prepare_temp_dir(); + let mut depot = read_depot(&dir); + + create_empty_file(dir.path().join("o3/file.txt")); + + let l1 = depot.create_link(LinkCreateParams { + origin: PathBuf::from("o3/file.txt"), + destination: PathBuf::from(".config/file.txt"), + }); + assert!(l1.is_ok()); + + let l2 = depot.create_link(LinkCreateParams { + origin: PathBuf::from("o4/file.txt"), + destination: PathBuf::from(".config/file.txt"), + }); + assert!(l2.is_err()); +} + +#[test] +fn test_depot_install_uninstall_link() { + let dir = prepare_temp_dir(); + let depot = read_depot(&dir); + let install_base = dir.path(); + + for link in depot.links() { + depot.install_link(link, install_base).unwrap(); + } + + for link in depot.links() { + let link_path = link.install_destination(install_base).unwrap(); + let link_target = std::fs::read_link(&link_path).unwrap(); + assert_eq!(link_target.canonicalize().unwrap(), link.origin_canonical()); + } + + for link in depot.links() { + depot.uninstall_link(link, install_base).unwrap(); + assert!(!link.install_destination(install_base).unwrap().exists()); + } +} diff --git a/dotup/tests/testing_depot.toml b/dotup/tests/testing_depot.toml new file mode 100644 index 0000000..ee5224a --- /dev/null +++ b/dotup/tests/testing_depot.toml @@ -0,0 +1,7 @@ +[[links]] +origin = "o1/file1.txt" +destination = "d1/file.txt" + +[[links]] +origin = "o2/file2.txt" +destination = "d2/d2/file.txt" -- cgit