Skip to content

Commit edb88c8

Browse files
committed
mv: inter partition copying test code refactoring
1 parent 8685694 commit edb88c8

File tree

1 file changed

+102
-100
lines changed

1 file changed

+102
-100
lines changed

tests/by-util/test_mv.rs

Lines changed: 102 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,105 +1623,107 @@ fn test_acl() {
16231623
// mv: try to overwrite 'b', overriding mode 0444 (r--r--r--)? y
16241624
// 'a' -> 'b'
16251625

1626-
// Ensure that the copying code used in an inter-partition move unlinks the destination symlink.
1627-
#[cfg(target_os = "linux")]
1628-
#[test]
1629-
fn test_mv_unlinks_dest_symlink() {
1630-
let scene = TestScenario::new(util_name!());
1631-
let at = &scene.fixtures;
1632-
1633-
// create a file in the current partition.
1634-
at.write("src", "src contents");
1635-
1636-
// create a folder in another partition.
1637-
let other_fs_tempdir =
1638-
tempfile::TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
1639-
1640-
// create a file inside that folder.
1641-
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
1642-
let mut file =
1643-
std::fs::File::create(&other_fs_file_path).expect("Unable to create other_fs_file");
1644-
std::io::Write::write_all(&mut file, b"other fs file contents")
1645-
.expect("Unable to write to other_fs_file");
1646-
1647-
// create a symlink to the file inside the same directory.
1648-
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
1649-
std::os::unix::fs::symlink(&other_fs_file_path, &symlink_path)
1650-
.expect("Unable to create symlink_to_file");
1651-
1652-
// mv src to symlink in another partition
1653-
scene
1654-
.ucmd()
1655-
.arg("src")
1656-
.arg(symlink_path.to_str().unwrap())
1657-
.succeeds();
1658-
1659-
// make sure that src got removed.
1660-
assert!(!at.file_exists("src"));
1661-
1662-
// make sure symlink got unlinked
1663-
assert!(!symlink_path.is_symlink());
1664-
1665-
// make sure that file contents in other_fs_file didn't change.
1666-
let mut new_contents = String::new();
1667-
std::io::Read::read_to_string(
1668-
&mut std::fs::File::open(&other_fs_file_path).expect("Unable to open other_fs_file"),
1669-
&mut new_contents,
1670-
)
1671-
.expect("Unable to read other_fs_file");
1672-
assert_eq!(new_contents, "other fs file contents");
1673-
1674-
// make sure that src file contents got copied into new file created in symlink_path .
1675-
let mut new_contents = String::new();
1676-
std::io::Read::read_to_string(
1677-
&mut std::fs::File::open(&symlink_path).expect("Unable to open file"),
1678-
&mut new_contents,
1679-
)
1680-
.expect("Unable to read file");
1681-
assert_eq!(new_contents, "src contents");
1682-
}
1683-
1684-
// In an inter-partition move if unlinking the destination symlink fails, ensure
1685-
// that it would output the proper error message.
1686-
#[cfg(target_os = "linux")]
1687-
#[test]
1688-
fn test_mv_unlinks_dest_symlink_error_message() {
1689-
let scene = TestScenario::new(util_name!());
1690-
let at = &scene.fixtures;
1626+
mod inter_partition_copying {
1627+
use crate::common::util::TestScenario;
1628+
1629+
use std::fs::{set_permissions, File};
1630+
use std::io::{Read, Write};
1631+
use std::os::unix::fs::{symlink, PermissionsExt};
1632+
use tempfile::TempDir;
1633+
1634+
// Ensure that the copying code used in an inter-partition move unlinks the destination symlink.
1635+
#[cfg(target_os = "linux")]
1636+
#[test]
1637+
pub(crate) fn test_mv_unlinks_dest_symlink() {
1638+
let scene = TestScenario::new(util_name!());
1639+
let at = &scene.fixtures;
1640+
1641+
// create a file in the current partition.
1642+
at.write("src", "src contents");
1643+
1644+
// create a folder in another partition.
1645+
let other_fs_tempdir =
1646+
TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
1647+
1648+
// create a file inside that folder.
1649+
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
1650+
let mut file = File::create(&other_fs_file_path).expect("Unable to create other_fs_file");
1651+
Write::write_all(&mut file, b"other fs file contents")
1652+
.expect("Unable to write to other_fs_file");
1653+
1654+
// create a symlink to the file inside the same directory.
1655+
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
1656+
symlink(&other_fs_file_path, &symlink_path).expect("Unable to create symlink_to_file");
1657+
1658+
// mv src to symlink in another partition
1659+
scene
1660+
.ucmd()
1661+
.arg("src")
1662+
.arg(symlink_path.to_str().unwrap())
1663+
.succeeds();
1664+
1665+
// make sure that src got removed.
1666+
assert!(!at.file_exists("src"));
1667+
1668+
// make sure symlink got unlinked
1669+
assert!(!symlink_path.is_symlink());
1670+
1671+
// make sure that file contents in other_fs_file didn't change.
1672+
let mut new_contents = String::new();
1673+
Read::read_to_string(
1674+
&mut File::open(&other_fs_file_path).expect("Unable to open other_fs_file"),
1675+
&mut new_contents,
1676+
)
1677+
.expect("Unable to read other_fs_file");
1678+
assert_eq!(new_contents, "other fs file contents");
1679+
1680+
// make sure that src file contents got copied into new file created in symlink_path .
1681+
let mut new_contents = String::new();
1682+
Read::read_to_string(
1683+
&mut File::open(&symlink_path).expect("Unable to open file"),
1684+
&mut new_contents,
1685+
)
1686+
.expect("Unable to read file");
1687+
assert_eq!(new_contents, "src contents");
1688+
}
16911689

1692-
// create a file in the current partition.
1693-
at.write("src", "src contents");
1694-
1695-
// create a folder in another partition.
1696-
let other_fs_tempdir =
1697-
tempfile::TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
1698-
1699-
// create a file inside that folder.
1700-
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
1701-
let mut file =
1702-
std::fs::File::create(&other_fs_file_path).expect("Unable to create other_fs_file");
1703-
std::io::Write::write_all(&mut file, b"other fs file contents")
1704-
.expect("Unable to write to other_fs_file");
1705-
1706-
// create a symlink to the file inside the same directory.
1707-
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
1708-
std::os::unix::fs::symlink(&other_fs_file_path, &symlink_path)
1709-
.expect("Unable to create symlink_to_file");
1710-
1711-
// disable write for the target folder so that when mv tries to remove the
1712-
// the destination symlink inside the target directory it would fail.
1713-
std::fs::set_permissions(
1714-
other_fs_tempdir.path(),
1715-
std::os::unix::fs::PermissionsExt::from_mode(0o555),
1716-
)
1717-
.expect("Unable to set permissions for temp directory");
1718-
1719-
// mv src to symlink in another partition
1720-
scene
1721-
.ucmd()
1722-
.arg("src")
1723-
.arg(symlink_path.to_str().unwrap())
1724-
.fails()
1725-
.stderr_contains("inter-device move failed:")
1726-
.stderr_contains("Permission denied");
1690+
// In an inter-partition move if unlinking the destination symlink fails, ensure
1691+
// that it would output the proper error message.
1692+
#[cfg(target_os = "linux")]
1693+
#[test]
1694+
pub(crate) fn test_mv_unlinks_dest_symlink_error_message() {
1695+
let scene = TestScenario::new(util_name!());
1696+
let at = &scene.fixtures;
1697+
1698+
// create a file in the current partition.
1699+
at.write("src", "src contents");
1700+
1701+
// create a folder in another partition.
1702+
let other_fs_tempdir =
1703+
TempDir::new_in("/dev/shm/").expect("Unable to create temp directory");
1704+
1705+
// create a file inside that folder.
1706+
let other_fs_file_path = other_fs_tempdir.path().join("other_fs_file");
1707+
let mut file = File::create(&other_fs_file_path).expect("Unable to create other_fs_file");
1708+
Write::write_all(&mut file, b"other fs file contents")
1709+
.expect("Unable to write to other_fs_file");
1710+
1711+
// create a symlink to the file inside the same directory.
1712+
let symlink_path = other_fs_tempdir.path().join("symlink_to_file");
1713+
symlink(&other_fs_file_path, &symlink_path).expect("Unable to create symlink_to_file");
1714+
1715+
// disable write for the target folder so that when mv tries to remove the
1716+
// the destination symlink inside the target directory it would fail.
1717+
set_permissions(other_fs_tempdir.path(), PermissionsExt::from_mode(0o555))
1718+
.expect("Unable to set permissions for temp directory");
1719+
1720+
// mv src to symlink in another partition
1721+
scene
1722+
.ucmd()
1723+
.arg("src")
1724+
.arg(symlink_path.to_str().unwrap())
1725+
.fails()
1726+
.stderr_contains("inter-device move failed:")
1727+
.stderr_contains("Permission denied");
1728+
}
17271729
}

0 commit comments

Comments
 (0)