diff options
author | Michael Gattozzi <mgattozzi@gmail.com> | 2020-01-01 22:54:34 -0500 |
---|---|---|
committer | Michael Gattozzi <mgattozzi@gmail.com> | 2020-01-01 22:54:34 -0500 |
commit | 86efa907288199acf7bb11ea3f426b18b230a4bd (patch) | |
tree | 6387d282c9a3c522d5c0d08ebf1d26bc5defc33b /hooked/tests | |
parent | ed345b6583684738975e3bf8a7e0c041df0123ab (diff) |
Make hooked and dev-suite git hooks cross platform
Up to this point hooked had been only designed to work on Windows, not
because dev-suite didn't want to support it, but because doing so was an
immense amount of work with no clear design due to how Unix and Window
paths are not at all the same. While shebang notation works on them for
both the paths are different.
In order to get around this we wrap Ruby, Python, and Bash scripts on
Windows with a different script that invokes the 'Git for Windows'
sh.exe to run the actual interpreters on the script. These can work fine
then as long as one has installed Git for Windows on their machine, and
has a copy of py.exe or ruby.exe on their path to be invoked.
There is one caveat. We have to assume that a user has installed their
copy of Git for Windows in the default location. This means if they
haven't the scripts will fail to run. There's not much we can do about
this and it's just a necessary wart to provide cross platform
capabilities for a project.
All projects can be initialized now with one of the language choices and
then have the proper files linked on their OS as part of the
initialization. Those who need to just link them in an already existing
project can just run `hooked link` in order to set their computer up.
This again handles the differences between the platforms. This project
is also updated to the new format of hooked so that collaboration is now
not limited to just Unix based OSes.
Diffstat (limited to 'hooked/tests')
-rw-r--r-- | hooked/tests/init.rs | 66 |
1 files changed, 57 insertions, 9 deletions
diff --git a/hooked/tests/init.rs b/hooked/tests/init.rs index 65c4aab..9c68ff3 100644 --- a/hooked/tests/init.rs +++ b/hooked/tests/init.rs @@ -1,10 +1,10 @@ use assert_cmd::prelude::*; use git2::Repository; +#[cfg(not(windows))] +use std::os::unix::fs::PermissionsExt; use std::{ - env, error::Error, fs, - os::unix::fs::PermissionsExt, process::Command, }; use tempfile::tempdir; @@ -30,19 +30,36 @@ const HOOKS: [&str; 18] = [ "sendemail-validate", ]; -#[test] -fn init() -> Result<(), Box<dyn Error>> { +fn lang(lang: &str) -> Result<(), Box<dyn Error>> { let dir = tempdir()?; let _ = Repository::init(&dir)?; - let mut cmd = Command::cargo_bin("hooked")?; - env::set_current_dir(&dir)?; - let _ = cmd.arg("init").assert().success(); + let _ = Command::cargo_bin("hooked")? + .arg("init") + .arg(lang) + .current_dir(&dir) + .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); + #[cfg(not(windows))] + let mut dev_hook = dev.join(hook); + #[cfg(windows)] + let mut dev_hook = dev.join("wrapper").join(hook); + + #[cfg(not(windows))] + let _ = match lang { + "bash" => dev_hook.set_extension("sh"), + "python" => dev_hook.set_extension("py"), + "ruby" => dev_hook.set_extension("rb"), + _ => unreachable!(), + }; + + #[cfg(windows)] + let _ = dev_hook.set_extension("sh"); + assert!(&git_hook.exists()); assert!(&dev_hook.exists()); assert!(fs::symlink_metadata(&git_hook)?.file_type().is_symlink()); @@ -54,8 +71,39 @@ fn init() -> Result<(), Box<dyn Error>> { // 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. + #[cfg(not(windows))] assert_eq!(dev_hook.metadata()?.permissions().mode() & 511, 0o755); - } + let shebang = fs::read_to_string(&dev_hook)? + .lines() + .nth(0) + .ok_or_else(|| "File is empty and has no shebang line")? + .to_owned(); + + #[cfg(not(windows))] + match lang { + "bash" => assert_eq!(shebang, "#!/usr/bin/env bash"), + "python" => assert_eq!(shebang, "#!/usr/bin/env python3"), + "ruby" => assert_eq!(shebang, "#!/usr/bin/env ruby"), + _ => unreachable!(), + } + #[cfg(windows)] + assert_eq!(shebang, "#!C:\\Program Files\\Git\\bin\\sh.exe") + } Ok(()) } + +#[test] +fn init_bash() -> Result<(), Box<dyn Error>> { + lang("bash") +} + +#[test] +fn init_python() -> Result<(), Box<dyn Error>> { + lang("python") +} + +#[test] +fn init_ruby() -> Result<(), Box<dyn Error>> { + lang("ruby") +} |