From 32cf4cdf0c61ca29d5287e6e857d36b4cee1b9cd Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 6 Apr 2026 15:25:32 +0200 Subject: [PATCH] du, ls: add integration tests for block size env var behavior --- tests/by-util/test_du.rs | 68 ++++++++++++++++++++++++++++++++++++++++ tests/by-util/test_ls.rs | 47 +++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 2b3f54302..b6a879aee 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -371,6 +371,74 @@ fn test_du_invalid_binary_size() { .stderr_only("du: invalid suffix in --threshold argument '0b123'\n"); } +#[test] +fn test_du_invalid_env_block_size_stops_lookup() { + // When DU_BLOCK_SIZE is set but invalid, it should stop the lookup + // and use the default block size — not fall through to BLOCK_SIZE. + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + let dir = "a"; + + at.mkdir(dir); + at.write(&format!("{dir}/file"), "some content"); + + let default_output = ts + .ucmd() + .arg(dir) + .arg("--block-size=1024") + .succeeds() + .stdout_move_str(); + + // Invalid DU_BLOCK_SIZE should use default (1024), not BLOCK_SIZE=1 + let result = ts + .ucmd() + .arg(dir) + .env("DU_BLOCK_SIZE", "invalid") + .env("BLOCK_SIZE", "1") + .succeeds() + .stdout_move_str(); + + assert_eq!(default_output, result); + + // Empty DU_BLOCK_SIZE should also use default + let result = ts + .ucmd() + .arg(dir) + .env("DU_BLOCK_SIZE", "") + .env("BLOCK_SIZE", "1") + .succeeds() + .stdout_move_str(); + + assert_eq!(default_output, result); +} + +#[test] +fn test_du_posixly_correct_default() { + // With POSIXLY_CORRECT and no block size env vars, default is 512 + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + let dir = "a"; + + at.mkdir(dir); + at.write(&format!("{dir}/file"), "some content"); + + let expected = ts + .ucmd() + .arg(dir) + .arg("--block-size=512") + .succeeds() + .stdout_move_str(); + + let result = ts + .ucmd() + .arg(dir) + .env("POSIXLY_CORRECT", "1") + .succeeds() + .stdout_move_str(); + + assert_eq!(expected, result); +} + #[test] fn test_du_binary_edge_cases() { let ts = TestScenario::new(util_name!()); diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index fe056008d..fb9a00fa2 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -5781,6 +5781,53 @@ fn test_ls_block_size_override() { .stdout_contains_line("total 8"); } +#[cfg(unix)] +#[test] +#[cfg(not(target_os = "openbsd"))] +fn test_ls_block_size_si_file_size() { + // Verify --si and --block-size interaction for file size display in -l + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write_bytes("file", &[0u8; 1024]); + + // --si last: file size shown as human-readable SI + scene + .ucmd() + .arg("-l") + .arg("--block-size=512") + .arg("--si") + .succeeds() + .stdout_contains("1.1k"); + + // --block-size last: file size shown in 512-byte blocks + scene + .ucmd() + .arg("-l") + .arg("--si") + .arg("--block-size=512") + .succeeds() + .stdout_contains(" 2 "); + + // --human-readable last: file size shown as human-readable IEC + scene + .ucmd() + .arg("-l") + .arg("--block-size=512") + .arg("--human-readable") + .succeeds() + .stdout_contains("1.0K"); + + // --block-size last: file size shown in 512-byte blocks + scene + .ucmd() + .arg("-l") + .arg("--human-readable") + .arg("--block-size=512") + .succeeds() + .stdout_contains(" 2 "); +} + #[test] fn test_ls_block_size_override_self() { new_ucmd!()