Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,9 @@ fn path_is_current_or_parent_directory(path: &Path) -> bool {
let dir_separator = MAIN_SEPARATOR as u8;
if let Ok(path_bytes) = path_str {
return path_bytes == ([b'.'])
|| path_bytes == ([b'.', dir_separator])
|| path_bytes == ([b'.', b'.'])
|| path_bytes == ([b'.', b'.', dir_separator])
|| path_bytes.ends_with(&[dir_separator, b'.'])
|| path_bytes.ends_with(&[dir_separator, b'.', b'.'])
|| path_bytes.ends_with(&[dir_separator, b'.', dir_separator])
Expand Down
7 changes: 7 additions & 0 deletions src/uucore/src/lib/features/backup_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ mod tests {
let result = determine_backup_mode(&matches).unwrap();

assert_eq!(result, BackupMode::Numbered);
// cleaning up because this can affect tests that rely on env state
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
}

Expand All @@ -615,6 +616,7 @@ mod tests {
let result = determine_backup_mode(&matches).unwrap();

assert_eq!(result, BackupMode::None);
// cleaning up because this can affect tests that rely on env state
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
}

Expand All @@ -630,6 +632,7 @@ mod tests {
assert!(result.is_err());
let text = format!("{}", result.unwrap_err());
assert!(text.contains("invalid argument 'foobar' for '$VERSION_CONTROL'"));
// cleaning up because this can affect tests that rely on env state
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
}

Expand All @@ -645,6 +648,7 @@ mod tests {
assert!(result.is_err());
let text = format!("{}", result.unwrap_err());
assert!(text.contains("ambiguous argument 'n' for '$VERSION_CONTROL'"));
// cleaning up because this can affect tests that rely on env state
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
}

Expand All @@ -658,6 +662,7 @@ mod tests {
let result = determine_backup_mode(&matches).unwrap();

assert_eq!(result, BackupMode::Simple);
// cleaning up because this can affect tests that rely on env state
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
}

Expand All @@ -682,6 +687,8 @@ mod tests {
let result = determine_backup_mode(&matches).unwrap();

assert_eq!(result, BackupMode::Numbered);
// cleaning up because this can affect tests that rely on env state
unsafe { env::remove_var(ENV_VERSION_CONTROL) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment to explain why you have to do that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment in the code? No other removals have it, add it to all of them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it isn't clear to me why this variable has to be removed here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it can affect other tests that are using env, but this is fragile though because if test fails this will not be cleaned up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add this information in the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

}

#[test]
Expand Down
37 changes: 37 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,22 @@ fn test_current_or_parent_dir_rm4() {

at.mkdir("d");

let file_1 = "file1";
let file_2 = "d/file2";

at.touch(file_1);
at.touch(file_2);

let answers = [
"rm: refusing to remove '.' or '..' directory: skipping 'd/.'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/./'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/./'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/..'",
"rm: refusing to remove '.' or '..' directory: skipping 'd/../'",
"rm: refusing to remove '.' or '..' directory: skipping '.'",
"rm: refusing to remove '.' or '..' directory: skipping './'",
"rm: refusing to remove '.' or '..' directory: skipping '../'",
"rm: refusing to remove '.' or '..' directory: skipping '..'",
];
let std_err_str = ts
.ucmd()
Expand All @@ -782,12 +792,20 @@ fn test_current_or_parent_dir_rm4() {
.arg("d/.////")
.arg("d/..")
.arg("d/../")
.arg(".")
.arg("./")
.arg("../")
.arg("..")
.fails()
.stderr_move_str();

for (idx, line) in std_err_str.lines().enumerate() {
assert_eq!(line, answers[idx]);
}
// checks that no file was silently removed
assert!(at.dir_exists("d"));
assert!(at.file_exists(file_1));
assert!(at.file_exists(file_2));
}

#[test]
Expand All @@ -798,12 +816,22 @@ fn test_current_or_parent_dir_rm4_windows() {

at.mkdir("d");

let file_1 = "file1";
let file_2 = "d/file2";

at.touch(file_1);
at.touch(file_2);

let answers = [
"rm: refusing to remove '.' or '..' directory: skipping 'd\\.'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\.\\'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\.\\'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\..'",
"rm: refusing to remove '.' or '..' directory: skipping 'd\\..\\'",
"rm: refusing to remove '.' or '..' directory: skipping '.'",
"rm: refusing to remove '.' or '..' directory: skipping '.\\'",
"rm: refusing to remove '.' or '..' directory: skipping '..'",
"rm: refusing to remove '.' or '..' directory: skipping '..\\'",
];
let std_err_str = ts
.ucmd()
Expand All @@ -813,12 +841,21 @@ fn test_current_or_parent_dir_rm4_windows() {
.arg("d\\.\\\\\\\\")
.arg("d\\..")
.arg("d\\..\\")
.arg(".")
.arg(".\\")
.arg("..")
.arg("..\\")
.fails()
.stderr_move_str();

for (idx, line) in std_err_str.lines().enumerate() {
assert_eq!(line, answers[idx]);
}

// checks that no file was silently removed
assert!(at.dir_exists("d"));
assert!(at.file_exists(file_1));
assert!(at.file_exists(file_2));
}

#[test]
Expand Down
Loading