Skip to content

Commit 1330cdf

Browse files
committed
fix(test-lexarg): Improve error consistency
1 parent fd09f43 commit 1330cdf

1 file changed

Lines changed: 27 additions & 54 deletions

File tree

  • crates/libtest-lexarg/src

crates/libtest-lexarg/src/lib.rs

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -330,35 +330,28 @@ impl TestOptsParseState {
330330
Long("logfile") => {
331331
let path = parser
332332
.next_flag_value()
333-
.ok_or_else(|| ErrorContext::msg("`--logfile` requires a path"))?;
334-
self.opts.logfile = Some(std::path::PathBuf::from(path));
333+
.ok_or_missing(Value(std::ffi::OsStr::new("PATH")))
334+
.path()
335+
.within(arg)?;
336+
self.opts.logfile = Some(path.to_owned());
335337
}
336338
Long("nocapture") => {
337339
self.opts.nocapture = true;
338340
}
339341
Long("test-threads") => {
340342
let test_threads = parser
341343
.next_flag_value()
342-
.ok_or_else(|| {
343-
ErrorContext::msg("`--test-threads` requires a positive integer")
344-
})?
345-
.to_str()
346-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?;
347-
self.opts.test_threads = match test_threads.parse::<std::num::NonZeroUsize>() {
348-
Ok(n) => Some(n),
349-
Err(_) => {
350-
return Err(ErrorContext::msg(
351-
"`--test-threads` must be a positive integer",
352-
));
353-
}
354-
};
344+
.ok_or_missing(Value(std::ffi::OsStr::new("NUM")))
345+
.parse()
346+
.within(arg)?;
347+
self.opts.test_threads = Some(test_threads);
355348
}
356349
Long("skip") => {
357350
let filter = parser
358351
.next_flag_value()
359-
.ok_or_else(|| ErrorContext::msg("`--skip` requires a value"))?
360-
.to_str()
361-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?;
352+
.ok_or_missing(Value(std::ffi::OsStr::new("NAME")))
353+
.string("NAME")
354+
.within(arg)?;
362355
self.opts.skip.push(filter.to_owned());
363356
}
364357
Long("exact") => {
@@ -367,20 +360,14 @@ impl TestOptsParseState {
367360
Long("color") => {
368361
let color = parser
369362
.next_flag_value()
370-
.ok_or_else(|| {
371-
ErrorContext::msg("`--color` requires one of `auto`, `always`, or `never`")
372-
})?
373-
.to_str()
374-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?;
363+
.ok_or_missing(Value(std::ffi::OsStr::new("WHEN")))
364+
.one_of(&["auto", "always", "never"])
365+
.within(arg)?;
375366
self.opts.color = match color {
376367
"auto" => ColorConfig::AutoColor,
377368
"always" => ColorConfig::AlwaysColor,
378369
"never" => ColorConfig::NeverColor,
379-
_ => {
380-
return Err(ErrorContext::msg(
381-
"`--color` accepts `auto`, `always`, or `never`",
382-
));
383-
}
370+
_ => unreachable!("`one_of` should prevent this"),
384371
};
385372
}
386373
Short("q") | Long("quiet") => {
@@ -391,23 +378,15 @@ impl TestOptsParseState {
391378
self.quiet = false;
392379
let format = parser
393380
.next_flag_value()
394-
.ok_or_else(|| {
395-
ErrorContext::msg(
396-
"`--format` requires one of `pretty`, `terse`, `json`, or `junit`",
397-
)
398-
})?
399-
.to_str()
400-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?;
381+
.ok_or_missing(Value(std::ffi::OsStr::new("FORMAT")))
382+
.one_of(&["pretty", "terse", "json", "junit"])
383+
.within(arg)?;
401384
self.format = Some(match format {
402385
"pretty" => OutputFormat::Pretty,
403386
"terse" => OutputFormat::Terse,
404387
"json" => OutputFormat::Json,
405388
"junit" => OutputFormat::Junit,
406-
_ => {
407-
return Err(ErrorContext::msg(
408-
"`--format` accepts `pretty`, `terse`, `json`, or `junit`",
409-
));
410-
}
389+
_ => unreachable!("`one_of` should prevent this"),
411390
});
412391
}
413392
Long("show-output") => {
@@ -416,13 +395,11 @@ impl TestOptsParseState {
416395
Short("Z") => {
417396
let feature = parser
418397
.next_flag_value()
419-
.ok_or_else(|| ErrorContext::msg("`-Z` requires a feature name"))?
420-
.to_str()
421-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?;
398+
.ok_or_missing(Value(std::ffi::OsStr::new("FEATURE")))
399+
.string("FEATURE")
400+
.within(arg)?;
422401
if !is_nightly() {
423-
return Err(ErrorContext::msg(
424-
"`-Z` is only accepted on the nightly compiler",
425-
));
402+
return Err(ErrorContext::msg("expected nightly compiler").unexpected(arg));
426403
}
427404
// Don't validate `feature` as other parsers might provide values
428405
self.opts.allowed_unstable.push(feature.to_owned());
@@ -450,17 +427,13 @@ impl TestOptsParseState {
450427
Long("shuffle-seed") => {
451428
let seed = parser
452429
.next_flag_value()
453-
.ok_or_else(|| ErrorContext::msg("`--shuffle-seed` requires a value"))?
454-
.to_str()
455-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?
456-
.parse::<u64>()
457-
.map_err(ErrorContext::msg)?;
430+
.ok_or_missing(Value(std::ffi::OsStr::new("SEED")))
431+
.parse()
432+
.within(arg)?;
458433
self.opts.shuffle_seed = Some(seed);
459434
}
460435
Value(filter) => {
461-
let filter = filter
462-
.to_str()
463-
.ok_or_else(|| ErrorContext::msg("unsupported value"))?;
436+
let filter = filter.string("FILTER")?;
464437
self.opts.filters.push(filter.to_owned());
465438
}
466439
_ => {

0 commit comments

Comments
 (0)