Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,17 +1016,15 @@ impl Stater {
.get_many::<OsString>(options::FILES)
.map(|v| v.cloned().collect())
.unwrap();
let format_str = if matches.contains_id(options::PRINTF) {
matches
.get_one::<String>(options::PRINTF)
.expect("Invalid format string")
let use_printf = matches.contains_id(options::PRINTF);
let format_option = if use_printf {
options::PRINTF
} else {
matches
.get_one::<String>(options::FORMAT)
.map_or("", |s| s.as_str())
options::FORMAT
};

let use_printf = matches.contains_id(options::PRINTF);
// An empty format given by the user is still used instead of the default.
let user_format = matches.get_one::<String>(format_option).map(String::as_str);
let format_str = user_format.unwrap_or_default();
let terse = matches.get_flag(options::TERSE);
let show_fs = matches.get_flag(options::FILE_SYSTEM);

Expand All @@ -1037,17 +1035,13 @@ impl Stater {
OptionValue::with_names(
format_str,
if use_printf { None } else { Some('c') },
Some(if use_printf {
options::PRINTF
} else {
options::FORMAT
}),
Some(format_option),
)
};
// A format stat built itself cannot hold an unknown directive, so an
// error there is our bug, has nothing to point at, and is raised now.
let mut format_error = None;
let default_tokens = if format_str.is_empty() {
let default_tokens = if user_format.is_none() {
let (tokens, error) =
Self::generate_tokens(&Self::default_format(show_fs, terse, false), use_printf);
if let Some(error) = error {
Expand Down Expand Up @@ -1080,7 +1074,7 @@ impl Stater {
Ok(Self {
follow: matches.get_flag(options::DEREFERENCE),
show_fs,
from_user: !format_str.is_empty(),
from_user: user_format.is_some(),
files,
mount_list: OnceCell::new(),
mount_list_needed,
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,24 @@ fn test_format_adds_newline_after_trailing_newline() {
.stdout_only(".\n");
}

#[rstest]
#[case::format_short(&["-c", "", "."], "\n")]
#[case::format_long(&["--format=", "."], "\n")]
#[case::format_two_files(&["-c", "", ".", "."], "\n\n")]
#[case::format_terse(&["-t", "-c", "", "."], "\n")]
#[case::printf(&["--printf=", "."], "")]
fn test_empty_format_is_used(#[case] args: &[&str], #[case] expected: &str) {
new_ucmd!().args(args).succeeds().stdout_only(expected);
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[rstest]
#[case::format(&["-f", "-c", "", "."], "\n")]
#[case::printf(&["-f", "--printf=", "."], "")]
fn test_fs_empty_format_is_used(#[case] args: &[&str], #[case] expected: &str) {
new_ucmd!().args(args).succeeds().stdout_only(expected);
}

#[test]
fn test_default_formats_end_with_a_single_newline() {
let ts = TestScenario::new(util_name!());
Expand Down
Loading