From a9a3794d5a366f185d65e8c4908b4db180cdec20 Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Sun, 28 Mar 2021 20:56:37 +0200 Subject: [PATCH] chmod: add tests --- src/uu/chmod/src/chmod.rs | 2 +- tests/by-util/test_chmod.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/uu/chmod/src/chmod.rs b/src/uu/chmod/src/chmod.rs index 119447b14..819c674a0 100644 --- a/src/uu/chmod/src/chmod.rs +++ b/src/uu/chmod/src/chmod.rs @@ -170,7 +170,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { // Iterate 'args' and delete the first occurrence // of a prefix '-' if it's associated with MODE // e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE" -fn strip_minus_from_mode(args: &mut Vec) -> bool { +pub fn strip_minus_from_mode(args: &mut Vec) -> bool { for i in 0..args.len() { if args[i].starts_with("-") { if let Some(second) = args[i].chars().nth(1) { diff --git a/tests/by-util/test_chmod.rs b/tests/by-util/test_chmod.rs index e7bc72677..3a53202fc 100644 --- a/tests/by-util/test_chmod.rs +++ b/tests/by-util/test_chmod.rs @@ -4,6 +4,8 @@ use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; use std::sync::Mutex; extern crate libc; +use self::chmod::strip_minus_from_mode; +extern crate chmod; use self::libc::umask; static TEST_FILE: &'static str = "file"; @@ -374,3 +376,32 @@ fn test_chmod_symlink_non_existing_recursive() { .stderr .contains("neither symbolic link 'tmp/test-long.link' nor referent has been changed")); } + +#[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 RFILE -R FILE", + "chmod -v --reference RFILE -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 = 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(" ")); + } +}