From 4f42cd4887b9402287ef7afb03fbdd88439a9390 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Thu, 2 Apr 2026 14:15:37 -0700 Subject: [PATCH] Refactor help command and add tests --- shout-rs/src/duration.rs | 2 +- shout-rs/src/main.rs | 39 ++++++++++++++++++++++++++++++++++++++- shout-rs/src/parse.rs | 2 +- shout-rs/tests/shout.rs | 16 ++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 shout-rs/tests/shout.rs diff --git a/shout-rs/src/duration.rs b/shout-rs/src/duration.rs index 85b6022..1ff9bbf 100644 --- a/shout-rs/src/duration.rs +++ b/shout-rs/src/duration.rs @@ -31,4 +31,4 @@ pub fn parse_duration(s: &str) -> Result { }; Ok(ms as u64) -} +} \ No newline at end of file diff --git a/shout-rs/src/main.rs b/shout-rs/src/main.rs index f85e958..cdb7335 100644 --- a/shout-rs/src/main.rs +++ b/shout-rs/src/main.rs @@ -77,11 +77,30 @@ fn print_test_help() { fn parse_args() -> Option<(&'static str, TestOpts)> { let args: Vec = std::env::args().skip(1).collect(); - if args.is_empty() || args[0] == "-h" || args[0] == "--help" || args[0] == "help" { + if args.is_empty() || args[0] == "-h" || args[0] == "--help" { print_usage(); process::exit(if args.is_empty() { 1 } else { 0 }); } + if args[0] == "help" { + if args.len() < 2 { + print_usage(); + } else { + match args[1].as_str() { + "test" => print_test_help(), + "example" => print_example_help(), + "version" => print_version_help(), + "help" => print_help_help(), + other => { + eprintln!("Unknown command: {other}"); + eprintln!("Run 'shout --help' for usage"); + process::exit(1); + } + } + } + process::exit(0); + } + let subcommand = args[0].as_str(); match subcommand { @@ -463,6 +482,24 @@ fn pathdiff(path: &Path, base: &Path) -> String { } } +fn print_example_help() { + eprintln!("Usage: shout example"); + eprintln!(); + eprintln!("Print an example .shout file"); +} + +fn print_version_help() { + eprintln!("Usage: shout version"); + eprintln!(); + eprintln!("Print the version"); +} + +fn print_help_help() { + eprintln!("Usage: shout help [command]"); + eprintln!(); + eprintln!("Display help for command"); +} + fn print_example() { println!( r#"# Example .shout file diff --git a/shout-rs/src/parse.rs b/shout-rs/src/parse.rs index da53873..c617f22 100644 --- a/shout-rs/src/parse.rs +++ b/shout-rs/src/parse.rs @@ -257,4 +257,4 @@ pub fn parse(path: &str, content: &str) -> Result { directives, teardown_commands, }) -} +} \ No newline at end of file diff --git a/shout-rs/tests/shout.rs b/shout-rs/tests/shout.rs new file mode 100644 index 0000000..3cf18e2 --- /dev/null +++ b/shout-rs/tests/shout.rs @@ -0,0 +1,16 @@ +use std::process::Command; + +fn shout() -> Command { + let mut cmd = Command::new(env!("CARGO_BIN_EXE_shout")); + cmd.current_dir(concat!(env!("CARGO_MANIFEST_DIR"))); + cmd +} + +#[test] +fn test_suite_passes() { + let status = shout() + .args(["test", "test/"]) + .status() + .unwrap(); + assert!(status.success(), "shout test test/ exited with {status}"); +}