fuzzel-pass 0.1.2

A password-store frontend for auto-typing passwords
use super::Result;

shell!(Fuzzel, "/usr/bin/fuzzel");

impl Fuzzel {
    /// Prompts the end user with a graphical menu by calling `fuzzel`
    /// and returns the user's choice.
    ///
    /// The choice must be a multiline [`&str`], each line representing
    /// a value that the user can pick
    ///
    /// * `chouce` - The menu
    ///
    /// [`&str`]: std::str
    pub fn pick(&self, choice: &str) -> Result<String> {
        self.inner
            .exec_with_pipe(choice, vec!["-d".into()])
            .map(|output| output.trim().into())
    }
}

#[cfg(test)]
mod test {
    use crate::error::Error;

    use super::*;
    use indoc::indoc;

    #[test]
    fn test_default() {
        let fuzel = Fuzzel::default();

        assert_eq!(Fuzzel::new("/usr/bin/fuzzel".into()), fuzel);
    }

    #[test]
    fn test_pick() {
        let fuzzel = mock_fuzzzel();
        let stdin = indoc!(
            "Bank/bank.example.com
            Store/store.example.com
            "
        );
        let expected = "Store/store.example.com";

        let result = fuzzel.pick(stdin);

        assert!(result.is_ok());

        if let Ok(actual) = result {
            assert_eq!(expected, actual);
        }
    }

    #[test]
    fn test_pick_empty_input() {
        let fuzzel = mock_fuzzzel();
        let stdin = "";
        let expected = Error::ChildExitStatusFailed("test/scripts/mock-fuzzel".into(), 2);

        let result = fuzzel.pick(stdin);

        assert!(result.is_err());

        if let Err(actual) = result {
            assert_eq!(expected, actual);
        }
    }

    fn mock_fuzzzel() -> Fuzzel {
        Fuzzel::new("test/scripts/mock-fuzzel".into())
    }
}