diff --git a/src/commands.rs b/src/commands.rs index 13db0b5..692be47 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,5 +1,7 @@ use std::process; +use mockall::mock; + pub mod branch; pub mod delete_branches; pub mod fixup; @@ -90,3 +92,158 @@ fn search_branch(command: &T, branch: &str, verbose: bool) -> Result<() Err(()) => Err("Failed to list branch"), } } + +mock! { + pub Cmd {} + + impl Exec for Cmd { + fn exec<'a>(&self, args: &[&'a str], verbose: bool) -> Result; + } +} + +#[cfg(test)] +mod tests { + use crate::commands::MockCmd; + + fn cmd_branch_not_found() -> MockCmd { + let mut command = MockCmd::new(); + command + .expect_exec() + .withf(|args, verbose| args == ["branch", "-l", "main"] && !(*verbose)) + .times(1) + .returning(|_, _| Ok(String::new())); + + command + } + + fn cmd_branch_main_found() -> MockCmd { + let mut command = MockCmd::new(); + command + .expect_exec() + .withf(|args, verbose| args == ["branch", "-l", "main"] && !(*verbose)) + .times(1) + .returning(|_, _| Ok("* main".to_string())); + + command + } + + fn cmd_default_branch_master_found() -> MockCmd { + let mut command = cmd_branch_not_found(); + command + .expect_exec() + .withf(|args, verbose| args == ["branch", "-l", "master"] && !(*verbose)) + .times(1) + .returning(|_, _| Ok("* master".to_string())); + + command + } + + fn cmd_checkout_main() -> MockCmd { + let mut command = MockCmd::new(); + command + .expect_exec() + .withf(|args, verbose| args == ["checkout", "main"] && !(*verbose)) + .times(1) + .returning(|_, _| Ok(String::new())); + + command + } + + #[test] + fn test_get_default_branch_main() { + let command = cmd_branch_main_found(); + let branch = super::get_default_branch(&command, false); + + assert_eq!(branch, Ok("main")); + } + + #[test] + fn test_get_default_branch_master() { + let command = cmd_default_branch_master_found(); + let branch = super::get_default_branch(&command, false); + + assert_eq!(branch, Ok("master")); + } + + #[test] + fn test_get_base_default_to_main() { + let command = cmd_branch_main_found(); + let base = super::get_base(&command, None, false); + + assert_eq!(base, "main"); + } + + #[test] + fn test_get_base_default_to_master() { + let command = cmd_default_branch_master_found(); + let base = super::get_base(&command, None, false); + + assert_eq!(base, "master"); + } + + #[test] + fn test_get_base_supplied_base() { + let command = MockCmd::new(); + let base = super::get_base(&command, Some("test".to_string()), false); + + assert_eq!(base, "test"); + } + + #[test] + fn test_refresh_base_success() { + let mut command = cmd_checkout_main(); + command + .expect_exec() + .withf(|args, verbose| args == ["pull"] && !(*verbose)) + .times(1) + .returning(|_, _| Ok(String::new())); + + let result = super::refresh_base(&command, "main", false); + + assert!(result.is_ok()); + } + + #[test] + fn test_refresh_base_checkout_failure() { + let mut command = MockCmd::new(); + command + .expect_exec() + .withf(|args, verbose| args == ["checkout", "main"] && !(*verbose)) + .times(1) + .returning(|_, _| Err(())); + + let result = super::refresh_base(&command, "main", false); + + assert!(result.is_err()); + } + + #[test] + fn test_refresh_base_pull_failure() { + let mut command = cmd_checkout_main(); + command + .expect_exec() + .withf(|args, verbose| args == ["pull"] && !(*verbose)) + .times(1) + .returning(|_, _| Err(())); + + let result = super::refresh_base(&command, "main", false); + + assert!(result.is_err()); + } + + #[test] + fn test_search_branch_found() { + let command = cmd_branch_main_found(); + let result = super::search_branch(&command, "main", false); + + assert!(result.is_ok()); + } + + #[test] + fn test_search_branch_not_found() { + let command = cmd_branch_not_found(); + let result = super::search_branch(&command, "main", false); + + assert!(result.is_err()); + } +} diff --git a/src/commands/branch.rs b/src/commands/branch.rs index 2445c74..48181a4 100644 --- a/src/commands/branch.rs +++ b/src/commands/branch.rs @@ -2,8 +2,8 @@ use crate::commands::{refresh_base, Exec}; pub fn run( command: &T, - name: String, - base: String, + name: &str, + base: &str, verbose: bool, ) -> Result<(), &'static str> { let result = refresh_base(command, &base, verbose); diff --git a/src/main.rs b/src/main.rs index 30a0cca..62d44e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ fn main() { Some(Commands::Branch { name, base }) => { let base = get_base(&command, base, cli.verbose); - branch::run(&command, name, base, cli.verbose) + branch::run(&command, &name, &base, cli.verbose) } Some(Commands::Rebase { base }) => { let base = get_base(&command, base, cli.verbose);