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

cp: do not set the current directory in tests

Setting the current directory in tests affects other tests, even if the
change is reverted after, because tests are run in parallel.

This should fix the flaky cp tests.
This commit is contained in:
Michael Debertol 2021-08-12 22:42:40 +02:00
parent 3a6769f74b
commit ce1323ce1c

View file

@ -16,8 +16,6 @@ use std::os::windows::fs::symlink_file;
use filetime::FileTime; use filetime::FileTime;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use rlimit::Resource; use rlimit::Resource;
#[cfg(not(windows))]
use std::env;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
use std::fs as std_fs; use std::fs as std_fs;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@ -743,20 +741,16 @@ fn test_cp_deref_folder_to_folder() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
let at = &scene.fixtures; let at = &scene.fixtures;
let cwd = env::current_dir().unwrap(); let path_to_new_symlink = at.plus(TEST_COPY_FROM_FOLDER);
let path_to_new_symlink = at.subdir.join(TEST_COPY_FROM_FOLDER); at.symlink_file(
&path_to_new_symlink
// Change the cwd to have a correct symlink .join(TEST_HELLO_WORLD_SOURCE)
assert!(env::set_current_dir(&path_to_new_symlink).is_ok()); .to_string_lossy(),
&path_to_new_symlink
#[cfg(not(windows))] .join(TEST_HELLO_WORLD_SOURCE_SYMLINK)
let _r = fs::symlink(TEST_HELLO_WORLD_SOURCE, TEST_HELLO_WORLD_SOURCE_SYMLINK); .to_string_lossy(),
#[cfg(windows)] );
let _r = symlink_file(TEST_HELLO_WORLD_SOURCE, TEST_HELLO_WORLD_SOURCE_SYMLINK);
// Back to the initial cwd (breaks the other tests)
assert!(env::set_current_dir(&cwd).is_ok());
//using -P -R option //using -P -R option
scene scene
@ -843,20 +837,16 @@ fn test_cp_no_deref_folder_to_folder() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
let at = &scene.fixtures; let at = &scene.fixtures;
let cwd = env::current_dir().unwrap(); let path_to_new_symlink = at.plus(TEST_COPY_FROM_FOLDER);
let path_to_new_symlink = at.subdir.join(TEST_COPY_FROM_FOLDER); at.symlink_file(
&path_to_new_symlink
// Change the cwd to have a correct symlink .join(TEST_HELLO_WORLD_SOURCE)
assert!(env::set_current_dir(&path_to_new_symlink).is_ok()); .to_string_lossy(),
&path_to_new_symlink
#[cfg(not(windows))] .join(TEST_HELLO_WORLD_SOURCE_SYMLINK)
let _r = fs::symlink(TEST_HELLO_WORLD_SOURCE, TEST_HELLO_WORLD_SOURCE_SYMLINK); .to_string_lossy(),
#[cfg(windows)] );
let _r = symlink_file(TEST_HELLO_WORLD_SOURCE, TEST_HELLO_WORLD_SOURCE_SYMLINK);
// Back to the initial cwd (breaks the other tests)
assert!(env::set_current_dir(&cwd).is_ok());
//using -P -R option //using -P -R option
scene scene
@ -969,10 +959,9 @@ fn test_cp_archive() {
} }
#[test] #[test]
#[cfg(target_os = "unix")] #[cfg(unix)]
fn test_cp_archive_recursive() { fn test_cp_archive_recursive() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let cwd = env::current_dir().unwrap();
// creates // creates
// dir/1 // dir/1
@ -988,26 +977,13 @@ fn test_cp_archive_recursive() {
at.touch(&file_1.to_string_lossy()); at.touch(&file_1.to_string_lossy());
at.touch(&file_2.to_string_lossy()); at.touch(&file_2.to_string_lossy());
// Change the cwd to have a correct symlink at.symlink_file("1", &file_1_link.to_string_lossy());
assert!(env::set_current_dir(&at.subdir.join(TEST_COPY_TO_FOLDER)).is_ok()); at.symlink_file("2", &file_2_link.to_string_lossy());
#[cfg(not(windows))]
{
let _r = fs::symlink("1", &file_1_link);
let _r = fs::symlink("2", &file_2_link);
}
#[cfg(windows)]
{
let _r = symlink_file("1", &file_1_link);
let _r = symlink_file("2", &file_2_link);
}
// Back to the initial cwd (breaks the other tests)
assert!(env::set_current_dir(&cwd).is_ok());
ucmd.arg("--archive") ucmd.arg("--archive")
.arg(TEST_COPY_TO_FOLDER) .arg(TEST_COPY_TO_FOLDER)
.arg(TEST_COPY_TO_FOLDER_NEW) .arg(TEST_COPY_TO_FOLDER_NEW)
.fails(); // fails for now .succeeds();
let scene2 = TestScenario::new("ls"); let scene2 = TestScenario::new("ls");
let result = scene2 let result = scene2
@ -1025,18 +1001,6 @@ fn test_cp_archive_recursive() {
.run(); .run();
println!("ls dest {}", result.stdout_str()); println!("ls dest {}", result.stdout_str());
assert!(at.file_exists(
&at.subdir
.join(TEST_COPY_TO_FOLDER_NEW)
.join("1.link")
.to_string_lossy()
));
assert!(at.file_exists(
&at.subdir
.join(TEST_COPY_TO_FOLDER_NEW)
.join("2.link")
.to_string_lossy()
));
assert!(at.file_exists( assert!(at.file_exists(
&at.subdir &at.subdir
.join(TEST_COPY_TO_FOLDER_NEW) .join(TEST_COPY_TO_FOLDER_NEW)