1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-15 19:36:16 +00:00

Merge pull request #4442 from bbara/chmod-usage

chmod: fix GNU test 'chmod/usage'
This commit is contained in:
Terts Diepraam 2023-03-17 15:18:39 +01:00 committed by GitHub
commit 2a4b48f253
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 170 additions and 42 deletions

View file

@ -4,9 +4,8 @@ use std::fs::{metadata, set_permissions, OpenOptions, Permissions};
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::sync::Mutex;
extern crate libc;
use uucore::mode::strip_minus_from_mode;
extern crate chmod;
extern crate libc;
use self::libc::umask;
static TEST_FILE: &str = "file";
@ -503,35 +502,6 @@ fn test_chmod_symlink_non_existing_file_recursive() {
.no_stderr();
}
#[test]
fn test_chmod_strip_minus_from_mode() {
let tests = vec![
// ( before, after )
("chmod -v -xw -R FILE", "chmod -v xw -R FILE"),
("chmod g=rwx FILE -c", "chmod g=rwx FILE -c"),
(
"chmod -c -R -w,o+w FILE --preserve-root",
"chmod -c -R w,o+w FILE --preserve-root",
),
("chmod -c -R +w FILE ", "chmod -c -R +w FILE "),
("chmod a=r,=xX FILE", "chmod a=r,=xX FILE"),
(
"chmod -v --reference REF_FILE -R FILE",
"chmod -v --reference REF_FILE -R FILE",
),
("chmod -Rvc -w-x FILE", "chmod -Rvc w-x FILE"),
("chmod 755 -v FILE", "chmod 755 -v FILE"),
("chmod -v +0004 FILE -R", "chmod -v +0004 FILE -R"),
("chmod -v -0007 FILE -R", "chmod -v 0007 FILE -R"),
];
for test in tests {
let mut args: Vec<String> = test.0.split(' ').map(|v| v.to_string()).collect();
let _mode_had_minus_prefix = strip_minus_from_mode(&mut args);
assert_eq!(test.1, args.join(" "));
}
}
#[test]
fn test_chmod_keep_setgid() {
for (from, arg, to) in [
@ -671,3 +641,68 @@ fn test_quiet_n_verbose_used_multiple_times() {
.arg("file")
.succeeds();
}
#[test]
fn test_gnu_invalid_mode() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("file");
scene.ucmd().arg("u+gr").arg("file").fails();
}
#[test]
fn test_gnu_options() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("file");
scene.ucmd().arg("-w").arg("file").succeeds();
scene.ucmd().arg("file").arg("-w").succeeds();
scene.ucmd().arg("-w").arg("--").arg("file").succeeds();
}
#[test]
fn test_gnu_repeating_options() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("file");
scene.ucmd().arg("-w").arg("-w").arg("file").succeeds();
scene
.ucmd()
.arg("-w")
.arg("-w")
.arg("-w")
.arg("file")
.succeeds();
}
#[test]
fn test_gnu_special_filenames() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let perms_before = Permissions::from_mode(0o100640);
let perms_after = Permissions::from_mode(0o100440);
make_file(&at.plus_as_string("--"), perms_before.mode());
scene.ucmd().arg("-w").arg("--").arg("--").succeeds();
assert_eq!(at.metadata("--").permissions(), perms_after);
set_permissions(at.plus("--"), perms_before.clone()).unwrap();
scene.ucmd().arg("--").arg("-w").arg("--").succeeds();
assert_eq!(at.metadata("--").permissions(), perms_after);
at.remove("--");
make_file(&at.plus_as_string("-w"), perms_before.mode());
scene.ucmd().arg("-w").arg("--").arg("-w").succeeds();
assert_eq!(at.metadata("-w").permissions(), perms_after);
set_permissions(at.plus("-w"), perms_before).unwrap();
scene.ucmd().arg("--").arg("-w").arg("-w").succeeds();
assert_eq!(at.metadata("-w").permissions(), perms_after);
}
#[test]
fn test_gnu_special_options() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("file");
scene.ucmd().arg("--").arg("--").arg("file").succeeds();
scene.ucmd().arg("--").arg("--").fails();
}