From 3cd4bd77799e172293cc377e7bc45ab18be7cba8 Mon Sep 17 00:00:00 2001 From: Michael Gattozzi Date: Mon, 25 Nov 2019 16:04:45 -0500 Subject: Add 'hooked init' test Up to this point testing of our command line tools just hasn't been happening. That's not great. While locally testing things by hand is possible, overtime various workflows will be harder to test by hand. By automating these tests we can avoid regressions that we wouldn't think to catch. Future work will involve working on adding tests for tools as they integrate together. --- hooked/Cargo.toml | 5 +++++ hooked/tests/init.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 hooked/tests/init.rs (limited to 'hooked') diff --git a/hooked/Cargo.toml b/hooked/Cargo.toml index d0781e0..2a7d477 100644 --- a/hooked/Cargo.toml +++ b/hooked/Cargo.toml @@ -12,3 +12,8 @@ paw = "1.0" shared = { path = "../shared" } structopt = { version = "0.3", features = ["paw"] } unicode-segmentation = "1.3" + +[dev-dependencies] +tempfile = "3" +assert_cmd = "0.10" +git2 = "0.10" diff --git a/hooked/tests/init.rs b/hooked/tests/init.rs new file mode 100644 index 0000000..2596d5b --- /dev/null +++ b/hooked/tests/init.rs @@ -0,0 +1,61 @@ +use assert_cmd::prelude::*; +use git2::Repository; +use std::{ + env, + error::Error, + fs, + os::unix::fs::PermissionsExt, + process::Command, +}; +use tempfile::tempdir; + +const HOOKS: [&str; 18] = [ + "applypatch-msg", + "post-applypatch", + "pre-commit", + "prepare-commit-msg", + "commit-msg", + "post-commit", + "pre-rebase", + "post-checkout", + "post-merge", + "pre-push", + "pre-receive", + "update", + "post-receive", + "post-update", + "push-to-checkout", + "pre-auto-gc", + "post-rewrite", + "sendemail-validate", +]; + +#[test] +fn init() -> Result<(), Box> { + let dir = tempdir()?; + Repository::init(&dir)?; + let mut cmd = Command::cargo_bin("hooked")?; + env::set_current_dir(&dir)?; + cmd.arg("init").assert().success(); + let git = &dir.path().join(".git").join("hooks"); + let dev = &dir.path().join(".dev-suite").join("hooked"); + + for hook in &HOOKS { + let git_hook = git.join(hook); + let dev_hook = dev.join(hook); + assert!(&git_hook.exists()); + assert!(&dev_hook.exists()); + assert!(fs::symlink_metadata(&git_hook)?.file_type().is_symlink()); + assert!(dev_hook.is_file()); + // why are we doing a bitwise and? + // git does a thing where it'll tack on extra bits to make the mode bit + // 0o100755 which is for uncommitted files or something. Not great. 511 is + // just 9 1s in binary with the rest being zero. This lets us get 3 octal + // numbers essentially, allowing us to test the actual value we wanted to + // test, and making this test work without special casing it if git ever + // changes. + assert_eq!(dev_hook.metadata()?.permissions().mode() & 511, 0o755); + } + + Ok(()) +} -- cgit v1.2.3