Refactor help command and add tests
This commit is contained in:
parent
9c516604f2
commit
efa9d5a90b
|
|
@ -31,4 +31,4 @@ pub fn parse_duration(s: &str) -> Result<u64, ParseDurationError> {
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(ms as u64)
|
Ok(ms as u64)
|
||||||
}
|
}
|
||||||
|
|
@ -77,11 +77,30 @@ fn print_test_help() {
|
||||||
fn parse_args() -> Option<(&'static str, TestOpts)> {
|
fn parse_args() -> Option<(&'static str, TestOpts)> {
|
||||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
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();
|
print_usage();
|
||||||
process::exit(if args.is_empty() { 1 } else { 0 });
|
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();
|
let subcommand = args[0].as_str();
|
||||||
|
|
||||||
match subcommand {
|
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() {
|
fn print_example() {
|
||||||
println!(
|
println!(
|
||||||
r#"# Example .shout file
|
r#"# Example .shout file
|
||||||
|
|
|
||||||
|
|
@ -257,4 +257,4 @@ pub fn parse(path: &str, content: &str) -> Result<ShoutFile, ParseError> {
|
||||||
directives,
|
directives,
|
||||||
teardown_commands,
|
teardown_commands,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
16
shout-rs/tests/shout.rs
Normal file
16
shout-rs/tests/shout.rs
Normal 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}");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user