diff options
| author | diogo464 <[email protected]> | 2021-07-11 08:34:18 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-07-11 08:37:27 +0100 |
| commit | 54112bd8b0273948cfc4f8f718b2655096321be8 (patch) | |
| tree | 757144ddb5a08251219a579bc9d0acfa2c8301bc | |
| parent | 1eae3cdd93fec0907e9450b4aaa791c7c2d845a1 (diff) | |
Added some tests to the dotup crate.
| -rw-r--r-- | Cargo.lock | 90 | ||||
| -rw-r--r-- | dotup/Cargo.toml | 3 | ||||
| -rw-r--r-- | dotup/tests/integration_tests.rs | 126 | ||||
| -rw-r--r-- | dotup/tests/testing_depot.toml | 7 |
4 files changed, 226 insertions, 0 deletions
| @@ -98,6 +98,7 @@ dependencies = [ | |||
| 98 | "log", | 98 | "log", |
| 99 | "serde", | 99 | "serde", |
| 100 | "slotmap", | 100 | "slotmap", |
| 101 | "tempfile", | ||
| 101 | "thiserror", | 102 | "thiserror", |
| 102 | "toml", | 103 | "toml", |
| 103 | ] | 104 | ] |
| @@ -130,6 +131,17 @@ dependencies = [ | |||
| 130 | ] | 131 | ] |
| 131 | 132 | ||
| 132 | [[package]] | 133 | [[package]] |
| 134 | name = "getrandom" | ||
| 135 | version = "0.2.3" | ||
| 136 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 137 | checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" | ||
| 138 | dependencies = [ | ||
| 139 | "cfg-if", | ||
| 140 | "libc", | ||
| 141 | "wasi", | ||
| 142 | ] | ||
| 143 | |||
| 144 | [[package]] | ||
| 133 | name = "glob" | 145 | name = "glob" |
| 134 | version = "0.3.0" | 146 | version = "0.3.0" |
| 135 | source = "registry+https://github.com/rust-lang/crates.io-index" | 147 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -222,6 +234,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 222 | checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" | 234 | checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" |
| 223 | 235 | ||
| 224 | [[package]] | 236 | [[package]] |
| 237 | name = "ppv-lite86" | ||
| 238 | version = "0.2.10" | ||
| 239 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 240 | checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" | ||
| 241 | |||
| 242 | [[package]] | ||
| 225 | name = "proc-macro-error" | 243 | name = "proc-macro-error" |
| 226 | version = "1.0.4" | 244 | version = "1.0.4" |
| 227 | source = "registry+https://github.com/rust-lang/crates.io-index" | 245 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -264,6 +282,55 @@ dependencies = [ | |||
| 264 | ] | 282 | ] |
| 265 | 283 | ||
| 266 | [[package]] | 284 | [[package]] |
| 285 | name = "rand" | ||
| 286 | version = "0.8.4" | ||
| 287 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 288 | checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" | ||
| 289 | dependencies = [ | ||
| 290 | "libc", | ||
| 291 | "rand_chacha", | ||
| 292 | "rand_core", | ||
| 293 | "rand_hc", | ||
| 294 | ] | ||
| 295 | |||
| 296 | [[package]] | ||
| 297 | name = "rand_chacha" | ||
| 298 | version = "0.3.1" | ||
| 299 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 300 | checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" | ||
| 301 | dependencies = [ | ||
| 302 | "ppv-lite86", | ||
| 303 | "rand_core", | ||
| 304 | ] | ||
| 305 | |||
| 306 | [[package]] | ||
| 307 | name = "rand_core" | ||
| 308 | version = "0.6.3" | ||
| 309 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 310 | checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" | ||
| 311 | dependencies = [ | ||
| 312 | "getrandom", | ||
| 313 | ] | ||
| 314 | |||
| 315 | [[package]] | ||
| 316 | name = "rand_hc" | ||
| 317 | version = "0.3.1" | ||
| 318 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 319 | checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" | ||
| 320 | dependencies = [ | ||
| 321 | "rand_core", | ||
| 322 | ] | ||
| 323 | |||
| 324 | [[package]] | ||
| 325 | name = "redox_syscall" | ||
| 326 | version = "0.2.9" | ||
| 327 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 328 | checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee" | ||
| 329 | dependencies = [ | ||
| 330 | "bitflags", | ||
| 331 | ] | ||
| 332 | |||
| 333 | [[package]] | ||
| 267 | name = "regex" | 334 | name = "regex" |
| 268 | version = "1.5.4" | 335 | version = "1.5.4" |
| 269 | source = "registry+https://github.com/rust-lang/crates.io-index" | 336 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -281,6 +348,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 281 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" | 348 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" |
| 282 | 349 | ||
| 283 | [[package]] | 350 | [[package]] |
| 351 | name = "remove_dir_all" | ||
| 352 | version = "0.5.3" | ||
| 353 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 354 | checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" | ||
| 355 | dependencies = [ | ||
| 356 | "winapi", | ||
| 357 | ] | ||
| 358 | |||
| 359 | [[package]] | ||
| 284 | name = "serde" | 360 | name = "serde" |
| 285 | version = "1.0.126" | 361 | version = "1.0.126" |
| 286 | source = "registry+https://github.com/rust-lang/crates.io-index" | 362 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -327,6 +403,20 @@ dependencies = [ | |||
| 327 | ] | 403 | ] |
| 328 | 404 | ||
| 329 | [[package]] | 405 | [[package]] |
| 406 | name = "tempfile" | ||
| 407 | version = "3.2.0" | ||
| 408 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 409 | checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" | ||
| 410 | dependencies = [ | ||
| 411 | "cfg-if", | ||
| 412 | "libc", | ||
| 413 | "rand", | ||
| 414 | "redox_syscall", | ||
| 415 | "remove_dir_all", | ||
| 416 | "winapi", | ||
| 417 | ] | ||
| 418 | |||
| 419 | [[package]] | ||
| 330 | name = "termcolor" | 420 | name = "termcolor" |
| 331 | version = "1.1.2" | 421 | version = "1.1.2" |
| 332 | source = "registry+https://github.com/rust-lang/crates.io-index" | 422 | source = "registry+https://github.com/rust-lang/crates.io-index" |
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"] } | |||
| 9 | thiserror = "*" | 9 | thiserror = "*" |
| 10 | toml = "*" | 10 | toml = "*" |
| 11 | slotmap = "*" | 11 | slotmap = "*" |
| 12 | |||
| 13 | [dev-dependencies] | ||
| 14 | 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 @@ | |||
| 1 | use std::path::{Path, PathBuf}; | ||
| 2 | |||
| 3 | use dotup::{ArchiveLink, Depot, DepotConfig, LinkCreateParams}; | ||
| 4 | use tempfile::TempDir; | ||
| 5 | |||
| 6 | const TESTING_DEPOT_NAME: &str = "depot.toml"; | ||
| 7 | const TESTING_DEPOT_CONTENTS: &str = include_str!("testing_depot.toml"); | ||
| 8 | |||
| 9 | fn create_empty_file(path: impl AsRef<Path>) { | ||
| 10 | let path = path.as_ref(); | ||
| 11 | if let Some(parent) = path.parent() { | ||
| 12 | std::fs::create_dir_all(parent).unwrap(); | ||
| 13 | } | ||
| 14 | std::fs::write(path, "").unwrap(); | ||
| 15 | } | ||
| 16 | |||
| 17 | fn prepare_empty_temp_dir() -> TempDir { | ||
| 18 | TempDir::new().unwrap() | ||
| 19 | } | ||
| 20 | |||
| 21 | fn prepare_temp_dir() -> TempDir { | ||
| 22 | let dir = TempDir::new().unwrap(); | ||
| 23 | std::fs::write(dir.path().join(TESTING_DEPOT_NAME), TESTING_DEPOT_CONTENTS).unwrap(); | ||
| 24 | create_empty_file(dir.path().join("o1/file1.txt")); | ||
| 25 | create_empty_file(dir.path().join("o2/file2.txt")); | ||
| 26 | dir | ||
| 27 | } | ||
| 28 | |||
| 29 | fn read_depot(dir: &TempDir) -> Depot { | ||
| 30 | let archive_path = dir.path().join(TESTING_DEPOT_NAME); | ||
| 31 | Depot::new(DepotConfig { | ||
| 32 | archive: dotup::archive_read(&archive_path).unwrap(), | ||
| 33 | archive_path, | ||
| 34 | }) | ||
| 35 | .unwrap() | ||
| 36 | } | ||
| 37 | |||
| 38 | #[test] | ||
| 39 | fn test_archive_deserialize() { | ||
| 40 | let archive = dotup::archive_deserialize(&TESTING_DEPOT_CONTENTS).unwrap(); | ||
| 41 | |||
| 42 | let link1 = ArchiveLink { | ||
| 43 | origin: PathBuf::from("o1/file1.txt"), | ||
| 44 | destination: PathBuf::from("d1/file.txt"), | ||
| 45 | }; | ||
| 46 | let link2 = ArchiveLink { | ||
| 47 | origin: PathBuf::from("o2/file2.txt"), | ||
| 48 | destination: PathBuf::from("d2/d2/file.txt"), | ||
| 49 | }; | ||
| 50 | |||
| 51 | assert_eq!(2, archive.links.len()); | ||
| 52 | assert!(archive.links.contains(&link1)); | ||
| 53 | assert!(archive.links.contains(&link2)); | ||
| 54 | } | ||
| 55 | |||
| 56 | #[test] | ||
| 57 | fn test_archive_exists() { | ||
| 58 | let empty_dir = prepare_empty_temp_dir(); | ||
| 59 | let dir = prepare_temp_dir(); | ||
| 60 | |||
| 61 | assert!(!dotup::archive_exists( | ||
| 62 | empty_dir.path().join(TESTING_DEPOT_NAME) | ||
| 63 | )); | ||
| 64 | assert!(dotup::archive_exists(dir.path().join(TESTING_DEPOT_NAME))); | ||
| 65 | } | ||
| 66 | |||
| 67 | #[test] | ||
| 68 | fn test_depot_create() { | ||
| 69 | let empty_dir = prepare_empty_temp_dir(); | ||
| 70 | let dir = prepare_temp_dir(); | ||
| 71 | |||
| 72 | let d1 = Depot::new(DepotConfig { | ||
| 73 | archive: Default::default(), | ||
| 74 | archive_path: empty_dir.path().join(TESTING_DEPOT_NAME), | ||
| 75 | }); | ||
| 76 | assert!(d1.is_err()); | ||
| 77 | |||
| 78 | let archive_path = dir.path().join(TESTING_DEPOT_NAME); | ||
| 79 | let d2 = Depot::new(DepotConfig { | ||
| 80 | archive: dotup::archive_read(&archive_path).unwrap(), | ||
| 81 | archive_path, | ||
| 82 | }); | ||
| 83 | assert!(d2.is_ok()); | ||
| 84 | } | ||
| 85 | |||
| 86 | #[test] | ||
| 87 | fn test_depot_create_link() { | ||
| 88 | let dir = prepare_temp_dir(); | ||
| 89 | let mut depot = read_depot(&dir); | ||
| 90 | |||
| 91 | create_empty_file(dir.path().join("o3/file.txt")); | ||
| 92 | |||
| 93 | let l1 = depot.create_link(LinkCreateParams { | ||
| 94 | origin: PathBuf::from("o3/file.txt"), | ||
| 95 | destination: PathBuf::from(".config/file.txt"), | ||
| 96 | }); | ||
| 97 | assert!(l1.is_ok()); | ||
| 98 | |||
| 99 | let l2 = depot.create_link(LinkCreateParams { | ||
| 100 | origin: PathBuf::from("o4/file.txt"), | ||
| 101 | destination: PathBuf::from(".config/file.txt"), | ||
| 102 | }); | ||
| 103 | assert!(l2.is_err()); | ||
| 104 | } | ||
| 105 | |||
| 106 | #[test] | ||
| 107 | fn test_depot_install_uninstall_link() { | ||
| 108 | let dir = prepare_temp_dir(); | ||
| 109 | let depot = read_depot(&dir); | ||
| 110 | let install_base = dir.path(); | ||
| 111 | |||
| 112 | for link in depot.links() { | ||
| 113 | depot.install_link(link, install_base).unwrap(); | ||
| 114 | } | ||
| 115 | |||
| 116 | for link in depot.links() { | ||
| 117 | let link_path = link.install_destination(install_base).unwrap(); | ||
| 118 | let link_target = std::fs::read_link(&link_path).unwrap(); | ||
| 119 | assert_eq!(link_target.canonicalize().unwrap(), link.origin_canonical()); | ||
| 120 | } | ||
| 121 | |||
| 122 | for link in depot.links() { | ||
| 123 | depot.uninstall_link(link, install_base).unwrap(); | ||
| 124 | assert!(!link.install_destination(install_base).unwrap().exists()); | ||
| 125 | } | ||
| 126 | } | ||
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 @@ | |||
| 1 | [[links]] | ||
| 2 | origin = "o1/file1.txt" | ||
| 3 | destination = "d1/file.txt" | ||
| 4 | |||
| 5 | [[links]] | ||
| 6 | origin = "o2/file2.txt" | ||
| 7 | destination = "d2/d2/file.txt" | ||
