pkgcraft/shell/commands/
hasq.rs

1use scallop::ExecStatus;
2
3use super::{TryParseArgs, make_builtin};
4
5#[derive(clap::Parser, Debug)]
6#[command(
7    name = "hasq",
8    disable_help_flag = true,
9    long_about = "Deprecated synonym for has."
10)]
11struct Command {
12    #[arg(long, action = clap::ArgAction::HelpLong)]
13    help: Option<bool>,
14
15    #[arg(allow_hyphen_values = true)]
16    needle: String,
17
18    #[arg(allow_hyphen_values = true)]
19    haystack: Vec<String>,
20}
21
22pub(crate) fn run(args: &[&str]) -> scallop::Result<ExecStatus> {
23    let cmd = Command::try_parse_args(args)?;
24    let found = cmd.haystack.contains(&cmd.needle);
25    Ok(ExecStatus::from(found))
26}
27
28make_builtin!("hasq", hasq_builtin);
29
30#[cfg(test)]
31mod tests {
32    use super::super::{assert_invalid_cmd, cmd_scope_tests, functions::hasq};
33    use super::*;
34
35    cmd_scope_tests!("hasq needle ${haystack}");
36
37    #[test]
38    fn invalid_args() {
39        assert_invalid_cmd(hasq, &[0]);
40    }
41
42    #[test]
43    fn contains() {
44        // no haystack
45        assert_eq!(hasq(&["1"]).unwrap(), ExecStatus::Failure(1));
46        // single element
47        assert_eq!(hasq(&["1", "1"]).unwrap(), ExecStatus::Success);
48        // multiple elements
49        assert_eq!(hasq(&["5", "1", "2", "3", "4", "5"]).unwrap(), ExecStatus::Success);
50        assert_eq!(hasq(&["6", "1", "2", "3", "4", "5"]).unwrap(), ExecStatus::Failure(1));
51        assert_eq!(hasq(&["-", "1", "2", "3", "4", "-"]).unwrap(), ExecStatus::Success);
52    }
53}