Skip to content

Commit

Permalink
FEAT: Add rule: man no space (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizvbo authored Mar 25, 2024
1 parent 88b6b99 commit 7fd3f07
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/rules/man_no_space.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::Rule;
use crate::{cli::command::CrabCommand, shell::Shell};

pub fn match_rule(command: &mut CrabCommand, system_shell: Option<&dyn Shell>) -> bool {
if let Some(output) = &command.output {
command.script.starts_with("man") && output.contains("command not found")
} else {
false
}
}

pub fn get_new_command(command: &mut CrabCommand, system_shell: Option<&dyn Shell>) -> Vec<String> {
vec![format!("man {}", &command.script[3..])]
}

pub fn get_rule() -> Rule {
Rule::new(
"man_no_space".to_owned(),
None,
Some(2000),
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("mandiff", "mandiff: command not found", true)]
#[case("", "", 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("mandiff", "", vec!["man diff"])]
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, None), 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 long_form_help;
mod ls_all;
mod ls_lah;
mod man;
mod man_no_space;
mod mkdir_p;
mod no_command;
mod python_command;
Expand Down Expand Up @@ -157,6 +158,7 @@ pub fn get_rules() -> Vec<Rule> {
ls_all::get_rule(),
ls_lah::get_rule(),
man::get_rule(),
man_no_space::get_rule(),
mkdir_p::get_rule(),
no_command::get_rule(),
python_command::get_rule(),
Expand Down

0 comments on commit 7fd3f07

Please sign in to comment.