Skip to content

Commit 32cf4cd

Browse files
sylvestreRenjiSann
authored andcommitted
du, ls: add integration tests for block size env var behavior
1 parent a846f91 commit 32cf4cd

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

tests/by-util/test_du.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,74 @@ fn test_du_invalid_binary_size() {
371371
.stderr_only("du: invalid suffix in --threshold argument '0b123'\n");
372372
}
373373

374+
#[test]
375+
fn test_du_invalid_env_block_size_stops_lookup() {
376+
// When DU_BLOCK_SIZE is set but invalid, it should stop the lookup
377+
// and use the default block size — not fall through to BLOCK_SIZE.
378+
let ts = TestScenario::new(util_name!());
379+
let at = &ts.fixtures;
380+
let dir = "a";
381+
382+
at.mkdir(dir);
383+
at.write(&format!("{dir}/file"), "some content");
384+
385+
let default_output = ts
386+
.ucmd()
387+
.arg(dir)
388+
.arg("--block-size=1024")
389+
.succeeds()
390+
.stdout_move_str();
391+
392+
// Invalid DU_BLOCK_SIZE should use default (1024), not BLOCK_SIZE=1
393+
let result = ts
394+
.ucmd()
395+
.arg(dir)
396+
.env("DU_BLOCK_SIZE", "invalid")
397+
.env("BLOCK_SIZE", "1")
398+
.succeeds()
399+
.stdout_move_str();
400+
401+
assert_eq!(default_output, result);
402+
403+
// Empty DU_BLOCK_SIZE should also use default
404+
let result = ts
405+
.ucmd()
406+
.arg(dir)
407+
.env("DU_BLOCK_SIZE", "")
408+
.env("BLOCK_SIZE", "1")
409+
.succeeds()
410+
.stdout_move_str();
411+
412+
assert_eq!(default_output, result);
413+
}
414+
415+
#[test]
416+
fn test_du_posixly_correct_default() {
417+
// With POSIXLY_CORRECT and no block size env vars, default is 512
418+
let ts = TestScenario::new(util_name!());
419+
let at = &ts.fixtures;
420+
let dir = "a";
421+
422+
at.mkdir(dir);
423+
at.write(&format!("{dir}/file"), "some content");
424+
425+
let expected = ts
426+
.ucmd()
427+
.arg(dir)
428+
.arg("--block-size=512")
429+
.succeeds()
430+
.stdout_move_str();
431+
432+
let result = ts
433+
.ucmd()
434+
.arg(dir)
435+
.env("POSIXLY_CORRECT", "1")
436+
.succeeds()
437+
.stdout_move_str();
438+
439+
assert_eq!(expected, result);
440+
}
441+
374442
#[test]
375443
fn test_du_binary_edge_cases() {
376444
let ts = TestScenario::new(util_name!());

tests/by-util/test_ls.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5781,6 +5781,53 @@ fn test_ls_block_size_override() {
57815781
.stdout_contains_line("total 8");
57825782
}
57835783

5784+
#[cfg(unix)]
5785+
#[test]
5786+
#[cfg(not(target_os = "openbsd"))]
5787+
fn test_ls_block_size_si_file_size() {
5788+
// Verify --si and --block-size interaction for file size display in -l
5789+
let scene = TestScenario::new(util_name!());
5790+
let at = &scene.fixtures;
5791+
5792+
at.write_bytes("file", &[0u8; 1024]);
5793+
5794+
// --si last: file size shown as human-readable SI
5795+
scene
5796+
.ucmd()
5797+
.arg("-l")
5798+
.arg("--block-size=512")
5799+
.arg("--si")
5800+
.succeeds()
5801+
.stdout_contains("1.1k");
5802+
5803+
// --block-size last: file size shown in 512-byte blocks
5804+
scene
5805+
.ucmd()
5806+
.arg("-l")
5807+
.arg("--si")
5808+
.arg("--block-size=512")
5809+
.succeeds()
5810+
.stdout_contains(" 2 ");
5811+
5812+
// --human-readable last: file size shown as human-readable IEC
5813+
scene
5814+
.ucmd()
5815+
.arg("-l")
5816+
.arg("--block-size=512")
5817+
.arg("--human-readable")
5818+
.succeeds()
5819+
.stdout_contains("1.0K");
5820+
5821+
// --block-size last: file size shown in 512-byte blocks
5822+
scene
5823+
.ucmd()
5824+
.arg("-l")
5825+
.arg("--human-readable")
5826+
.arg("--block-size=512")
5827+
.succeeds()
5828+
.stdout_contains(" 2 ");
5829+
}
5830+
57845831
#[test]
57855832
fn test_ls_block_size_override_self() {
57865833
new_ucmd!()

0 commit comments

Comments
 (0)