diff options
| author | diogo464 <[email protected]> | 2021-07-11 10:07:33 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-07-11 10:07:33 +0100 |
| commit | 29b9f6ecd227e52f5973503dfcf4e4f228a43d80 (patch) | |
| tree | 2047469b2f00afea666a7a3d1e25ea3a8fbf0c10 | |
| parent | fcc5fbe564ee74e5f8434349424cbc4c646597df (diff) | |
Added some tests to dotup_cli crate.
| -rw-r--r-- | Cargo.lock | 102 | ||||
| -rw-r--r-- | dotup_cli/Cargo.toml | 5 | ||||
| -rw-r--r-- | dotup_cli/tests/cli.rs | 145 |
3 files changed, 252 insertions, 0 deletions
| @@ -18,6 +18,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 18 | checksum = "15af2628f6890fe2609a3b91bef4c83450512802e59489f9c1cb1fa5df064a61" | 18 | checksum = "15af2628f6890fe2609a3b91bef4c83450512802e59489f9c1cb1fa5df064a61" |
| 19 | 19 | ||
| 20 | [[package]] | 20 | [[package]] |
| 21 | name = "assert_cmd" | ||
| 22 | version = "1.0.7" | ||
| 23 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 24 | checksum = "3d20831bd004dda4c7c372c19cdabff369f794a95e955b3f13fe460e3e1ae95f" | ||
| 25 | dependencies = [ | ||
| 26 | "bstr", | ||
| 27 | "doc-comment", | ||
| 28 | "predicates", | ||
| 29 | "predicates-core", | ||
| 30 | "predicates-tree", | ||
| 31 | "wait-timeout", | ||
| 32 | ] | ||
| 33 | |||
| 34 | [[package]] | ||
| 21 | name = "atty" | 35 | name = "atty" |
| 22 | version = "0.2.14" | 36 | version = "0.2.14" |
| 23 | source = "registry+https://github.com/rust-lang/crates.io-index" | 37 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -41,6 +55,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 41 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" | 55 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" |
| 42 | 56 | ||
| 43 | [[package]] | 57 | [[package]] |
| 58 | name = "bstr" | ||
| 59 | version = "0.2.16" | ||
| 60 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 61 | checksum = "90682c8d613ad3373e66de8c6411e0ae2ab2571e879d2efbf73558cc66f21279" | ||
| 62 | dependencies = [ | ||
| 63 | "lazy_static", | ||
| 64 | "memchr", | ||
| 65 | "regex-automata", | ||
| 66 | ] | ||
| 67 | |||
| 68 | [[package]] | ||
| 44 | name = "cfg-if" | 69 | name = "cfg-if" |
| 45 | version = "1.0.0" | 70 | version = "1.0.0" |
| 46 | source = "registry+https://github.com/rust-lang/crates.io-index" | 71 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -92,6 +117,18 @@ dependencies = [ | |||
| 92 | ] | 117 | ] |
| 93 | 118 | ||
| 94 | [[package]] | 119 | [[package]] |
| 120 | name = "difflib" | ||
| 121 | version = "0.4.0" | ||
| 122 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 123 | checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" | ||
| 124 | |||
| 125 | [[package]] | ||
| 126 | name = "doc-comment" | ||
| 127 | version = "0.3.3" | ||
| 128 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 129 | checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" | ||
| 130 | |||
| 131 | [[package]] | ||
| 95 | name = "dotup" | 132 | name = "dotup" |
| 96 | version = "0.1.0" | 133 | version = "0.1.0" |
| 97 | dependencies = [ | 134 | dependencies = [ |
| @@ -108,13 +145,21 @@ name = "dotup_cli" | |||
| 108 | version = "0.1.0" | 145 | version = "0.1.0" |
| 109 | dependencies = [ | 146 | dependencies = [ |
| 110 | "anyhow", | 147 | "anyhow", |
| 148 | "assert_cmd", | ||
| 111 | "clap", | 149 | "clap", |
| 112 | "dotup", | 150 | "dotup", |
| 113 | "flexi_logger", | 151 | "flexi_logger", |
| 114 | "log", | 152 | "log", |
| 153 | "tempfile", | ||
| 115 | ] | 154 | ] |
| 116 | 155 | ||
| 117 | [[package]] | 156 | [[package]] |
| 157 | name = "either" | ||
| 158 | version = "1.6.1" | ||
| 159 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 160 | checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" | ||
| 161 | |||
| 162 | [[package]] | ||
| 118 | name = "flexi_logger" | 163 | name = "flexi_logger" |
| 119 | version = "0.18.0" | 164 | version = "0.18.0" |
| 120 | source = "registry+https://github.com/rust-lang/crates.io-index" | 165 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -182,6 +227,15 @@ dependencies = [ | |||
| 182 | ] | 227 | ] |
| 183 | 228 | ||
| 184 | [[package]] | 229 | [[package]] |
| 230 | name = "itertools" | ||
| 231 | version = "0.10.1" | ||
| 232 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 233 | checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf" | ||
| 234 | dependencies = [ | ||
| 235 | "either", | ||
| 236 | ] | ||
| 237 | |||
| 238 | [[package]] | ||
| 185 | name = "lazy_static" | 239 | name = "lazy_static" |
| 186 | version = "1.4.0" | 240 | version = "1.4.0" |
| 187 | source = "registry+https://github.com/rust-lang/crates.io-index" | 241 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -240,6 +294,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 240 | checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" | 294 | checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" |
| 241 | 295 | ||
| 242 | [[package]] | 296 | [[package]] |
| 297 | name = "predicates" | ||
| 298 | version = "2.0.0" | ||
| 299 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 300 | checksum = "c6e46ca79eb4e21e2ec14430340c71250ab69332abf85521c95d3a8bc336aa76" | ||
| 301 | dependencies = [ | ||
| 302 | "difflib", | ||
| 303 | "itertools", | ||
| 304 | "predicates-core", | ||
| 305 | ] | ||
| 306 | |||
| 307 | [[package]] | ||
| 308 | name = "predicates-core" | ||
| 309 | version = "1.0.2" | ||
| 310 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 311 | checksum = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451" | ||
| 312 | |||
| 313 | [[package]] | ||
| 314 | name = "predicates-tree" | ||
| 315 | version = "1.0.2" | ||
| 316 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 317 | checksum = "15f553275e5721409451eb85e15fd9a860a6e5ab4496eb215987502b5f5391f2" | ||
| 318 | dependencies = [ | ||
| 319 | "predicates-core", | ||
| 320 | "treeline", | ||
| 321 | ] | ||
| 322 | |||
| 323 | [[package]] | ||
| 243 | name = "proc-macro-error" | 324 | name = "proc-macro-error" |
| 244 | version = "1.0.4" | 325 | version = "1.0.4" |
| 245 | source = "registry+https://github.com/rust-lang/crates.io-index" | 326 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -342,6 +423,12 @@ dependencies = [ | |||
| 342 | ] | 423 | ] |
| 343 | 424 | ||
| 344 | [[package]] | 425 | [[package]] |
| 426 | name = "regex-automata" | ||
| 427 | version = "0.1.10" | ||
| 428 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 429 | checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" | ||
| 430 | |||
| 431 | [[package]] | ||
| 345 | name = "regex-syntax" | 432 | name = "regex-syntax" |
| 346 | version = "0.6.25" | 433 | version = "0.6.25" |
| 347 | source = "registry+https://github.com/rust-lang/crates.io-index" | 434 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -475,6 +562,12 @@ dependencies = [ | |||
| 475 | ] | 562 | ] |
| 476 | 563 | ||
| 477 | [[package]] | 564 | [[package]] |
| 565 | name = "treeline" | ||
| 566 | version = "0.1.0" | ||
| 567 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 568 | checksum = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41" | ||
| 569 | |||
| 570 | [[package]] | ||
| 478 | name = "unicode-segmentation" | 571 | name = "unicode-segmentation" |
| 479 | version = "1.8.0" | 572 | version = "1.8.0" |
| 480 | source = "registry+https://github.com/rust-lang/crates.io-index" | 573 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -505,6 +598,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 505 | checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" | 598 | checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" |
| 506 | 599 | ||
| 507 | [[package]] | 600 | [[package]] |
| 601 | name = "wait-timeout" | ||
| 602 | version = "0.2.0" | ||
| 603 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 604 | checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" | ||
| 605 | dependencies = [ | ||
| 606 | "libc", | ||
| 607 | ] | ||
| 608 | |||
| 609 | [[package]] | ||
| 508 | name = "wasi" | 610 | name = "wasi" |
| 509 | version = "0.10.0+wasi-snapshot-preview1" | 611 | version = "0.10.0+wasi-snapshot-preview1" |
| 510 | source = "registry+https://github.com/rust-lang/crates.io-index" | 612 | source = "registry+https://github.com/rust-lang/crates.io-index" |
diff --git a/dotup_cli/Cargo.toml b/dotup_cli/Cargo.toml index 891d32a..cf92f9e 100644 --- a/dotup_cli/Cargo.toml +++ b/dotup_cli/Cargo.toml | |||
| @@ -6,6 +6,7 @@ version = "0.1.0" | |||
| 6 | [[bin]] | 6 | [[bin]] |
| 7 | name = "dotup" | 7 | name = "dotup" |
| 8 | path = "src/main.rs" | 8 | path = "src/main.rs" |
| 9 | doc = false | ||
| 9 | 10 | ||
| 10 | [dependencies] | 11 | [dependencies] |
| 11 | anyhow = "*" | 12 | anyhow = "*" |
| @@ -18,3 +19,7 @@ path = "../dotup" | |||
| 18 | [dependencies.flexi_logger] | 19 | [dependencies.flexi_logger] |
| 19 | features = ["colors"] | 20 | features = ["colors"] |
| 20 | version = "*" | 21 | version = "*" |
| 22 | |||
| 23 | [dev-dependencies] | ||
| 24 | tempfile = "3.2" | ||
| 25 | assert_cmd = "1.0" | ||
diff --git a/dotup_cli/tests/cli.rs b/dotup_cli/tests/cli.rs new file mode 100644 index 0000000..3b8d657 --- /dev/null +++ b/dotup_cli/tests/cli.rs | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | use assert_cmd::{assert::Assert, prelude::*}; | ||
| 2 | use dotup::ArchiveLink; | ||
| 3 | use std::{ | ||
| 4 | path::{Path, PathBuf}, | ||
| 5 | process::Command, | ||
| 6 | }; | ||
| 7 | use tempfile::TempDir; | ||
| 8 | |||
| 9 | const DEPOT_FILE_NAME: &str = "depot.toml"; | ||
| 10 | const BIN_NAME: &str = "dotup"; | ||
| 11 | |||
| 12 | fn create_empty_file(path: impl AsRef<Path>) { | ||
| 13 | let path = path.as_ref(); | ||
| 14 | if let Some(parent) = path.parent() { | ||
| 15 | std::fs::create_dir_all(parent).unwrap(); | ||
| 16 | } | ||
| 17 | std::fs::write(path, "").unwrap(); | ||
| 18 | } | ||
| 19 | |||
| 20 | fn prepare_command(dir: &TempDir) -> Command { | ||
| 21 | let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); | ||
| 22 | cmd.current_dir(dir.path()); | ||
| 23 | cmd | ||
| 24 | } | ||
| 25 | |||
| 26 | fn run_command(dir: &TempDir, cmd: &str) -> Assert { | ||
| 27 | let mut c = prepare_command(dir); | ||
| 28 | c.current_dir(dir.path()); | ||
| 29 | c.args(cmd.split_whitespace()); | ||
| 30 | c.assert() | ||
| 31 | } | ||
| 32 | |||
| 33 | fn prepare_dir() -> TempDir { | ||
| 34 | let dir = TempDir::new().unwrap(); | ||
| 35 | create_empty_file(dir.path().join("o1/file.txt")); | ||
| 36 | create_empty_file(dir.path().join("o1/dir/file.txt")); | ||
| 37 | create_empty_file(dir.path().join("o2/file1.txt")); | ||
| 38 | create_empty_file(dir.path().join("o2/file2.txt")); | ||
| 39 | dir | ||
| 40 | } | ||
| 41 | |||
| 42 | #[test] | ||
| 43 | fn test_cli_init() { | ||
| 44 | let dir = prepare_dir(); | ||
| 45 | let assert = run_command(&dir, "init"); | ||
| 46 | |||
| 47 | assert.success().code(0); | ||
| 48 | assert!(dir.path().join(DEPOT_FILE_NAME).is_file()); | ||
| 49 | } | ||
| 50 | |||
| 51 | #[test] | ||
| 52 | fn test_cli_link() { | ||
| 53 | let dir = prepare_dir(); | ||
| 54 | run_command(&dir, "init").success(); | ||
| 55 | |||
| 56 | let assert = run_command(&dir, "link o1 .config"); | ||
| 57 | assert.success().code(0); | ||
| 58 | |||
| 59 | let assert = run_command(&dir, "link --directory o2 .scripts"); | ||
| 60 | assert.success().code(0); | ||
| 61 | |||
| 62 | let archive = dotup::archive_read(dir.path().join(DEPOT_FILE_NAME)).unwrap(); | ||
| 63 | let link1 = ArchiveLink { | ||
| 64 | origin: PathBuf::from("o1/file.txt"), | ||
| 65 | destination: PathBuf::from(".config/file.txt"), | ||
| 66 | }; | ||
| 67 | let link2 = ArchiveLink { | ||
| 68 | origin: PathBuf::from("o1/dir/file.txt"), | ||
| 69 | destination: PathBuf::from(".config/dir/file.txt"), | ||
| 70 | }; | ||
| 71 | let link3 = ArchiveLink { | ||
| 72 | origin: PathBuf::from("o2"), | ||
| 73 | destination: PathBuf::from(".scripts"), | ||
| 74 | }; | ||
| 75 | |||
| 76 | assert!(archive.links.contains(&link1)); | ||
| 77 | assert!(archive.links.contains(&link2)); | ||
| 78 | assert!(archive.links.contains(&link3)); | ||
| 79 | } | ||
| 80 | |||
| 81 | #[test] | ||
| 82 | fn test_cli_install_uninstall_unlink() { | ||
| 83 | let dir = prepare_dir(); | ||
| 84 | run_command(&dir, "init").success(); | ||
| 85 | run_command(&dir, "link o1 .config").success(); | ||
| 86 | run_command(&dir, "link --directory o2 .scripts").success(); | ||
| 87 | |||
| 88 | let install_dir = TempDir::new().unwrap(); | ||
| 89 | let install_base = format!("{}", install_dir.path().display()); | ||
| 90 | run_command( | ||
| 91 | &dir, | ||
| 92 | &format!("install --install-base {} o1 o2", install_base), | ||
| 93 | ) | ||
| 94 | .success(); | ||
| 95 | |||
| 96 | assert_eq!( | ||
| 97 | std::fs::read_link(install_dir.path().join(".config/file.txt")).unwrap(), | ||
| 98 | dir.path().join("o1/file.txt") | ||
| 99 | ); | ||
| 100 | assert_eq!( | ||
| 101 | std::fs::read_link(install_dir.path().join(".config/dir/file.txt")).unwrap(), | ||
| 102 | dir.path().join("o1/dir/file.txt") | ||
| 103 | ); | ||
| 104 | assert_eq!( | ||
| 105 | std::fs::read_link(install_dir.path().join(".scripts")).unwrap(), | ||
| 106 | dir.path().join("o2") | ||
| 107 | ); | ||
| 108 | |||
| 109 | run_command( | ||
| 110 | &dir, | ||
| 111 | &format!("uninstall --install-base {} o1/file.txt", install_base), | ||
| 112 | ) | ||
| 113 | .success(); | ||
| 114 | assert!(!install_dir.path().join(".config/file.txt").exists()); | ||
| 115 | assert!(install_dir.path().join(".config/dir/file.txt").exists()); | ||
| 116 | assert!(install_dir.path().join(".scripts").exists()); | ||
| 117 | |||
| 118 | run_command( | ||
| 119 | &dir, | ||
| 120 | &format!("uninstall --install-base {} o1", install_base), | ||
| 121 | ) | ||
| 122 | .success(); | ||
| 123 | assert!(!install_dir.path().join(".config/file.txt").exists()); | ||
| 124 | assert!(!install_dir.path().join(".config/dir/file.txt").exists()); | ||
| 125 | assert!(install_dir.path().join(".scripts").exists()); | ||
| 126 | |||
| 127 | assert_eq!( | ||
| 128 | 3, | ||
| 129 | dotup::archive_read(dir.path().join(DEPOT_FILE_NAME)) | ||
| 130 | .unwrap() | ||
| 131 | .links | ||
| 132 | .len() | ||
| 133 | ); | ||
| 134 | |||
| 135 | run_command(&dir, &format!("unlink --uninstall {} o2", install_base)).success(); | ||
| 136 | assert!(!install_dir.path().join(".scripts").exists()); | ||
| 137 | |||
| 138 | assert_eq!( | ||
| 139 | 2, | ||
| 140 | dotup::archive_read(dir.path().join(DEPOT_FILE_NAME)) | ||
| 141 | .unwrap() | ||
| 142 | .links | ||
| 143 | .len() | ||
| 144 | ); | ||
| 145 | } | ||
