aboutsummaryrefslogtreecommitdiff
path: root/hooked
diff options
context:
space:
mode:
authorMichael Gattozzi <mgattozzi@gmail.com>2019-11-25 16:04:45 -0500
committerMichael Gattozzi <mgattozzi@gmail.com>2019-11-25 16:04:45 -0500
commit3cd4bd77799e172293cc377e7bc45ab18be7cba8 (patch)
treee48e793307fb6e5b147171f2941fbe0a8d21349c /hooked
parent7f3d02a01bc5174cd9e2a4b1c44be64534cb1a37 (diff)
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.
Diffstat (limited to 'hooked')
-rw-r--r--hooked/Cargo.toml5
-rw-r--r--hooked/tests/init.rs61
2 files changed, 66 insertions, 0 deletions
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<dyn Error>> {
+ 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(())
+}