mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
mv: use read_to_string and write when testing inter-partition copying (#6583)
* mv: inter partition copying test code cleanup * mv: inter partition copying use of read_to_string and write * Update tests/by-util/test_mv.rs Co-authored-by: Ben Wiederhake <BenWiederhake.GitHub@gmx.de> --------- Co-authored-by: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
This commit is contained in:
parent
868569496e
commit
5baf382f7e
1 changed files with 82 additions and 91 deletions
|
@ -1623,105 +1623,96 @@ fn test_acl() {
|
||||||
// mv: try to overwrite 'b', overriding mode 0444 (r--r--r--)? y
|
// mv: try to overwrite 'b', overriding mode 0444 (r--r--r--)? y
|
||||||
// 'a' -> 'b'
|
// 'a' -> 'b'
|
||||||
|
|
||||||
// Ensure that the copying code used in an inter-partition move unlinks the destination symlink.
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
#[test]
|
mod inter_partition_copying {
|
||||||
fn test_mv_unlinks_dest_symlink() {
|
use crate::common::util::TestScenario;
|
||||||
let scene = TestScenario::new(util_name!());
|
use std::fs::{read_to_string, set_permissions, write};
|
||||||
let at = &scene.fixtures;
|
use std::os::unix::fs::{symlink, PermissionsExt};
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
// create a file in the current partition.
|
// Ensure that the copying code used in an inter-partition move unlinks the destination symlink.
|
||||||
at.write("src", "src contents");
|
#[test]
|
||||||
|
pub(crate) fn test_mv_unlinks_dest_symlink() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
|
let at = &scene.fixtures;
|
||||||
|
|
||||||
// create a folder in another partition.
|
// create a file in the current partition.
|
||||||
let other_fs_tempdir =
|
at.write("src", "src contents");
|
||||||
tempfile::TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
|
|
||||||
|
|
||||||
// create a file inside that folder.
|
// create a folder in another partition.
|
||||||
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
|
let other_fs_tempdir =
|
||||||
let mut file =
|
TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
|
||||||
std::fs::File::create(&other_fs_file_path).expect("Unable to create other_fs_file");
|
|
||||||
std::io::Write::write_all(&mut file, b"other fs file contents")
|
|
||||||
.expect("Unable to write to other_fs_file");
|
|
||||||
|
|
||||||
// create a symlink to the file inside the same directory.
|
// create a file inside that folder.
|
||||||
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
|
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
|
||||||
std::os::unix::fs::symlink(&other_fs_file_path, &symlink_path)
|
write(&other_fs_file_path, "other fs file contents")
|
||||||
.expect("Unable to create symlink_to_file");
|
.expect("Unable to write to other_fs_file");
|
||||||
|
|
||||||
// mv src to symlink in another partition
|
// create a symlink to the file inside the same directory.
|
||||||
scene
|
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
|
||||||
.ucmd()
|
symlink(&other_fs_file_path, &symlink_path).expect("Unable to create symlink_to_file");
|
||||||
.arg("src")
|
|
||||||
.arg(symlink_path.to_str().unwrap())
|
|
||||||
.succeeds();
|
|
||||||
|
|
||||||
// make sure that src got removed.
|
// mv src to symlink in another partition
|
||||||
assert!(!at.file_exists("src"));
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.arg("src")
|
||||||
|
.arg(symlink_path.to_str().unwrap())
|
||||||
|
.succeeds();
|
||||||
|
|
||||||
// make sure symlink got unlinked
|
// make sure that src got removed.
|
||||||
assert!(!symlink_path.is_symlink());
|
assert!(!at.file_exists("src"));
|
||||||
|
|
||||||
// make sure that file contents in other_fs_file didn't change.
|
// make sure symlink got unlinked
|
||||||
let mut new_contents = String::new();
|
assert!(!symlink_path.is_symlink());
|
||||||
std::io::Read::read_to_string(
|
|
||||||
&mut std::fs::File::open(&other_fs_file_path).expect("Unable to open other_fs_file"),
|
|
||||||
&mut new_contents,
|
|
||||||
)
|
|
||||||
.expect("Unable to read other_fs_file");
|
|
||||||
assert_eq!(new_contents, "other fs file contents");
|
|
||||||
|
|
||||||
// make sure that src file contents got copied into new file created in symlink_path .
|
// make sure that file contents in other_fs_file didn't change.
|
||||||
let mut new_contents = String::new();
|
assert_eq!(
|
||||||
std::io::Read::read_to_string(
|
read_to_string(&other_fs_file_path,).expect("Unable to read other_fs_file"),
|
||||||
&mut std::fs::File::open(&symlink_path).expect("Unable to open file"),
|
"other fs file contents"
|
||||||
&mut new_contents,
|
);
|
||||||
)
|
|
||||||
.expect("Unable to read file");
|
// make sure that src file contents got copied into new file created in symlink_path
|
||||||
assert_eq!(new_contents, "src contents");
|
assert_eq!(
|
||||||
}
|
read_to_string(&symlink_path).expect("Unable to read other_fs_file"),
|
||||||
|
"src contents"
|
||||||
// In an inter-partition move if unlinking the destination symlink fails, ensure
|
);
|
||||||
// that it would output the proper error message.
|
}
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
#[test]
|
// In an inter-partition move if unlinking the destination symlink fails, ensure
|
||||||
fn test_mv_unlinks_dest_symlink_error_message() {
|
// that it would output the proper error message.
|
||||||
let scene = TestScenario::new(util_name!());
|
#[test]
|
||||||
let at = &scene.fixtures;
|
pub(crate) fn test_mv_unlinks_dest_symlink_error_message() {
|
||||||
|
let scene = TestScenario::new(util_name!());
|
||||||
// create a file in the current partition.
|
let at = &scene.fixtures;
|
||||||
at.write("src", "src contents");
|
|
||||||
|
// create a file in the current partition.
|
||||||
// create a folder in another partition.
|
at.write("src", "src contents");
|
||||||
let other_fs_tempdir =
|
|
||||||
tempfile::TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
|
// create a folder in another partition.
|
||||||
|
let other_fs_tempdir =
|
||||||
// create a file inside that folder.
|
TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
|
||||||
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
|
|
||||||
let mut file =
|
// create a file inside that folder.
|
||||||
std::fs::File::create(&other_fs_file_path).expect("Unable to create other_fs_file");
|
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
|
||||||
std::io::Write::write_all(&mut file, b"other fs file contents")
|
write(&other_fs_file_path, "other fs file contents")
|
||||||
.expect("Unable to write to other_fs_file");
|
.expect("Unable to write to other_fs_file");
|
||||||
|
|
||||||
// create a symlink to the file inside the same directory.
|
// create a symlink to the file inside the same directory.
|
||||||
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
|
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
|
||||||
std::os::unix::fs::symlink(&other_fs_file_path, &symlink_path)
|
symlink(&other_fs_file_path, &symlink_path).expect("Unable to create symlink_to_file");
|
||||||
.expect("Unable to create symlink_to_file");
|
|
||||||
|
// disable write for the target folder so that when mv tries to remove the
|
||||||
// disable write for the target folder so that when mv tries to remove the
|
// the destination symlink inside the target directory it would fail.
|
||||||
// the destination symlink inside the target directory it would fail.
|
set_permissions(other_fs_tempdir.path(), PermissionsExt::from_mode(0o555))
|
||||||
std::fs::set_permissions(
|
.expect("Unable to set permissions for temp directory");
|
||||||
other_fs_tempdir.path(),
|
|
||||||
std::os::unix::fs::PermissionsExt::from_mode(0o555),
|
// mv src to symlink in another partition
|
||||||
)
|
scene
|
||||||
.expect("Unable to set permissions for temp directory");
|
.ucmd()
|
||||||
|
.arg("src")
|
||||||
// mv src to symlink in another partition
|
.arg(symlink_path.to_str().unwrap())
|
||||||
scene
|
.fails()
|
||||||
.ucmd()
|
.stderr_contains("inter-device move failed:")
|
||||||
.arg("src")
|
.stderr_contains("Permission denied");
|
||||||
.arg(symlink_path.to_str().unwrap())
|
}
|
||||||
.fails()
|
|
||||||
.stderr_contains("inter-device move failed:")
|
|
||||||
.stderr_contains("Permission denied");
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue