From 63203a0a6874aed9d706df0a05f4cf845deb7e38 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 25 Sep 2022 03:33:36 +0200 Subject: [PATCH] test: add -N FILE exists and has been modified since it was last read Upstream: tests/misc/test-N.sh --- src/uu/test/src/parser.rs | 6 +++--- src/uu/test/src/test.rs | 5 +++++ tests/by-util/test_test.rs | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/uu/test/src/parser.rs b/src/uu/test/src/parser.rs index 5d27f8838..1177f49ea 100644 --- a/src/uu/test/src/parser.rs +++ b/src/uu/test/src/parser.rs @@ -55,8 +55,8 @@ impl Symbol { "-eq" | "-ge" | "-gt" | "-le" | "-lt" | "-ne" => Self::Op(Operator::Int(s)), "-ef" | "-nt" | "-ot" => Self::Op(Operator::File(s)), "-n" | "-z" => Self::UnaryOp(UnaryOperator::StrlenOp(s)), - "-b" | "-c" | "-d" | "-e" | "-f" | "-g" | "-G" | "-h" | "-k" | "-L" | "-O" - | "-p" | "-r" | "-s" | "-S" | "-t" | "-u" | "-w" | "-x" => { + "-b" | "-c" | "-d" | "-e" | "-f" | "-g" | "-G" | "-h" | "-k" | "-L" | "-N" + | "-O" | "-p" | "-r" | "-s" | "-S" | "-t" | "-u" | "-w" | "-x" => { Self::UnaryOp(UnaryOperator::FiletestOp(s)) } _ => Self::Literal(s), @@ -108,7 +108,7 @@ impl Symbol { /// INTOP → -eq | -ge | -gt | -le | -lt | -ne /// FILEOP → -ef | -nt | -ot /// STRLEN → -n | -z -/// FILETEST → -b | -c | -d | -e | -f | -g | -G | -h | -k | -L | -O | -p | +/// FILETEST → -b | -c | -d | -e | -f | -g | -G | -h | -k | -L | -N | -O | -p | /// -r | -s | -S | -t | -u | -w | -x /// BOOLOP → -a | -o /// diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index e53ba4db1..e16ac85ea 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -205,6 +205,7 @@ fn eval(stack: &mut Vec) -> Result { "-h" => path(&f, &PathCondition::SymLink), "-k" => path(&f, &PathCondition::Sticky), "-L" => path(&f, &PathCondition::SymLink), + "-N" => path(&f, &PathCondition::ExistsModifiedLastRead), "-O" => path(&f, &PathCondition::UserOwns), "-p" => path(&f, &PathCondition::Fifo), "-r" => path(&f, &PathCondition::Readable), @@ -273,6 +274,7 @@ enum PathCondition { CharacterSpecial, Directory, Exists, + ExistsModifiedLastRead, Regular, GroupIdFlag, GroupOwns, @@ -351,6 +353,9 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool { PathCondition::CharacterSpecial => file_type.is_char_device(), PathCondition::Directory => file_type.is_dir(), PathCondition::Exists => true, + PathCondition::ExistsModifiedLastRead => { + metadata.accessed().unwrap() < metadata.modified().unwrap() + } PathCondition::Regular => file_type.is_file(), PathCondition::GroupIdFlag => metadata.mode() & S_ISGID != 0, PathCondition::GroupOwns => metadata.gid() == getegid(), diff --git a/tests/by-util/test_test.rs b/tests/by-util/test_test.rs index 3d74dfa90..8ee4f8732 100644 --- a/tests/by-util/test_test.rs +++ b/tests/by-util/test_test.rs @@ -882,3 +882,16 @@ fn test_bracket_syntax_version() { .succeeds() .stdout_matches(&r"\[ \d+\.\d+\.\d+".parse().unwrap()); } + +#[test] +#[allow(non_snake_case)] +fn test_file_N() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + scene.ucmd().args(&["-N", "regular_file"]).fails(); + // The file will have different create/modified data + // so, test -N will return 0 + sleep(std::time::Duration::from_millis(1000)); + at.touch("regular_file"); + scene.ucmd().args(&["-N", "regular_file"]).succeeds(); +}