aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2021-07-11 08:34:18 +0100
committerdiogo464 <[email protected]>2021-07-11 08:37:27 +0100
commit54112bd8b0273948cfc4f8f718b2655096321be8 (patch)
tree757144ddb5a08251219a579bc9d0acfa2c8301bc
parent1eae3cdd93fec0907e9450b4aaa791c7c2d845a1 (diff)
Added some tests to the dotup crate.
-rw-r--r--Cargo.lock90
-rw-r--r--dotup/Cargo.toml3
-rw-r--r--dotup/tests/integration_tests.rs126
-rw-r--r--dotup/tests/testing_depot.toml7
4 files changed, 226 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a981e03..a44b60b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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]]
134name = "getrandom"
135version = "0.2.3"
136source = "registry+https://github.com/rust-lang/crates.io-index"
137checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
138dependencies = [
139 "cfg-if",
140 "libc",
141 "wasi",
142]
143
144[[package]]
133name = "glob" 145name = "glob"
134version = "0.3.0" 146version = "0.3.0"
135source = "registry+https://github.com/rust-lang/crates.io-index" 147source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -222,6 +234,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
222checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" 234checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
223 235
224[[package]] 236[[package]]
237name = "ppv-lite86"
238version = "0.2.10"
239source = "registry+https://github.com/rust-lang/crates.io-index"
240checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
241
242[[package]]
225name = "proc-macro-error" 243name = "proc-macro-error"
226version = "1.0.4" 244version = "1.0.4"
227source = "registry+https://github.com/rust-lang/crates.io-index" 245source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -264,6 +282,55 @@ dependencies = [
264] 282]
265 283
266[[package]] 284[[package]]
285name = "rand"
286version = "0.8.4"
287source = "registry+https://github.com/rust-lang/crates.io-index"
288checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
289dependencies = [
290 "libc",
291 "rand_chacha",
292 "rand_core",
293 "rand_hc",
294]
295
296[[package]]
297name = "rand_chacha"
298version = "0.3.1"
299source = "registry+https://github.com/rust-lang/crates.io-index"
300checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
301dependencies = [
302 "ppv-lite86",
303 "rand_core",
304]
305
306[[package]]
307name = "rand_core"
308version = "0.6.3"
309source = "registry+https://github.com/rust-lang/crates.io-index"
310checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
311dependencies = [
312 "getrandom",
313]
314
315[[package]]
316name = "rand_hc"
317version = "0.3.1"
318source = "registry+https://github.com/rust-lang/crates.io-index"
319checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
320dependencies = [
321 "rand_core",
322]
323
324[[package]]
325name = "redox_syscall"
326version = "0.2.9"
327source = "registry+https://github.com/rust-lang/crates.io-index"
328checksum = "5ab49abadf3f9e1c4bc499e8845e152ad87d2ad2d30371841171169e9d75feee"
329dependencies = [
330 "bitflags",
331]
332
333[[package]]
267name = "regex" 334name = "regex"
268version = "1.5.4" 335version = "1.5.4"
269source = "registry+https://github.com/rust-lang/crates.io-index" 336source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -281,6 +348,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
281checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 348checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
282 349
283[[package]] 350[[package]]
351name = "remove_dir_all"
352version = "0.5.3"
353source = "registry+https://github.com/rust-lang/crates.io-index"
354checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
355dependencies = [
356 "winapi",
357]
358
359[[package]]
284name = "serde" 360name = "serde"
285version = "1.0.126" 361version = "1.0.126"
286source = "registry+https://github.com/rust-lang/crates.io-index" 362source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -327,6 +403,20 @@ dependencies = [
327] 403]
328 404
329[[package]] 405[[package]]
406name = "tempfile"
407version = "3.2.0"
408source = "registry+https://github.com/rust-lang/crates.io-index"
409checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
410dependencies = [
411 "cfg-if",
412 "libc",
413 "rand",
414 "redox_syscall",
415 "remove_dir_all",
416 "winapi",
417]
418
419[[package]]
330name = "termcolor" 420name = "termcolor"
331version = "1.1.2" 421version = "1.1.2"
332source = "registry+https://github.com/rust-lang/crates.io-index" 422source = "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"] }
9thiserror = "*" 9thiserror = "*"
10toml = "*" 10toml = "*"
11slotmap = "*" 11slotmap = "*"
12
13[dev-dependencies]
14tempfile = "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 @@
1use std::path::{Path, PathBuf};
2
3use dotup::{ArchiveLink, Depot, DepotConfig, LinkCreateParams};
4use tempfile::TempDir;
5
6const TESTING_DEPOT_NAME: &str = "depot.toml";
7const TESTING_DEPOT_CONTENTS: &str = include_str!("testing_depot.toml");
8
9fn 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
17fn prepare_empty_temp_dir() -> TempDir {
18 TempDir::new().unwrap()
19}
20
21fn 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
29fn 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]
39fn 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]
57fn 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]
68fn 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]
87fn 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]
107fn 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]]
2origin = "o1/file1.txt"
3destination = "d1/file.txt"
4
5[[links]]
6origin = "o2/file2.txt"
7destination = "d2/d2/file.txt"