From 40bb48bb701f88c902b912fb51fea88b5ae9a8b8 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 16 Jun 2020 19:16:49 -0400 Subject: test for hashing bits --- src/db.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/db.rs b/src/db.rs index c0e4cda..eeaa27a 100644 --- a/src/db.rs +++ b/src/db.rs @@ -68,7 +68,12 @@ impl Posts { hasher.update(body); let hash = format!("{:x}", hasher.finalize()); + #[cfg(test)] + let homedir = std::env::var("PWD")?; + + #[cfg(not(test))] let homedir = std::env::var("HOME")?; + let writepath = format!("{}/.clinte.sha256", homedir); fs::write(writepath, &hash)?; @@ -163,4 +168,22 @@ mod tests { all.delete(1); all.write(); } + + #[test] + fn check_hash() { + let strposts = fs::read_to_string(PATH).unwrap(); + let mut hasher = Sha256::new(); + hasher.update(strposts); + let rhs = format!("{:x}", hasher.finalize()); + + let all = Posts::get_all(PATH); + all.hash().unwrap(); + + let pwd = std::env::var("PWD").unwrap(); + let hashpath = format!("{}/.clinte.sha256", pwd); + let lhs = fs::read_to_string(hashpath.clone()).unwrap(); + fs::remove_file(hashpath).unwrap(); + + assert_eq!(lhs, rhs); + } } -- cgit 1.4.1-2-gfad0