df: show "block-size argument too large" error

This commit is contained in:
Daniel Hofstetter 2022-04-30 15:36:50 +02:00
parent 69f8543d8f
commit 15412f100a
2 changed files with 39 additions and 6 deletions

View file

@ -14,6 +14,7 @@ mod table;
use uucore::display::Quotable;
use uucore::error::{UError, UResult, USimpleError};
use uucore::fsext::{read_fs_list, MountInfo};
use uucore::parse_size::ParseSizeError;
use uucore::{format_usage, show};
use clap::{crate_version, Arg, ArgMatches, Command};
@ -105,7 +106,8 @@ impl Default for Options {
#[derive(Debug)]
enum OptionsError {
InvalidBlockSize,
BlockSizeTooLarge(String),
InvalidBlockSize(String),
/// An error getting the columns to display in the output table.
ColumnError(ColumnError),
@ -116,11 +118,14 @@ enum OptionsError {
impl fmt::Display for OptionsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
// TODO This should include the raw string provided as the argument.
//
// TODO This needs to vary based on whether `--block-size`
// or `-B` were provided.
Self::InvalidBlockSize => write!(f, "invalid --block-size argument"),
Self::BlockSizeTooLarge(s) => {
write!(f, "--block-size argument {} too large", s.quote())
}
// TODO This needs to vary based on whether `--block-size`
// or `-B` were provided.
Self::InvalidBlockSize(s) => write!(f, "invalid --block-size argument {}", s),
Self::ColumnError(ColumnError::MultipleColumns(s)) => write!(
f,
"option --output: field {} used more than once",
@ -155,8 +160,12 @@ impl Options {
Ok(Self {
show_local_fs: matches.is_present(OPT_LOCAL),
show_all_fs: matches.is_present(OPT_ALL),
block_size: block_size_from_matches(matches)
.map_err(|_| OptionsError::InvalidBlockSize)?,
block_size: block_size_from_matches(matches).map_err(|e| match e {
ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge(
matches.value_of(OPT_BLOCKSIZE).unwrap().to_string(),
),
ParseSizeError::ParseFailure(s) => OptionsError::InvalidBlockSize(s),
})?,
include,
exclude,
show_total: matches.is_present(OPT_TOTAL),

View file

@ -419,6 +419,30 @@ fn test_block_size_with_suffix() {
//assert_eq!(get_header("1GB"), "1GB-blocks");
}
#[test]
fn test_too_large_block_size() {
fn run_command(size: &str) {
new_ucmd!()
.arg(format!("--block-size={}", size))
.fails()
.stderr_contains(format!("--block-size argument '{}' too large", size));
}
let too_large_sizes = vec!["1Y", "1Z"];
for size in too_large_sizes {
run_command(size);
}
}
#[test]
fn test_invalid_block_size() {
new_ucmd!()
.arg("--block-size=x")
.fails()
.stderr_contains("invalid --block-size argument 'x'");
}
#[test]
fn test_output_selects_columns() {
let output = new_ucmd!()