1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

Tests: provides easy mount of temp fs (#7249)

This commit is contained in:
sreehari prasad 2025-02-01 23:31:49 +05:30 committed by GitHub
parent 3891ee1159
commit c2505841e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 82 additions and 26 deletions

View file

@ -232,7 +232,7 @@ fn test_manpage() {
let test_scenario = TestScenario::new("");
let child = Command::new(test_scenario.bin_path)
let child = Command::new(&test_scenario.bin_path)
.arg("manpage")
.arg("base64")
.stdin(Stdio::piped())

View file

@ -2288,7 +2288,7 @@ fn test_cp_one_file_system() {
use crate::common::util::AtPath;
use walkdir::WalkDir;
let scene = TestScenario::new(util_name!());
let mut scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
// Test must be run as root (or with `sudo -E`)
@ -2304,14 +2304,8 @@ fn test_cp_one_file_system() {
let mountpoint_path = &at_src.plus_as_string(TEST_MOUNT_MOUNTPOINT);
scene
.cmd("mount")
.arg("-t")
.arg("tmpfs")
.arg("-o")
.arg("size=640k") // ought to be enough
.arg("tmpfs")
.arg(mountpoint_path)
.succeeds();
.mount_temp_fs(mountpoint_path)
.expect("mounting tmpfs failed");
at_src.touch(TEST_MOUNT_OTHER_FILESYSTEM_FILE);
@ -2324,7 +2318,7 @@ fn test_cp_one_file_system() {
.succeeds();
// Ditch the mount before the asserts
scene.cmd("umount").arg(mountpoint_path).succeeds();
scene.umount_temp_fs();
assert!(!at_dst.file_exists(TEST_MOUNT_OTHER_FILESYSTEM_FILE));
// Check if the other files were copied from the source folder hierarchy

View file

@ -1683,7 +1683,7 @@ fn test_reading_partial_blocks_from_fifo() {
fn test_reading_partial_blocks_from_fifo_unbuffered() {
// Create the FIFO.
let ts = TestScenario::new(util_name!());
let at = ts.fixtures;
let at = &ts.fixtures;
at.mkfifo("fifo");
let fifoname = at.plus_as_string("fifo");

View file

@ -655,7 +655,7 @@ fn test_du_time() {
#[cfg(feature = "touch")]
fn birth_supported() -> bool {
let ts = TestScenario::new(util_name!());
let m = match std::fs::metadata(ts.fixtures.subdir) {
let m = match std::fs::metadata(&ts.fixtures.subdir) {
Ok(m) => m,
Err(e) => panic!("{}", e),
};

View file

@ -137,7 +137,7 @@ fn test_debug_2() {
let result = ts
.ucmd()
.arg("-vv")
.arg(ts.bin_path)
.arg(&ts.bin_path)
.args(&["echo", "hello2"])
.succeeds();
result.stderr_matches(
@ -165,7 +165,7 @@ fn test_debug1_part_of_string_arg() {
let result = ts
.ucmd()
.arg("-vS FOO=BAR")
.arg(ts.bin_path)
.arg(&ts.bin_path)
.args(&["echo", "hello1"])
.succeeds();
result.stderr_matches(
@ -186,7 +186,7 @@ fn test_debug2_part_of_string_arg() {
let result = ts
.ucmd()
.arg("-vvS FOO=BAR")
.arg(ts.bin_path)
.arg(&ts.bin_path)
.args(&["echo", "hello2"])
.succeeds();
result.stderr_matches(

View file

@ -31,7 +31,7 @@ fn test_deleted_dir() {
use std::process::Command;
let ts = TestScenario::new(util_name!());
let at = ts.fixtures;
let at = &ts.fixtures;
let output = Command::new("sh")
.arg("-c")
.arg(format!(

View file

@ -4,7 +4,7 @@
// file that was distributed with this source code.
//spell-checker: ignore (linux) rlimit prlimit coreutil ggroups uchild uncaptured scmd SHLVL canonicalized openpty
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus tmpfs
#![allow(dead_code)]
#![allow(
@ -1169,6 +1169,8 @@ pub struct TestScenario {
pub util_name: String,
pub fixtures: AtPath,
tmpd: Rc<TempDir>,
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
tmp_fs_mountpoint: Option<String>,
}
impl TestScenario {
@ -1182,6 +1184,8 @@ impl TestScenario {
util_name: util_name.as_ref().into(),
fixtures: AtPath::new(tmpd.as_ref().path()),
tmpd,
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
tmp_fs_mountpoint: None,
};
let mut fixture_path_builder = env::current_dir().unwrap();
fixture_path_builder.push(TESTS_DIR);
@ -1215,6 +1219,44 @@ impl TestScenario {
pub fn ccmd<S: AsRef<str>>(&self, util_name: S) -> UCommand {
UCommand::with_util(util_name, self.tmpd.clone())
}
/// Mounts a temporary filesystem at the specified mount point.
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
pub fn mount_temp_fs(&mut self, mount_point: &str) -> core::result::Result<(), String> {
if self.tmp_fs_mountpoint.is_some() {
return Err("already mounted".to_string());
}
let cmd_result = self
.cmd("mount")
.arg("-t")
.arg("tmpfs")
.arg("-o")
.arg("size=640k") // ought to be enough
.arg("tmpfs")
.arg(mount_point)
.run();
if !cmd_result.succeeded() {
return Err(format!("mount failed: {}", cmd_result.stderr_str()));
}
self.tmp_fs_mountpoint = Some(mount_point.to_string());
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
/// Unmounts the temporary filesystem if it is currently mounted.
pub fn umount_temp_fs(&mut self) {
if let Some(mount_point) = self.tmp_fs_mountpoint.as_ref() {
self.cmd("umount").arg(mount_point).succeeds();
self.tmp_fs_mountpoint = None;
}
}
}
impl Drop for TestScenario {
fn drop(&mut self) {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
self.umount_temp_fs();
}
}
#[cfg(unix)]
@ -4007,4 +4049,24 @@ mod tests {
.stdout_is(expected);
std::assert_eq!(p_umask, get_umask()); // make sure parent umask didn't change
}
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
#[test]
fn test_mount_temp_fs() {
let mut scene = TestScenario::new("util");
let at = &scene.fixtures;
// Test must be run as root (or with `sudo -E`)
if scene.cmd("whoami").run().stdout_str() != "root\n" {
return;
}
at.mkdir("mountpoint");
let mountpoint = at.plus("mountpoint");
scene.mount_temp_fs(mountpoint.to_str().unwrap()).unwrap();
scene
.cmd("df")
.arg("-h")
.arg(mountpoint)
.succeeds()
.stdout_contains("tmpfs");
}
}

View file

@ -35,7 +35,7 @@ fn execution_phrase_single() {
use std::process::Command;
let scenario = TestScenario::new("ls");
symlink_file(scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
let output = Command::new(scenario.fixtures.plus("uu-ls"))
.arg("--some-invalid-arg")
.output()
@ -56,7 +56,7 @@ fn util_name_double() {
};
let scenario = TestScenario::new("sort");
let mut child = Command::new(scenario.bin_path)
let mut child = Command::new(&scenario.bin_path)
.arg("sort")
.stdin(Stdio::piped())
.stderr(Stdio::piped())
@ -78,7 +78,7 @@ fn util_name_single() {
};
let scenario = TestScenario::new("sort");
symlink_file(scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
let mut child = Command::new(scenario.fixtures.plus("uu-sort"))
.stdin(Stdio::piped())
.stderr(Stdio::piped())
@ -102,7 +102,7 @@ fn util_invalid_name_help() {
};
let scenario = TestScenario::new("invalid_name");
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
let child = Command::new(scenario.fixtures.plus("invalid_name"))
.arg("--help")
.stdin(Stdio::piped())
@ -138,7 +138,7 @@ fn util_non_utf8_name_help() {
let scenario = TestScenario::new("invalid_name");
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff"));
symlink_file(scenario.bin_path, &non_utf8_path).unwrap();
symlink_file(&scenario.bin_path, &non_utf8_path).unwrap();
let child = Command::new(&non_utf8_path)
.arg("--help")
.stdin(Stdio::piped())
@ -166,7 +166,7 @@ fn util_invalid_name_invalid_command() {
};
let scenario = TestScenario::new("invalid_name");
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
let child = Command::new(scenario.fixtures.plus("invalid_name"))
.arg("definitely_invalid")
.stdin(Stdio::piped())
@ -192,7 +192,7 @@ fn util_completion() {
};
let scenario = TestScenario::new("completion");
let child = Command::new(scenario.bin_path)
let child = Command::new(&scenario.bin_path)
.arg("completion")
.arg("true")
.arg("powershell")
@ -220,7 +220,7 @@ fn util_manpage() {
};
let scenario = TestScenario::new("completion");
let child = Command::new(scenario.bin_path)
let child = Command::new(&scenario.bin_path)
.arg("manpage")
.arg("true")
.stdin(Stdio::piped())