Skip to content

Commit 64181e9

Browse files
committed
rewatch: add baseline clap behavior tests
1 parent 8186450 commit 64181e9

File tree

1 file changed

+63
-14
lines changed

1 file changed

+63
-14
lines changed

rewatch/src/main.rs

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,11 @@ mod tests {
228228
parse_cli(args.iter().map(OsString::from).collect())
229229
}
230230

231+
// Default command behaviour.
231232
#[test]
232-
fn defaults_to_build_without_args() {
233+
fn no_subcommand_defaults_to_build() {
233234
let cli = parse(&["rescript"]).expect("expected default build command");
234-
235-
match cli.command {
236-
cli::Command::Build(build_args) => assert_eq!(build_args.folder.folder, "."),
237-
other => panic!("expected build command, got {other:?}"),
238-
}
235+
assert!(matches!(cli.command, cli::Command::Build(_)));
239236
}
240237

241238
#[test]
@@ -249,37 +246,89 @@ mod tests {
249246
}
250247

251248
#[test]
252-
fn respects_global_flag_before_subcommand() {
253-
let cli = parse(&["rescript", "-v", "watch"]).expect("expected watch command");
249+
fn trailing_global_flag_is_treated_as_global() {
250+
let cli = parse(&["rescript", "my-project", "-v"]).expect("expected build command");
254251

255-
assert!(matches!(cli.command, cli::Command::Watch(_)));
252+
assert_eq!(cli.verbose.log_level_filter(), LevelFilter::Debug);
253+
match cli.command {
254+
cli::Command::Build(build_args) => assert_eq!(build_args.folder.folder, "my-project"),
255+
other => panic!("expected build command, got {other:?}"),
256+
}
256257
}
257258

258259
#[test]
259-
fn trailing_global_flag_is_treated_as_global() {
260-
let cli = parse(&["rescript", "my-project", "-v"]).expect("expected build command");
260+
fn unknown_subcommand_help_uses_global_help() {
261+
let err = parse(&["rescript", "xxx", "--help"]).expect_err("expected global help");
262+
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
263+
}
264+
265+
// Build command specifics.
266+
#[test]
267+
fn build_help_shows_subcommand_help() {
268+
let err = parse(&["rescript", "build", "--help"]).expect_err("expected subcommand help");
269+
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
270+
let rendered = err.to_string();
271+
assert!(
272+
rendered.contains("Usage: rescript build"),
273+
"unexpected help: {rendered:?}"
274+
);
275+
assert!(!rendered.contains("Usage: rescript [OPTIONS] <COMMAND>"));
276+
}
261277

278+
#[test]
279+
fn build_allows_global_verbose_flag() {
280+
let cli = parse(&["rescript", "build", "-v"]).expect("expected build command");
262281
assert_eq!(cli.verbose.log_level_filter(), LevelFilter::Debug);
282+
assert!(matches!(cli.command, cli::Command::Build(_)));
283+
}
284+
285+
#[test]
286+
fn build_option_is_parsed_normally() {
287+
let cli = parse(&["rescript", "build", "--no-timing"]).expect("expected build command");
288+
263289
match cli.command {
264-
cli::Command::Build(build_args) => assert_eq!(build_args.folder.folder, "my-project"),
290+
cli::Command::Build(build_args) => assert!(build_args.no_timing),
265291
other => panic!("expected build command, got {other:?}"),
266292
}
267293
}
268294

295+
// Subcommand flag handling.
296+
#[test]
297+
fn respects_global_flag_before_subcommand() {
298+
let cli = parse(&["rescript", "-v", "watch"]).expect("expected watch command");
299+
300+
assert!(matches!(cli.command, cli::Command::Watch(_)));
301+
}
302+
269303
#[test]
270304
fn invalid_option_for_subcommand_does_not_fallback() {
271305
let err = parse(&["rescript", "watch", "--no-timing"]).expect_err("expected watch parse failure");
272306
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
273307
}
274308

309+
// Version/help flag handling.
310+
#[test]
311+
fn version_flag_before_subcommand_displays_version() {
312+
let err = parse(&["rescript", "-V", "build"]).expect_err("expected version display");
313+
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
314+
}
315+
316+
#[test]
317+
fn version_flag_after_subcommand_is_rejected() {
318+
let err = parse(&["rescript", "build", "-V"]).expect_err("expected unexpected argument");
319+
assert_eq!(err.kind(), ErrorKind::UnknownArgument);
320+
}
321+
275322
#[test]
276-
fn help_flag_does_not_default_to_build() {
323+
fn global_help_flag_shows_help() {
277324
let err = parse(&["rescript", "--help"]).expect_err("expected clap help error");
278325
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
326+
let rendered = err.to_string();
327+
assert!(rendered.contains("Usage: rescript [OPTIONS] <COMMAND>"));
279328
}
280329

281330
#[test]
282-
fn version_flag_does_not_default_to_build() {
331+
fn global_version_flag_shows_version() {
283332
let err = parse(&["rescript", "--version"]).expect_err("expected clap version error");
284333
assert_eq!(err.kind(), ErrorKind::DisplayVersion);
285334
}

0 commit comments

Comments
 (0)