Skip to content

Commit 1885d6c

Browse files
committed
Add unit test for make_fifo
1 parent 3e2aa4a commit 1885d6c

File tree

1 file changed

+25
-0
lines changed
  • src/uucore/src/lib/features

1 file changed

+25
-0
lines changed

src/uucore/src/lib/features/fs.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ mod tests {
840840
#[cfg(unix)]
841841
use std::os::unix;
842842
#[cfg(unix)]
843+
use std::os::unix::fs::FileTypeExt;
844+
#[cfg(unix)]
843845
use tempfile::{NamedTempFile, tempdir};
844846

845847
struct NormalizePathTestCase<'a> {
@@ -1072,4 +1074,27 @@ mod tests {
10721074
let file_path = PathBuf::from("~/foo.txt");
10731075
assert!(matches!(get_filename(&file_path), Some("foo.txt")));
10741076
}
1077+
1078+
#[cfg(unix)]
1079+
#[test]
1080+
fn test_make_fifo() {
1081+
// Create the FIFO in a temporary directory.
1082+
let tempdir = tempdir().unwrap();
1083+
let path = tempdir.path().join("f");
1084+
assert!(make_fifo(&path).is_ok());
1085+
1086+
// Check that it is indeed a FIFO.
1087+
assert!(std::fs::metadata(&path).unwrap().file_type().is_fifo());
1088+
1089+
// Check that we can write to it and read from it.
1090+
//
1091+
// Write and read need to happen in different threads,
1092+
// otherwise `write` would block indefinitely while waiting
1093+
// for the `read`.
1094+
let path2 = path.clone();
1095+
std::thread::spawn(move || {
1096+
assert!(std::fs::write(&path2, b"foo").is_ok())
1097+
});
1098+
assert_eq!(std::fs::read(&path).unwrap(), b"foo");
1099+
}
10751100
}

0 commit comments

Comments
 (0)