From 10151cd6cd185b9e3651e4dddc9b0cc9724ca19d Mon Sep 17 00:00:00 2001 From: Dimitrij Denissenko Date: Thu, 27 May 2021 10:31:26 +0100 Subject: [PATCH] Ignore dates in file names --- Gemfile.lock | 2 +- lib/net/ftp/list/unix.rb | 3 +- test/test_net_ftp_list_unix.rb | 69 +++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 25db38f..45b2bd0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,4 +56,4 @@ DEPENDENCIES timecop BUNDLED WITH - 2.2.14 + 2.2.17 diff --git a/lib/net/ftp/list/unix.rb b/lib/net/ftp/list/unix.rb index 1f6d7f2..aaa7fe5 100644 --- a/lib/net/ftp/list/unix.rb +++ b/lib/net/ftp/list/unix.rb @@ -14,8 +14,7 @@ class Net::FTP::List::Unix < Net::FTP::List::Parser ([pbcdlfmSs-]) (((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\+?\s+ (?:(\d+)\s+)? - (\S+)?\s+ - (?:(\S+(?:\s\S+)*)\s+)? + (\S+)?\s+(\S+)?\s+ (?:\d+,\s+)? (\d+)\s+ ((?:\d+[-/]\d+[-/]\d+)|(?:\S+\s+\S+))\s+ diff --git a/test/test_net_ftp_list_unix.rb b/test/test_net_ftp_list_unix.rb index e57e3c9..e5b722a 100644 --- a/test/test_net_ftp_list_unix.rb +++ b/test/test_net_ftp_list_unix.rb @@ -3,21 +3,26 @@ require 'timecop' class TestNetFTPListUnix < Test::Unit::TestCase + def parse(*args, **opts) + Net::FTP::List.parse(*args, **opts) + end + def setup - @dir = Net::FTP::List.parse 'drwxr-xr-x 4 user group 4096 Jan 1 00:00 etc' - @file = Net::FTP::List.parse '-rw-r--r-- 1 root other 531 Dec 31 23:59 README' - @other_dir = Net::FTP::List.parse 'drwxr-xr-x 8 1791 600 4096 Mar 11 07:57 forums' - @spaces = Net::FTP::List.parse 'drwxrwxr-x 2 danial danial 72 May 23 12:52 spaces suck' - @symlink = Net::FTP::List.parse 'lrwxrwxrwx 1 danial danial 4 Oct 30 15:26 bar -> /etc' - @empty_symlink = Net::FTP::List.parse 'lrwxrwxrwx 1 danial danial 4 Oct 30 15:26 foo' - @older_date = Net::FTP::List.parse '-rwxrwxrwx 1 owner group 154112 Feb 15 2008 participando.xls' - @block_dev = Net::FTP::List.parse 'brw-r----- 1 root disk 1, 0 Apr 13 2006 ram0', timezone: :local - @char_dev = Net::FTP::List.parse 'crw-rw-rw- 1 root root 1, 3 Apr 13 2006 null' - @socket_dev = Net::FTP::List.parse 'srw-rw-rw- 1 root root 0 Aug 20 14:15 log' - @pipe_dev = Net::FTP::List.parse 'prw-r----- 1 root adm 0 Nov 22 10:30 xconsole' - @file_no_inodes = Net::FTP::List.parse '-rw-r--r-- foo@utchost foo@utchost 6034 May 14 23:13 index.html' - @file_today = Net::FTP::List.parse 'crw-rw-rw- 1 root root 1, 3 Aug 16 14:28 today.txt' - @no_user = Net::FTP::List.parse '-rw-rw---- 2786 Jul 7 01:57 README' + @dir = parse('drwxr-xr-x 4 user group 4096 Jan 1 00:00 etc') + @file = parse('-rw-r--r-- 1 root other 531 Dec 31 23:59 README') + @other_dir = parse('drwxr-xr-x 8 1791 600 4096 Mar 11 07:57 forums') + @spaces = parse('drwxrwxr-x 2 danial danial 72 May 23 12:52 spaces suck') + @symlink = parse('lrwxrwxrwx 1 danial danial 4 Oct 30 15:26 bar -> /etc') + @empty_symlink = parse('lrwxrwxrwx 1 danial danial 4 Oct 30 15:26 foo') + @block_dev = parse('brw-r----- 1 root disk 1, 0 Apr 13 2006 ram0', timezone: :local) + @char_dev = parse('crw-rw-rw- 1 root root 1, 3 Apr 13 2006 null') + @socket_dev = parse('srw-rw-rw- 1 root root 0 Aug 20 14:15 log') + @pipe_dev = parse('prw-r----- 1 root adm 0 Nov 22 10:30 xconsole') + @file_no_inodes = parse('-rw-r--r-- foo@utchost foo@utchost 6034 May 14 23:13 index.html') + @file_today = parse('crw-rw-rw- 1 root root 1, 3 Aug 16 14:28 today.txt') + @no_user = parse('-rw-rw---- 2786 Jul 7 01:57 README') + @older_date = parse('-rwxrwxrwx 1 owner group 154112 Feb 15 2008 participando.xls') + @file_with_date = parse('-rw-r--r-- 1 tig tig 39757835 Jan 26 2014 PSA 23 99 May-2010.pdf') end def test_parse_new @@ -32,50 +37,72 @@ def test_parse_new assert_equal 'Unix', @socket_dev.server_type, 'LIST unix socket device' assert_equal 'Unix', @pipe_dev.server_type, 'LIST unix socket device' assert_equal 'Unix', @file_no_inodes.server_type, 'LIST unixish file with no inodes' + assert_equal 'Unix', @file_today.server_type, 'LIST unixish file' assert_equal 'Unix', @no_user.server_type, 'LIST unixish file with no user/group' + assert_equal 'Unix', @older_date.server_type, 'LIST unixish file with older date' + assert_equal 'Unix', @file_with_date.server_type, 'LIST unix with file containing a date' end # mtimes in the past, same year. def test_ruby_unix_like_date_past_same_year Timecop.freeze(Time.utc(2009, 1, 1)) do - assert_equal Time.utc(2009, 1, 1), Net::FTP::List.parse(@dir.raw).mtime + assert_equal Time.utc(2009, 1, 1), parse(@dir.raw).mtime end Timecop.freeze(Time.utc(2008, 4, 1)) do - assert_equal Time.utc(2008, 3, 11, 7, 57), Net::FTP::List.parse(@other_dir.raw).mtime + assert_equal Time.utc(2008, 3, 11, 7, 57), parse(@other_dir.raw).mtime end end # mtimes in the past, previous year def test_ruby_unix_like_date_past_previous_year Timecop.freeze(Time.utc(2008, 2, 4)) do - assert_equal Time.utc(2007, 10, 30, 15, 26), Net::FTP::List.parse(@symlink.raw).mtime + assert_equal Time.utc(2007, 10, 30, 15, 26), parse(@symlink.raw).mtime end end # mtime in the future. def test_ruby_unix_like_date_future Timecop.freeze(Time.utc(2006, 3, 1)) do - assert_equal Time.utc(2006, 4, 13), Net::FTP::List.parse(@char_dev.raw).mtime + assert_equal Time.utc(2006, 4, 13), parse(@char_dev.raw).mtime end end # Parsed during a leap year. def test_ruby_unix_like_date_leap_year Timecop.freeze(Time.utc(2012, 1, 2)) do - assert_equal Time.utc(2011, 10, 30, 15, 26), Net::FTP::List.parse(@symlink.raw).mtime + assert_equal Time.utc(2011, 10, 30, 15, 26), parse(@symlink.raw).mtime end end # mtimes today, same year. def test_ruby_unix_like_date_today_same_year Timecop.freeze(Time.utc(2013, 8, 16)) do - assert_equal Time.utc(2013, 8, 16, 14, 28), Net::FTP::List.parse(@file_today.raw).mtime + assert_equal Time.utc(2013, 8, 16, 14, 28), parse(@file_today.raw).mtime end end + def test_basename + assert_equal 'etc', @dir.basename + assert_equal 'README', @file.basename + assert_equal 'forums', @other_dir.basename + assert_equal 'spaces suck', @spaces.basename + assert_equal 'bar', @symlink.basename + assert_equal 'foo', @empty_symlink.basename + assert_equal 'ram0', @block_dev.basename + assert_equal 'null', @char_dev.basename + assert_equal 'log', @socket_dev.basename + assert_equal 'xconsole', @pipe_dev.basename + assert_equal 'index.html', @file_no_inodes.basename + assert_equal 'today.txt', @file_today.basename + assert_equal 'README', @no_user.basename + assert_equal 'participando.xls', @older_date.basename + assert_equal 'PSA 23 99 May-2010.pdf', @file_with_date.basename + end + def test_mtime assert_equal Time.utc(2008, 2, 15), @older_date.mtime assert_equal Time.local(2006, 4, 13), @block_dev.mtime + assert_equal Time.utc(2014, 1, 26), @file_with_date.mtime end def test_ruby_unix_like_dir @@ -141,7 +168,7 @@ def test_single_digit_hour Timecop.freeze(Time.utc(2014, 8, 16)) do single_digit_hour = nil assert_nothing_raised do - single_digit_hour = Net::FTP::List.parse('-rw-r--r-- 1 root other 531 Dec 31 3:59 README') + single_digit_hour = parse('-rw-r--r-- 1 root other 531 Dec 31 3:59 README') end assert_equal Time.utc(2013, 12, 31, 3, 59), single_digit_hour.mtime