Skip to content

Commit

Permalink
Add commands.rs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luladjiev committed Jan 13, 2024
1 parent d8232fb commit baea37f
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
157 changes: 157 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::process;

use mockall::mock;

pub mod branch;
pub mod delete_branches;
pub mod fixup;
Expand Down Expand Up @@ -90,3 +92,158 @@ fn search_branch<T: Exec>(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<String, ()>;
}
}

#[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());
}
}
4 changes: 2 additions & 2 deletions src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::commands::{refresh_base, Exec};

pub fn run<T: Exec>(
command: &T,
name: String,
base: String,
name: &str,
base: &str,
verbose: bool,
) -> Result<(), &'static str> {
let result = refresh_base(command, &base, verbose);
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit baea37f

Please sign in to comment.