aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: a223b8cc97e372c963b793d7ad595c65c5dbfd4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use clap::Parser;

/// Minimal SafariBooks port (cookies only).
#[derive(Parser, Debug, PartialEq)]
#[command(version)]
pub struct Args {
    /// Book digits ID from the O'Reilly URL.
    pub bookid: String,

    /// Do not delete the log file on success.
    #[arg(long = "preserve-log")]
    pub preserve_log: bool,
}

#[cfg(test)]
mod tests {
    use super::Args;
    use clap::{CommandFactory, Parser};

    #[test]
    fn parses_positional_bookid_only() {
        // safaribooks-rs 9781491958698
        let args = Args::try_parse_from(["safaribooks-rs", "9781491958698"]).unwrap();
        assert_eq!(args.bookid, "9781491958698");
        assert!(!args.preserve_log);
    }

    #[test]
    fn parses_with_preserve_log_flag() {
        // safaribooks-rs --preserve-log 9781491958698
        let args =
            Args::try_parse_from(["safaribooks-rs", "--preserve-log", "9781491958698"]).unwrap();
        assert_eq!(args.bookid, "9781491958698");
        assert!(args.preserve_log);
    }

    #[test]
    fn error_when_missing_bookid() {
        // safaribooks-rs --preserve-log
        let err = Args::try_parse_from(["safaribooks-rs", "--preserve-log"]).unwrap_err();
        let msg = err.to_string();
        // clap’s message should indicate the missing required argument
        assert!(msg.contains("<BOOKID>"));
    }

    #[test]
    fn error_on_unknown_flag() {
        // safaribooks-rs --kindle 9781491958698
        let err =
            Args::try_parse_from(["safaribooks-rs", "--kindle", "9781491958698"]).unwrap_err();
        let msg = err.to_string().to_lowercase();
        assert!(msg.contains("unexpected argument '--kindle' found"));
    }

    #[test]
    fn help_and_version_are_available() {
        // `--help` and `--version` are auto-provided by clap
        let help = Args::command().render_help().to_string();
        assert!(help.contains("Usage:"));
        assert!(help.contains("<BOOKID>"));
        assert!(help.contains("--preserve-log"));

        let version = Args::command().render_version();
        assert!(!version.trim().is_empty());
    }
}