Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set env var with git params when available #123

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/git_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
#[cfg(test)]
use crate::rusty_hook::rusty_hook_tests::utils::build_simple_command_runner;
use crate::rusty_hook::rusty_hook_tests::utils::{build_simple_command_runner, GIT_REV_PARSE_CMD};

#[cfg(test)]
mod get_root_directory_path_tests {
Expand All @@ -14,7 +14,7 @@ mod get_root_directory_path_tests {
dir: Option<&str>,
stream_io: bool,
_env: Option<&HashMap<String, String>>| {
if cmd == "git rev-parse --show-toplevel" && dir == Some(target_dir) && !stream_io {
if cmd == GIT_REV_PARSE_CMD && dir == Some(target_dir) && !stream_io {
Ok(Some(String::from(exp)))
} else {
Ok(None)
Expand Down
47 changes: 28 additions & 19 deletions src/rusty_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,31 @@ where
};

let config_file_contents =
match config::get_config_file_contents(read_file, file_exists, &root_directory_path) {
Ok(contents) => contents,
Err(err) => {
if err == config::NO_CONFIG_FILE_FOUND {
return Err(Some(String::from(config::NO_CONFIG_FILE_FOUND)));
config::get_config_file_contents(read_file, file_exists, &root_directory_path).map_err(
|e| {
if e == config::NO_CONFIG_FILE_FOUND {
Some(e)
} else {
return Err(Some(String::from("Failed to parse config file")));
Some(String::from("Failed to parse config file"))
}
}
};
},
)?;

let log_details = config::get_log_setting(&config_file_contents);
let mut script = match config::get_hook_script(&config_file_contents, &hook_name) {
Ok(script) => script,
Err(err) => {
let (script, env_vars) = match (
config::get_hook_script(&config_file_contents, &hook_name),
args,
) {
(Ok(script), None) => (script, None),
(Ok(script), Some(a)) => (
script.replace("%rh!", &a),
Some(
vec![("RUSTY_HOOK_GIT_PARAMS".to_owned(), a)]
.into_iter()
.collect::<HashMap<String, String>>(),
),
),
(Err(err), _) => {
if err == config::MISSING_CONFIG_KEY {
return Ok(());
}
Expand All @@ -112,14 +122,13 @@ where
);
log(&message, log_details);

if let Some(args) = args {
script = script.replace("%rh!", &args)
}

match run_command(&script, Some(&root_directory_path), log_details, None) {
Err(e) => Err(e),
Ok(_) => Ok(()),
}
run_command(
&script,
Some(&root_directory_path),
log_details,
env_vars.as_ref(),
)
.map(|_| ())
}

#[cfg(test)]
Expand Down
151 changes: 79 additions & 72 deletions src/rusty_hook_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::collections::HashMap;
pub(crate) mod utils {
use std::collections::HashMap;

pub(crate) const GIT_REV_PARSE_CMD: &str = "git rev-parse --show-toplevel";

#[allow(clippy::type_complexity)]
pub(crate) fn build_simple_command_runner(
outcome: Result<Option<String>, Option<String>>,
Expand Down Expand Up @@ -106,14 +108,7 @@ mod run_tests {
let read_file = |_file_path: &str| panic!("");
let file_exists = |_path: &str| panic!("");
let log = |_path: &str, _should_log: bool| panic!("");
let result = run(
run_command,
file_exists,
read_file,
log,
"",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "", None);
assert_eq!(result, Err(Some(String::from(exp_err))));
}

Expand All @@ -124,14 +119,7 @@ mod run_tests {
let read_file = |_file_path: &str| Err(());
let file_exists = |_path: &str| Ok(false);
let log = |_path: &str, _should_log: bool| panic!("");
let result = run(
run_command,
file_exists,
read_file,
log,
"",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "", None);
assert_eq!(result, Err(Some(String::from(exp_err))));
}

Expand All @@ -142,14 +130,7 @@ mod run_tests {
let read_file = |_file_path: &str| Err(());
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| panic!("");
let result = run(
run_command,
file_exists,
read_file,
log,
"",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "", None);
assert_eq!(result, Err(Some(String::from(exp_err))));
}

Expand All @@ -162,14 +143,7 @@ mod run_tests {
let read_file = |_file_path: &str| Ok(String::from(contents));
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| panic!("");
let result = run(
run_command,
file_exists,
read_file,
log,
"pre-push",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "pre-push", None);
assert_eq!(result, Ok(()));
}

Expand All @@ -181,14 +155,7 @@ mod run_tests {
let read_file = |_file_path: &str| Ok(String::from(contents));
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| panic!("");
let result = run(
run_command,
file_exists,
read_file,
log,
"pre-push",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "pre-push", None);
assert_eq!(result, Err(Some(String::from(exp_err))));
}

Expand Down Expand Up @@ -216,14 +183,7 @@ mod run_tests {
panic!("")
}
};
let result = run(
run_command,
file_exists,
read_file,
log,
"pre-commit",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "pre-commit", None);
assert_eq!(result, Ok(()));
}

Expand Down Expand Up @@ -251,14 +211,7 @@ mod run_tests {
panic!("")
}
};
let result = run(
run_command,
file_exists,
read_file,
log,
"pre-commit",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "pre-commit", None);
assert_eq!(result, Ok(()));
}

Expand All @@ -274,14 +227,7 @@ mod run_tests {
let read_file = |_file_path: &str| Ok(String::from(contents));
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| ();
let result = run(
run_command,
file_exists,
read_file,
log,
"pre-commit",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "pre-commit", None);
assert_eq!(result, Ok(()));
}

Expand All @@ -306,14 +252,75 @@ mod run_tests {
let read_file = |_file_path: &str| Ok(String::from(contents));
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| ();
let result = run(
run_command,
file_exists,
read_file,
log,
"pre-commit",
Some("".into()),
);
let result = run(run_command, file_exists, read_file, log, "pre-commit", None);
assert_eq!(result, Err(Some(String::from(exp_err))));
}

mod git_params {
use super::super::utils::GIT_REV_PARSE_CMD;
use super::*;

#[test]
fn handles_no_params_correctly() {
let contents = r#"[hooks]
pre-push = "echo %rh!"

[logging]
verbose = false
"#;
let run_command =
|c: &str, _: Option<&str>, _: bool, env: Option<&HashMap<String, String>>| {
assert!(env.is_none());
if c != GIT_REV_PARSE_CMD {
assert_eq!(c, "echo %rh!");
}

Ok(Some(String::new()))
};
let read_file = |_file_path: &str| Ok(String::from(contents));
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| ();
let result = run(run_command, file_exists, read_file, log, "pre-push", None);
assert!(result.is_ok());
}

#[test]
fn handles_params_correctly() {
let params = ".git/COMMIT_EDITMSG";
let hook_script = "echo %rh!";
let contents = format!(
r#"[hooks]
commit-msg = "{}"

[logging]
verbose = false
"#,
&hook_script
);
let run_command =
|c: &str, _: Option<&str>, _: bool, env: Option<&HashMap<String, String>>| {
if c != GIT_REV_PARSE_CMD {
assert!(env.is_some());
assert_eq!(c, &format!("echo {}", &params));
let env = env.unwrap();
assert_eq!(env.len(), 1);
assert_eq!(env.get("RUSTY_HOOK_GIT_PARAMS").unwrap(), &params);
}

Ok(Some(hook_script.to_owned()))
};
let read_file = |_file_path: &str| Ok(contents.to_owned());
let file_exists = |_path: &str| Ok(true);
let log = |_path: &str, _should_log: bool| ();
let result = run(
run_command,
file_exists,
read_file,
log,
"commit-msg",
Some(params.to_owned()),
);
assert!(result.is_ok());
}
}
}