Refactor help command and add tests

This commit is contained in:
Chris Wanstrath 2026-04-02 14:15:37 -07:00
parent 7ca6e12263
commit 4f42cd4887
4 changed files with 56 additions and 3 deletions

View File

@ -31,4 +31,4 @@ pub fn parse_duration(s: &str) -> Result<u64, ParseDurationError> {
};
Ok(ms as u64)
}
}

View File

@ -77,11 +77,30 @@ fn print_test_help() {
fn parse_args() -> Option<(&'static str, TestOpts)> {
let args: Vec<String> = 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

View File

@ -257,4 +257,4 @@ pub fn parse(path: &str, content: &str) -> Result<ShoutFile, ParseError> {
directives,
teardown_commands,
})
}
}

16
shout-rs/tests/shout.rs Normal file
View File

@ -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}");
}