Skip to content

Commit

Permalink
FEAT: Add rule: git two dashes (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizvbo authored Apr 29, 2024
1 parent f121ce0 commit 5e7f046
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/rules/git_two_dashes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::{
cli::command::CrabCommand,
rules::{
utils::git::{get_new_command_with_git_support, match_rule_with_git_support},
Rule,
},
shell::Shell,
utils::replace_argument,
};

fn auxiliary_match_rule(command: &CrabCommand) -> bool {
if let Some(stdout) = &command.output {
stdout.contains("error: did you mean `") && stdout.contains("` (with two dashes ?)")
} else {
false
}
}

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> {
if let Some(stdout) = &command.output {
if let Some(to) = &stdout.split('`').nth(1) {
return vec![replace_argument(&command.script, &to[1..], to)];
}
}
vec![]
}

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_two_dashes".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 crate::shell::Bash;
use rstest::rstest;

#[rstest]
#[case(
"git add -patch",
"error: did you mean `--patch` (with two dashes ?)",
true
)]
#[case(
"git checkout -patch",
"error: did you mean `--patch` (with two dashes ?)",
true
)]
#[case(
"git commit -amend",
"error: did you mean `--amend` (with two dashes ?)",
true
)]
#[case(
"git push -tags",
"error: did you mean `--tags` (with two dashes ?)",
true
)]
#[case(
"git rebase -continue",
"error: did you mean `--continue` (with two dashes ?)",
true
)]
#[case("git add --patch", "", false)]
#[case("git checkout --patch", "", false)]
#[case("git commit --amend", "", false)]
#[case("git push --tags", "", false)]
#[case("git rebase --continue", "", 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 add -patch", "error: did you mean `--patch` (with two dashes ?)", vec!["git add --patch"])]
#[case("git checkout -patch", "error: did you mean `--patch` (with two dashes ?)", vec!["git checkout --patch"])]
#[case("git commit -amend", "error: did you mean `--amend` (with two dashes ?)", vec!["git commit --amend"])]
#[case("git push -tags", "error: did you mean `--tags` (with two dashes ?)", vec!["git push --tags"])]
#[case("git rebase -continue", "error: did you mean `--continue` (with two dashes ?)", vec!["git rebase --continue"])]
fn test_get_new_command(
#[case] command: &str,
#[case] stdout: &str,
#[case] expected: Vec<&str>,
) {
let system_shell = Bash {};
let mut command = CrabCommand::new(command.to_owned(), Some(stdout.to_owned()), None);
assert_eq!(get_new_command(&mut command, Some(&system_shell)), expected);
}
}
2 changes: 2 additions & 0 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod git_rm_local_modifications;
mod git_rm_recursive;
mod git_stash;
mod git_stash_pop;
mod git_two_dashes;
mod go_run;
mod gradle_wrapper;
mod grep_arguments_order;
Expand Down Expand Up @@ -145,6 +146,7 @@ pub fn get_rules() -> Vec<Rule> {
chmod_x::get_rule(),
choco_install::get_rule(),
composer_not_command::get_rule(),
git_two_dashes::get_rule(),
conda_mistype::get_rule(),
cp_create_destination::get_rule(),
cp_omitting_directory::get_rule(),
Expand Down

0 comments on commit 5e7f046

Please sign in to comment.