Skip to content

Commit

Permalink
Add delete_branches.rs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luladjiev committed Jan 13, 2024
1 parent 8f295aa commit b05a5e8
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn run<T: Exec>(
base: &str,
verbose: bool,
) -> Result<(), &'static str> {
let result = refresh_base(command, &base, verbose);
let result = refresh_base(command, base, verbose);

if let Err(()) = result {
return Err("Failed to refresh base branch");
Expand Down
134 changes: 117 additions & 17 deletions src/commands/delete_branches.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
use crate::commands::Exec;

pub fn run<T: Exec>(command: &T, dry_run: bool, verbose: bool) -> Result<(), &'static str> {
pub fn run<T: Exec>(command: &T, dry_run: bool, verbose: bool) -> Result<(), &str> {
match try_delete_branches(command, dry_run, verbose) {
Ok(output) => {
println!("{output}");
Ok(())
}
Err(err) => Err(err),
}
}

fn try_delete_branches<T: Exec>(
command: &T,
dry_run: bool,
verbose: bool,
) -> Result<String, &'static str> {
let result = command.exec(&["fetch", "--prune"], verbose);

if let Err(()) = result {
return Err("Failed to fetch");
}

let result = command.exec(&["branch", "-vv"], verbose);
let branches = command.exec(&["branch", "-vv"], verbose);

match result {
Ok(branches) => try_delete_branches(command, &branches, dry_run, verbose),
Err(()) => Err("Failed to get branches"),
}
}
let Ok(branches) = branches else {
return Err("Failed to get branches");
};

fn try_delete_branches<T: Exec>(
command: &T,
branches: &str,
dry_run: bool,
verbose: bool,
) -> Result<(), &'static str> {
let mut result = Vec::new();

for line in branches.lines() {
Expand All @@ -47,11 +53,105 @@ fn try_delete_branches<T: Exec>(
}
}

if result.is_empty() {
println!("No branches to delete");
let message = if result.is_empty() {
"No branches to delete".to_string()
} else {
println!("{}", result.join("\n"));
result.join("\n")
};

Ok(message)
}

#[cfg(test)]
mod tests {
use crate::commands::MockCmd;

use super::*;

fn cmd_fetch_prune() -> MockCmd {
let mut command = MockCmd::new();
command
.expect_exec()
.withf(|args, verbose| args == ["fetch", "--prune"] && !(*verbose))
.times(1)
.returning(|_, _| Ok(String::new()));

command
}

fn cmd_fetch_prune_branch() -> MockCmd {
let mut command = cmd_fetch_prune();
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-vv"] && !(*verbose))
.times(1)
.returning(|_, _| Ok(" branch1 [origin/branch1: gone]\n branch2 [origin/branch2: gone]\n* branch3 [origin/branch3]".to_string()));

command
}

#[test]
fn try_delete_branches_does_not_delete_when_dry_run() {
let command = cmd_fetch_prune_branch();

let result = try_delete_branches(&command, true, false);

assert!(result.is_ok());
assert_eq!(
result.unwrap(),
"Deleted branch branch1\nDeleted branch branch2"
);
}

Ok(())
#[test]
fn try_delete_branches_does_not_delete_current_branch() {
let mut command = cmd_fetch_prune_branch();
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-D", "branch1"] && !(*verbose))
.times(1)
.returning(|_, _| Ok(String::new()));
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-D", "branch2"] && !(*verbose))
.times(1)
.returning(|_, _| Ok(String::new()));

let result = try_delete_branches(&command, false, false);

assert!(result.is_ok());
assert_eq!(
result.unwrap(),
"Deleted branch branch1\nDeleted branch branch2"
);
}

#[test]
fn try_delete_branches_returns_error_when_delete_fails() {
let mut command = cmd_fetch_prune_branch();
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-D", "branch1"] && !(*verbose))
.times(1)
.returning(|_, _| Err(()));

let result = try_delete_branches(&command, false, false);

assert!(result.is_err());
}

#[test]
fn try_delete_branches_no_branches_to_delete() {
let mut command = cmd_fetch_prune();
command
.expect_exec()
.withf(|args, verbose| args == ["branch", "-vv"] && !(*verbose))
.times(1)
.returning(|_, _| Ok("* branch3 [origin/branch3]".to_string()));

let result = try_delete_branches(&command, false, false);

assert!(result.is_ok());
assert_eq!(result.unwrap(), "No branches to delete");
}
}

0 comments on commit b05a5e8

Please sign in to comment.