~savoy/mailcap

951391de82c0256e586eb94cf37ec7a052cea3e1 — savoy 2 years ago 96bff3b
duplicate mailcap entries are accounted for

The check for default locations has now reversed the path entries,
ordered from least to highest priority. In this case if the user as a
~/.mailcap file, mime types listed here will take precedence over those
in other / locations.

Signed-off-by: savoy <git@liberation.red>
1 files changed, 31 insertions(+), 11 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +31 -11
@@ 115,10 115,10 @@ impl Mailcap {
            let home = get_user_home();

            let mut default_locations: Vec<PathBuf> = vec![
                home.join(".mailcap"),
                PathBuf::from("/etc/mailcap"),
                PathBuf::from("/usr/etc/mailcap"),
                PathBuf::from("/usr/local/etc/mailcap"),
                PathBuf::from("/usr/etc/mailcap"),
                PathBuf::from("/etc/mailcap"),
                home.join(".mailcap"),
            ];

            mailcap_files.append(&mut default_locations)


@@ 217,11 217,10 @@ mod tests {
    use serial_test::serial;
    use std::io::Write;

    fn create_dummy_mailcap_line() -> (PathBuf, String) {
    fn create_dummy_mailcap_line(dummy_value: Vec<String>) -> (PathBuf, Vec<String>) {
        let path = PathBuf::from("/tmp/mailcap-rs.test");
        let mut dummy_file = File::create(&path).unwrap();
        let dummy_value = String::from("text/html; qutebrowser '%s'; test=test -n \"$DISPLAY\"; nametemplate=%s.html; needsterminal");
        write!(&mut dummy_file, "{}", dummy_value).unwrap();
        writeln!(&mut dummy_file, "{:?}", dummy_value).unwrap();

        (path, dummy_value)
    }


@@ 244,10 243,10 @@ mod tests {

        let home = get_user_home();
        let default_locations: Vec<PathBuf> = vec![
            home.join(".mailcap"),
            PathBuf::from("/etc/mailcap"),
            PathBuf::from("/usr/etc/mailcap"),
            PathBuf::from("/usr/local/etc/mailcap"),
            PathBuf::from("/usr/etc/mailcap"),
            PathBuf::from("/etc/mailcap"),
            home.join(".mailcap"),
        ];

        assert_eq!(default_locations, Mailcap::list_potential_files())


@@ 331,8 330,10 @@ mod tests {
    #[test]
    #[serial]
    fn create_mailcap_struct() {
        let (_path, dummy_line) = create_dummy_mailcap_line();
        let dummy_line_vectorized = Mailcap::parse_valid_lines(vec![dummy_line]).unwrap();
        let (_path, dummy_line) = create_dummy_mailcap_line(
            vec!["text/html; qutebrowser '%s'; test=test -n \"$DISPLAY\"; nametemplate=%s.html; needsterminal".to_string()]
        );
        let dummy_line_vectorized = Mailcap::parse_valid_lines(dummy_line).unwrap();
        let dummy_line = Entry::from(dummy_line_vectorized[0].to_owned());

        env::set_var("MAILCAPS", "/tmp/mailcap-rs.test");


@@ 341,4 342,23 @@ mod tests {
            assert_eq!(i.command, dummy_line.command)
        }
    }

    #[test]
    #[serial]
    fn mailcap_with_duplicates() {
        let (_path, dummy_line) = create_dummy_mailcap_line(
            vec![
                "text/html; qutebrowser '%s'; test=test -n \"$DISPLAY\"; nametemplate=%s.html; needsterminal".to_string(),
                "text/html; firefox '%s'; test=test -n \"$DISPLAY\"; nametemplate=%s.html".to_string()]
        );

        let dummy_line_vectorized = Mailcap::parse_valid_lines(dummy_line).unwrap();
        let dummy_line = Entry::from(dummy_line_vectorized[1].to_owned());

        env::set_var("MAILCAPS", "/tmp/mailcap-rs.test");
        let mailcap = Mailcap::new().unwrap();
        if let Some(i) = mailcap.data.get("text/html") {
            assert_eq!(i.command, dummy_line.command)
        }
    }
}