Skip to main content

testing_conventions/
lib.rs

1use clap::{Parser, Subcommand};
2
3#[derive(Parser, Debug)]
4#[command(
5    name = "testing-conventions",
6    version,
7    about = "Enforce testing conventions in libraries (Python, TypeScript, and Rust).",
8    long_about = None,
9)]
10pub struct Cli {
11    #[command(subcommand)]
12    command: Option<Command>,
13}
14
15#[derive(Subcommand, Debug)]
16enum Command {
17    /// Check the repository against its testing-conventions config.
18    Check,
19}
20
21pub fn run<I, T>(args: I) -> anyhow::Result<i32>
22where
23    I: IntoIterator<Item = T>,
24    T: Into<std::ffi::OsString> + Clone,
25{
26    let cli = Cli::try_parse_from(args)?;
27    match cli.command {
28        // Rules aren't implemented yet; the scaffold just proves the wiring.
29        Some(Command::Check) | None => Ok(0),
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn no_args_returns_ok_zero() {
39        assert_eq!(run(["testing-conventions"]).unwrap(), 0);
40    }
41
42    #[test]
43    fn check_returns_ok_zero() {
44        assert_eq!(run(["testing-conventions", "check"]).unwrap(), 0);
45    }
46
47    #[test]
48    fn unknown_flag_errors() {
49        assert!(run(["testing-conventions", "--bogus"]).is_err());
50    }
51
52    #[test]
53    fn help_flag_returns_clap_display_help() {
54        let err = run(["testing-conventions", "--help"]).expect_err("--help should bubble");
55        let clap_err = err
56            .downcast_ref::<clap::Error>()
57            .expect("error should be a clap::Error");
58        assert_eq!(clap_err.kind(), clap::error::ErrorKind::DisplayHelp);
59    }
60
61    #[test]
62    fn version_flag_returns_clap_display_version() {
63        let err = run(["testing-conventions", "--version"]).expect_err("--version should bubble");
64        let clap_err = err
65            .downcast_ref::<clap::Error>()
66            .expect("error should be a clap::Error");
67        assert_eq!(clap_err.kind(), clap::error::ErrorKind::DisplayVersion);
68    }
69}