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: Add rule: git remote delete #155

Merged
merged 1 commit into from
Apr 22, 2024
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
73 changes: 73 additions & 0 deletions src/rules/git_remote_delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::{
cli::command::CrabCommand,
rules::{
utils::git::{get_new_command_with_git_support, match_rule_with_git_support},
Rule,
},
shell::Shell,
};
use regex::Regex;

fn auxiliary_match_rule(command: &CrabCommand) -> bool {
command.script.contains("remote delete")
}

pub fn match_rule(command: &mut CrabCommand, _system_shell: Option<&dyn Shell>) -> bool {
match_rule_with_git_support(auxiliary_match_rule, command)
}

fn auxiliary_get_new_command(
command: &CrabCommand,
system_shell: Option<&dyn Shell>,
) -> Vec<String> {
let re = Regex::new(r"delete").unwrap();
vec![re.replace(&command.script, "remove").to_string()]
}

pub fn get_new_command(
command: &mut CrabCommand,
_system_shell: Option<&dyn Shell>,
) -> Vec<String> {
get_new_command_with_git_support(auxiliary_get_new_command, command, _system_shell)
}

pub fn get_rule() -> Rule {
Rule::new(
"git_remote_delete".to_owned(),
None,
None,
None,
match_rule,
get_new_command,
None,
)
}

#[cfg(test)]
mod tests {
use super::{get_new_command, match_rule};
use crate::cli::command::CrabCommand;
use rstest::rstest;

#[rstest]
#[case("git remote delete foo", "", true)]
#[case("git remote remove foo", "", false)]
#[case("git remote add foo", "", false)]
#[case("git commit", "", false)]
fn test_match(#[case] command: &str, #[case] stdout: &str, #[case] is_match: bool) {
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(match_rule(&mut command, None), is_match);
}

#[rstest]
#[case("git remote delete foo", "", vec!["git remote remove foo"])]
#[case("git remote delete delete", "", vec!["git remote remove delete"])]
fn test_get_new_command(
#[case] command: &str,
#[case] stdout: &str,
#[case] expected: Vec<&str>,
) {
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(get_new_command(&mut command, None), expected);
}
}
6 changes: 4 additions & 2 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mod git_push_force;
mod git_push_without_commits;
mod git_rebase_merge_dir;
mod git_rebase_no_changes;
mod git_remote_delete;
mod go_run;
mod gradle_wrapper;
mod grep_arguments_order;
Expand Down Expand Up @@ -156,9 +157,7 @@ pub fn get_rules() -> Vec<Rule> {
git_checkout::get_rule(),
git_clone::get_rule(),
git_clone_missing::get_rule(),
git_push_without_commits::get_rule(),
git_commit_add::get_rule(),
git_rebase_no_changes::get_rule(),
git_commit_amend::get_rule(),
git_commit_reset::get_rule(),
git_diff_no_index::get_rule(),
Expand All @@ -174,7 +173,10 @@ pub fn get_rules() -> Vec<Rule> {
git_push::get_rule(),
git_push_different_branch_names::get_rule(),
git_push_force::get_rule(),
git_push_without_commits::get_rule(),
git_rebase_merge_dir::get_rule(),
git_rebase_no_changes::get_rule(),
git_remote_delete::get_rule(),
go_run::get_rule(),
gradle_wrapper::get_rule(),
grep_arguments_order::get_rule(),
Expand Down
Loading