Skip to content

Commit 25c7f25

Browse files
committed
test_df: Use lossy stdout string
The tests fail when a non-UTF8 path is mounted, that's... not a common case, but using a lossy string works just as well for the tests, so let's use that.
1 parent 905dfc6 commit 25c7f25

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

tests/by-util/test_df.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn test_df_output() {
117117
.arg("-H")
118118
.arg("--total")
119119
.succeeds()
120-
.stdout_move_str();
120+
.stdout_str_lossy();
121121
let actual = output.lines().take(1).collect::<Vec<&str>>()[0];
122122
let actual = actual.split_whitespace().collect::<Vec<_>>();
123123
assert_eq!(actual, expected);
@@ -151,7 +151,7 @@ fn test_df_output_overridden() {
151151
.arg("-hH")
152152
.arg("--total")
153153
.succeeds()
154-
.stdout_move_str();
154+
.stdout_str_lossy();
155155
let actual = output.lines().take(1).collect::<Vec<&str>>()[0];
156156
let actual = actual.split_whitespace().collect::<Vec<_>>();
157157
assert_eq!(actual, expected);
@@ -181,7 +181,7 @@ fn test_default_headers() {
181181
"on",
182182
]
183183
};
184-
let output = new_ucmd!().succeeds().stdout_move_str();
184+
let output = new_ucmd!().succeeds().stdout_str_lossy();
185185
let actual = output.lines().take(1).collect::<Vec<&str>>()[0];
186186
let actual = actual.split_whitespace().collect::<Vec<_>>();
187187
assert_eq!(actual, expected);
@@ -195,7 +195,7 @@ fn test_precedence_of_human_readable_and_si_header_over_output_header() {
195195
let output = new_ucmd!()
196196
.args(&[arg, "--output=size"])
197197
.succeeds()
198-
.stdout_move_str();
198+
.stdout_str_lossy();
199199
let header = output.lines().next().unwrap();
200200
assert_eq!(header, " Size");
201201
}
@@ -207,7 +207,7 @@ fn test_used_header_starts_with_space() {
207207
// using -h here to ensure the width of the column's content is <= 4
208208
.args(&["-h", "--output=used"])
209209
.succeeds()
210-
.stdout_move_str();
210+
.stdout_str_lossy();
211211
let header = output.lines().next().unwrap();
212212
assert_eq!(header, " Used");
213213
}
@@ -226,19 +226,19 @@ fn test_order_same() {
226226
let output1 = new_ucmd!()
227227
.arg("--output=source")
228228
.succeeds()
229-
.stdout_move_str();
229+
.stdout_str_lossy();
230230
let output2 = new_ucmd!()
231231
.arg("--output=source")
232232
.succeeds()
233-
.stdout_move_str();
233+
.stdout_str_lossy();
234234
assert_eq!(output1, output2);
235235
}
236236

237237
/// Test of mount point begin repeated
238238
#[cfg(all(unix, not(target_os = "freebsd")))] // FIXME: fix this test for FreeBSD
239239
#[test]
240240
fn test_output_mp_repeat() {
241-
let output1 = new_ucmd!().arg("/").arg("/").succeeds().stdout_move_str();
241+
let output1 = new_ucmd!().arg("/").arg("/").succeeds().stdout_str_lossy();
242242
let output1: Vec<String> = output1
243243
.lines()
244244
.map(|l| String::from(l.split_once(' ').unwrap().0))
@@ -272,7 +272,7 @@ fn test_type_option() {
272272
let fs_types = new_ucmd!()
273273
.arg("--output=fstype")
274274
.succeeds()
275-
.stdout_move_str();
275+
.stdout_str_lossy();
276276
let fs_type = fs_types.lines().nth(1).unwrap().trim();
277277

278278
new_ucmd!().args(&["-t", fs_type]).succeeds();
@@ -292,7 +292,7 @@ fn test_type_option_with_file() {
292292
let fs_type = new_ucmd!()
293293
.args(&["--output=fstype", "."])
294294
.succeeds()
295-
.stdout_move_str();
295+
.stdout_str_lossy();
296296
let fs_type = fs_type.lines().nth(1).unwrap().trim();
297297

298298
new_ucmd!().args(&["-t", fs_type, "."]).succeeds();
@@ -310,7 +310,7 @@ fn test_type_option_with_file() {
310310
let fs_types = new_ucmd!()
311311
.arg("--output=fstype")
312312
.succeeds()
313-
.stdout_move_str();
313+
.stdout_str_lossy();
314314
let fs_types: Vec<_> = fs_types
315315
.lines()
316316
.skip(1)
@@ -335,7 +335,7 @@ fn test_exclude_all_types() {
335335
let fs_types = new_ucmd!()
336336
.arg("--output=fstype")
337337
.succeeds()
338-
.stdout_move_str();
338+
.stdout_str_lossy();
339339
let fs_types: HashSet<_> = fs_types.lines().skip(1).collect();
340340

341341
let mut args = Vec::new();
@@ -379,7 +379,7 @@ fn test_total() {
379379
// ...
380380
// /dev/loop14 63488 63488 0 100% /snap/core20/1361
381381
// total 258775268 98099712 148220200 40% -
382-
let output = new_ucmd!().arg("--total").succeeds().stdout_move_str();
382+
let output = new_ucmd!().arg("--total").succeeds().stdout_str_lossy();
383383

384384
// Skip the header line.
385385
let lines: Vec<&str> = output.lines().skip(1).collect();
@@ -422,21 +422,21 @@ fn test_total_label_in_correct_column() {
422422
let output = new_ucmd!()
423423
.args(&["--output=source", "--total", "."])
424424
.succeeds()
425-
.stdout_move_str();
425+
.stdout_str_lossy();
426426
let last_line = output.lines().last().unwrap();
427427
assert_eq!(last_line.trim(), "total");
428428

429429
let output = new_ucmd!()
430430
.args(&["--output=target", "--total", "."])
431431
.succeeds()
432-
.stdout_move_str();
432+
.stdout_str_lossy();
433433
let last_line = output.lines().last().unwrap();
434434
assert_eq!(last_line.trim(), "total");
435435

436436
let output = new_ucmd!()
437437
.args(&["--output=source,target", "--total", "."])
438438
.succeeds()
439-
.stdout_move_str();
439+
.stdout_str_lossy();
440440
let last_line = output.lines().last().unwrap();
441441
assert_eq!(
442442
last_line.split_whitespace().collect::<Vec<&str>>(),
@@ -446,7 +446,7 @@ fn test_total_label_in_correct_column() {
446446
let output = new_ucmd!()
447447
.args(&["--output=target,source", "--total", "."])
448448
.succeeds()
449-
.stdout_move_str();
449+
.stdout_str_lossy();
450450
let last_line = output.lines().last().unwrap();
451451
assert_eq!(
452452
last_line.split_whitespace().collect::<Vec<&str>>(),
@@ -463,7 +463,7 @@ fn test_use_percentage() {
463463
// "percentage" values.
464464
.args(&["--total", "--output=used,avail,pcent", "--block-size=1"])
465465
.succeeds()
466-
.stdout_move_str();
466+
.stdout_str_lossy();
467467

468468
// Skip the header line.
469469
let lines: Vec<&str> = output.lines().skip(1).collect();
@@ -488,7 +488,7 @@ fn test_iuse_percentage() {
488488
let output = new_ucmd!()
489489
.args(&["--total", "--output=itotal,iused,ipcent"])
490490
.succeeds()
491-
.stdout_move_str();
491+
.stdout_str_lossy();
492492

493493
// Skip the header line.
494494
let lines: Vec<&str> = output.lines().skip(1).collect();
@@ -518,7 +518,7 @@ fn test_default_block_size() {
518518
let output = new_ucmd!()
519519
.arg("--output=size")
520520
.succeeds()
521-
.stdout_move_str();
521+
.stdout_str_lossy();
522522
let header = output.lines().next().unwrap().trim().to_string();
523523

524524
assert_eq!(header, "1K-blocks");
@@ -527,7 +527,7 @@ fn test_default_block_size() {
527527
.arg("--output=size")
528528
.env("POSIXLY_CORRECT", "1")
529529
.succeeds()
530-
.stdout_move_str();
530+
.stdout_str_lossy();
531531
let header = output.lines().next().unwrap().trim().to_string();
532532

533533
assert_eq!(header, "512B-blocks");
@@ -547,14 +547,14 @@ fn test_default_block_size_in_posix_portability_mode() {
547547
.to_string()
548548
}
549549

550-
let output = new_ucmd!().arg("-P").succeeds().stdout_move_str();
550+
let output = new_ucmd!().arg("-P").succeeds().stdout_str_lossy();
551551
assert_eq!(get_header(&output), "1024-blocks");
552552

553553
let output = new_ucmd!()
554554
.arg("-P")
555555
.env("POSIXLY_CORRECT", "1")
556556
.succeeds()
557-
.stdout_move_str();
557+
.stdout_str_lossy();
558558
assert_eq!(get_header(&output), "512-blocks");
559559
}
560560

@@ -564,7 +564,7 @@ fn test_block_size_1024() {
564564
let output = new_ucmd!()
565565
.args(&["-B", &format!("{block_size}"), "--output=size"])
566566
.succeeds()
567-
.stdout_move_str();
567+
.stdout_str_lossy();
568568
output.lines().next().unwrap().trim().to_string()
569569
}
570570

@@ -588,7 +588,7 @@ fn test_block_size_with_suffix() {
588588
let output = new_ucmd!()
589589
.args(&["-B", block_size, "--output=size"])
590590
.succeeds()
591-
.stdout_move_str();
591+
.stdout_str_lossy();
592592
output.lines().next().unwrap().trim().to_string()
593593
}
594594

@@ -612,7 +612,7 @@ fn test_block_size_in_posix_portability_mode() {
612612
let output = new_ucmd!()
613613
.args(&["-P", "-B", block_size])
614614
.succeeds()
615-
.stdout_move_str();
615+
.stdout_str_lossy();
616616
output
617617
.lines()
618618
.next()
@@ -639,7 +639,7 @@ fn test_block_size_from_env() {
639639
.arg("--output=size")
640640
.env(env_var, env_value)
641641
.succeeds()
642-
.stdout_move_str();
642+
.stdout_str_lossy();
643643
output.lines().next().unwrap().trim().to_string()
644644
}
645645

@@ -658,7 +658,7 @@ fn test_block_size_from_env_precedences() {
658658
.env(k1, v1)
659659
.env(k2, v2)
660660
.succeeds()
661-
.stdout_move_str();
661+
.stdout_str_lossy();
662662
output.lines().next().unwrap().trim().to_string()
663663
}
664664

@@ -677,7 +677,7 @@ fn test_precedence_of_block_size_arg_over_env() {
677677
.args(&["-B", "999", "--output=size"])
678678
.env("DF_BLOCK_SIZE", "111")
679679
.succeeds()
680-
.stdout_move_str();
680+
.stdout_str_lossy();
681681
let header = output.lines().next().unwrap().trim().to_string();
682682

683683
assert_eq!(header, "999B-blocks");
@@ -691,7 +691,7 @@ fn test_invalid_block_size_from_env() {
691691
.arg("--output=size")
692692
.env("DF_BLOCK_SIZE", "invalid")
693693
.succeeds()
694-
.stdout_move_str();
694+
.stdout_str_lossy();
695695
let header = output.lines().next().unwrap().trim().to_string();
696696

697697
assert_eq!(header, default_block_size_header);
@@ -701,7 +701,7 @@ fn test_invalid_block_size_from_env() {
701701
.env("DF_BLOCK_SIZE", "invalid")
702702
.env("BLOCK_SIZE", "222")
703703
.succeeds()
704-
.stdout_move_str();
704+
.stdout_str_lossy();
705705
let header = output.lines().next().unwrap().trim().to_string();
706706

707707
assert_eq!(header, default_block_size_header);
@@ -717,7 +717,7 @@ fn test_ignore_block_size_from_env_in_posix_portability_mode() {
717717
.env("BLOCK_SIZE", "222")
718718
.env("BLOCKSIZE", "333")
719719
.succeeds()
720-
.stdout_move_str();
720+
.stdout_str_lossy();
721721
let header = output
722722
.lines()
723723
.next()
@@ -784,13 +784,13 @@ fn test_output_selects_columns() {
784784
let output = new_ucmd!()
785785
.args(&["--output=source"])
786786
.succeeds()
787-
.stdout_move_str();
787+
.stdout_str_lossy();
788788
assert_eq!(output.lines().next().unwrap(), "Filesystem");
789789

790790
let output = new_ucmd!()
791791
.args(&["--output=source,target"])
792792
.succeeds()
793-
.stdout_move_str();
793+
.stdout_str_lossy();
794794
assert_eq!(
795795
output
796796
.lines()
@@ -804,7 +804,7 @@ fn test_output_selects_columns() {
804804
let output = new_ucmd!()
805805
.args(&["--output=source,target,used"])
806806
.succeeds()
807-
.stdout_move_str();
807+
.stdout_str_lossy();
808808
assert_eq!(
809809
output
810810
.lines()
@@ -821,7 +821,7 @@ fn test_output_multiple_occurrences() {
821821
let output = new_ucmd!()
822822
.args(&["--output=source", "--output=target"])
823823
.succeeds()
824-
.stdout_move_str();
824+
.stdout_str_lossy();
825825
assert_eq!(
826826
output
827827
.lines()
@@ -840,7 +840,7 @@ fn test_output_file_all_filesystems() {
840840
let output = new_ucmd!()
841841
.arg("--output=file")
842842
.succeeds()
843-
.stdout_move_str();
843+
.stdout_str_lossy();
844844
let mut lines = output.lines();
845845
assert_eq!(lines.next().unwrap(), "File");
846846
for line in lines {
@@ -862,7 +862,7 @@ fn test_output_file_specific_files() {
862862
let output = ucmd
863863
.args(&["--output=file", "a", "b", "c"])
864864
.succeeds()
865-
.stdout_move_str();
865+
.stdout_str_lossy();
866866
let actual: Vec<&str> = output.lines().collect();
867867
assert_eq!(actual, vec!["File", "a", "b", "c"]);
868868
}
@@ -876,7 +876,7 @@ fn test_file_column_width_if_filename_contains_unicode_chars() {
876876
let output = ucmd
877877
.args(&["--output=file,target", "äöü.txt"])
878878
.succeeds()
879-
.stdout_move_str();
879+
.stdout_str_lossy();
880880
let actual = output.lines().next().unwrap();
881881
// expected width: 7 chars (length of äöü.txt) + 1 char (column separator)
882882
assert_eq!(actual, "File Mounted on");

tests/uutests/src/lib/util.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ impl CmdResult {
357357
std::str::from_utf8(&self.stdout).unwrap()
358358
}
359359

360+
/// Returns the program's standard output as a string, automatically handling invalid utf8
361+
pub fn stdout_str_lossy(self) -> String {
362+
String::from_utf8_lossy(&self.stdout).to_string()
363+
}
364+
360365
/// Returns the program's standard output as a string
361366
/// consumes self
362367
pub fn stdout_move_str(self) -> String {

0 commit comments

Comments
 (0)