test(cli-rs): check parser errors once in the CLI output tests

usage_errors_match_the_typescript_cli already checks both output
streams for these exact arguments. Drop the parser tests' substring
checks for the same errors. help_matches_the_typescript_cli covers
hidden flags and visits all help pages; table_matches_the_typescript_cli
still compares the command tree without counting the fixture itself.
boolean_takes_a_literal keeps the positive boolean parsing cases.
This commit is contained in:
Simon Klee
2026-11-18 20:43:43 +01:00
Unverified
parent 51ff2d439b
commit 7b3af47991
2 changed files with 1 additions and 45 deletions
+1 -40
View File
@@ -40,8 +40,6 @@ fn negation_sets_a_boolean_to_false() {
assert_eq!(command!(&["mini"], Command::Mini(args) => args.replay()), None);
assert!(!command!(&["run", "--no-thinking", "hi"], Command::Run(args) => args.thinking));
assert!(!parse(&["--no-print-logs"]).unwrap().cli.global.print_logs);
assert!(error(&["mini", "--no-replay=true"]).contains("omit the value and use --no-replay=true by itself"));
assert!(error(&["mini", "--no-replay", "true"]).contains("omit the value"));
}
#[test]
@@ -52,16 +50,6 @@ fn boolean_takes_a_literal() {
assert_eq!(run(&["run", "--auto=0"]), (false, vec![]));
assert_eq!(run(&["run", "--auto", "hello"]), (true, vec!["hello".to_string()]));
assert_eq!(command!(&["mini", "--replay=off"], Command::Mini(args) => args.replay()), Some(false));
assert!(!command!(&["uninstall", "-f", "n"], Command::Uninstall(_) => false));
assert!(error(&["run", "--auto=maybe"]).contains("Expected \"true\" | \"yes\""));
}
#[test]
fn a_flag_given_twice_is_an_error() {
assert!(error(&["uninstall", "--dry-run", "--dry-run"]).contains("2 occurrences"));
assert!(error(&["mini", "--replay", "--no-replay"]).contains("2 occurrences"));
assert!(error(&["run", "-c", "--continue"]).contains("2 occurrences"));
assert!(error(&["run", "-s", "a", "-s", "b"]).contains("Invalid value for flag --session: \"2 occurrences\""));
}
#[test]
@@ -85,9 +73,6 @@ fn key_value_flags_need_a_key_and_a_value() {
other => panic!("{other:?}"),
});
assert_eq!(env, [("K".into(), "V".into())]);
assert!(error(&["api", "op", "--param", "novalue"]).contains("Expected format: key=value"));
assert!(error(&["api", "op", "--param", "=1"]).contains("must be non-empty"));
assert!(error(&["mcp", "add", "x", "--header", "k="]).contains("must be non-empty"));
}
#[test]
@@ -115,19 +100,11 @@ fn words_after_double_dash_are_arguments() {
}
#[test]
fn hidden_flags_parse_but_stay_out_of_help() {
fn hidden_flags_still_parse() {
assert!(command!(&["run", "--yolo"], Command::Run(args) => args.permissions.auto()));
assert!(command!(&["run", "--dangerously-skip-permissions"], Command::Run(args) => args.permissions.auto()));
assert!(parse(&["mini", "--demo"]).is_ok());
assert!(parse(&["--yolo"]).unwrap().cli.root.permissions.auto());
let help = |names: &[&str]| crate::help::render(&parse::find_path(names), "opencode", false);
assert!(
help(&["run"]).contains("--auto")
&& !help(&["run"]).contains("yolo")
&& !help(&["run"]).contains("dangerously")
);
assert!(help(&["mini"]).contains("--replay") && !help(&["mini"]).contains("--demo"));
assert!(error(&["run", "--yol"]).ends_with("in command opencode-native run"), "no suggestion of a hidden flag");
}
#[test]
@@ -141,17 +118,10 @@ fn server_flags_apply_where_declared() {
assert_eq!(server(&["--server", "http://a", "project"]).server.as_deref(), Some("http://a"));
assert_eq!(server(&["debug", "heap-dump", "--server", "http://a"]).server.as_deref(), Some("http://a"));
assert!(!server(&["debug", "heap-dump"]).standalone);
assert!(error(&["--server", "http://a", "upgrade"]).contains("Unrecognized flag: --server in command"));
assert!(error(&["--standalone", "debug", "heap-dump"]).contains("Unrecognized flag: --standalone"));
assert!(error(&["upgrade", "--server", "http://a"]).contains("--server"));
assert!(error(&["--server", "a", "session", "list", "--server", "b"]).contains("2 occurrences"));
assert!(error(&["--standalone", "auth", "list", "--standalone"]).contains("2 occurrences"));
}
#[test]
fn root_flags_do_not_pass_to_subcommands() {
assert!(error(&["-c", "upgrade"]).contains("Unrecognized flag: --continue in command opencode-native upgrade"));
assert!(error(&["--prompt", "hi", "run"]).contains("Unrecognized flag: --prompt"));
assert!(error(&["--auto", "session", "list"]).contains("Unrecognized flag: --auto"));
}
@@ -190,11 +160,8 @@ fn short_flags_cluster() {
#[test]
fn negative_numbers_are_values() {
assert_eq!(command!(&["mini", "--replay-limit", "-5"], Command::Mini(args) => args.replay_limit), Some(-5));
assert!(error(&["stats", "--days", "-1"]).contains("greater than or equal to 0"));
assert_eq!(command!(&["run", "-1.5e3"], Command::Run(args) => args.message), ["-1.5e3"]);
assert_eq!(command!(&["run", "-.5", "-"], Command::Run(args) => args.message), ["-.5", "-"]);
assert!(error(&["run", "-1x"]).contains("Unrecognized flag: -1"));
assert!(error(&["stats", "--year", "1969"]).contains("between 1970 and 9999"));
assert_eq!(command!(&["stats"], Command::Stats(args) => args.limit), 5);
}
@@ -241,19 +208,13 @@ fn only_the_first_word_names_a_subcommand() {
args.command,
Some(SessionCommand::List(list)) if list.max_count == Some(3)
)));
assert!(error(&["debug", "nope"]).contains("Unknown subcommand \"nope\" for \"opencode-native debug\""));
assert!(error(&["api", "a", "b", "c"]).contains("Unexpected positional argument: \"c\""));
assert!(error(&["api"]).contains("Missing required argument"));
}
#[test]
fn aliases_and_values_match() {
assert!(matches!(parse(&["update", "1.2.3", "-m", "brew"]).unwrap().cli.command, Some(Command::Upgrade(_))));
assert!(error(&["upgrade", "--method", "apt"]).contains("\"apt\""));
let url = command!(&["pair", "--url", "https://example.com/base//"], Command::Pair(args) => args.url);
assert_eq!(url.as_deref(), Some("https://example.com/base"));
assert!(error(&["pair", "--url", "https://user:secret@example.com"]).contains("without credentials"));
assert!(error(&["pair", "--url", "ftp://example.com"]).contains("HTTP(S)"));
for url in ["https://bücher.example", "https://Bücher.example", "https://%42%C3%BCcher.example"] {
assert_eq!(
error(&["pair", "--url", url]),
@@ -9,7 +9,6 @@ fn fixture() -> Value {
#[test]
fn table_matches_the_typescript_cli() {
let fixture = fixture();
assert_eq!(count(&fixture["root"]), 48, "commands.ts has 48 commands");
let builtins: Vec<Value> = BUILTINS.iter().chain(ROOT.globals).map(flag).collect();
let mut diffs = Vec::new();
compare("builtins", &fixture["builtins"], &Value::from(builtins), &mut diffs);
@@ -17,10 +16,6 @@ fn table_matches_the_typescript_cli() {
assert!(diffs.is_empty(), "the table differs from the TypeScript tree:\n{}", diffs.join("\n"));
}
fn count(node: &Value) -> usize {
1 + node["commands"].as_array().unwrap().iter().map(count).sum::<usize>()
}
fn compare(path: &str, expected: &Value, actual: &Value, diffs: &mut Vec<String>) {
if expected != actual {
diffs.push(format!("{path}:\n TypeScript {expected}\n Rust {actual}"));