net-ftp-list 3.2.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 3
3
3
  :minor: 2
4
- :patch: 0
4
+ :patch: 1
5
5
  :build:
@@ -1,5 +1,6 @@
1
1
  # Represents an entry of the FTP list. Gets returned when you parse a list.
2
2
  class Net::FTP::List::Entry
3
+ include Comparable
3
4
 
4
5
  ALLOWED_ATTRIBUTES = [:raw, :basename, :dir, :file, :symlink, :mtime, :filesize, :device, :server_type] #:nodoc:
5
6
 
@@ -14,6 +15,35 @@ class Net::FTP::List::Entry
14
15
  end
15
16
  end
16
17
 
18
+ # Tests for objects equality (value and type).
19
+ #
20
+ # @param entry [Net::FTP::List::Entry] an entry of the FTP list.
21
+ #
22
+ # @return [true, false] true if the objects are equal and have the same type; false otherwise.
23
+ #
24
+ def eql?(other)
25
+ return false if !other.instance_of? self.class
26
+ return true if self.object_id == other.object_id
27
+
28
+ self.raw == other.raw # if it's exactly the same line then the objects are the same
29
+ end
30
+
31
+
32
+ # Compares the receiver against another object.
33
+ #
34
+ # @param (see #eql?)
35
+ #
36
+ # @return [Fixnum] -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object.
37
+ #
38
+ def <=>(other)
39
+ if other.instance_of? self.class
40
+ return self.filesize <=> other.filesize
41
+ elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float
42
+ return self.filesize <=> other
43
+ end
44
+ raise ArgumentError.new('comparison of %s with %s failed!' % [self.class, other.class])
45
+ end
46
+
17
47
  # The raw list entry string.
18
48
  def raw
19
49
  @raw ||= ''
@@ -41,8 +41,18 @@ class Net::FTP::List::Unix < Net::FTP::List::Parser
41
41
 
42
42
  # TODO: Permissions, users, groups, date/time.
43
43
  filesize = match[18].to_i
44
- mtime = Time.parse("#{match[19]} #{match[20]}")
45
- mtime -= ONE_YEAR if mtime > Time.now
44
+ mtime_string = "#{match[19]} #{match[20]}"
45
+ mtime = Time.parse(mtime_string)
46
+
47
+ # Unix mtimes specify a 4 digit year unless the data is within the past 180
48
+ # days or so. Future dates always specify a 4 digit year.
49
+ #
50
+ # Since the above #parse call fills in today's date for missing date
51
+ # components, it can sometimes get the year wrong. To fix this, we make
52
+ # sure all mtimes without a 4 digit year are in the past.
53
+ if match[20] !~ /\d{4}/ && mtime > Time.now
54
+ mtime = Time.parse("#{mtime_string} #{mtime.year - 1}")
55
+ end
46
56
 
47
57
  basename = match[21].strip
48
58
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{net-ftp-list}
8
- s.version = "3.2.0"
8
+ s.version = "3.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Stateless Systems"]
12
- s.date = %q{2011-01-09}
12
+ s.date = %q{2012-08-01}
13
13
  s.email = %q{enquiries@statelesssystems.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.txt"
@@ -3,6 +3,54 @@ require 'net/ftp/list'
3
3
 
4
4
  class TestNetFTPEntry < Test::Unit::TestCase
5
5
 
6
+ def test_equality
7
+ a = Net::FTP::List::Entry.new('foo1', {:basename => 'foo1'})
8
+ b = Net::FTP::List::Entry.new('foo2', {:basename => 'foo2'})
9
+ c = Net::FTP::List::Entry.new('foo1', {:basename => 'foo1'})
10
+
11
+ assert a.eql? c
12
+ assert a.eql? a
13
+ assert !a.eql?(b)
14
+ assert !a.eql?(1)
15
+ assert !a.eql?(1.0)
16
+ end
17
+
18
+ def test_comparison
19
+ raw1 = '-rw-r--r-- 1 root other 1 Dec 31 23:59 file1'
20
+ raw2 = '-rw-r--r-- 1 root other 2 Dec 31 23:59 file2'
21
+ raw3 = '-rw-r--r-- 1 root other 3 Dec 31 23:59 file3'
22
+
23
+ file1 = Net::FTP::List.parse(raw1)
24
+ file2 = Net::FTP::List.parse(raw2)
25
+ file3 = Net::FTP::List.parse(raw3)
26
+
27
+ assert file2 > file1
28
+ assert file2 >= file1
29
+ assert file2 == file2
30
+ assert file2 <= file3
31
+ assert file2 < file3
32
+ assert !(file2 < file1)
33
+ assert !(file2 <= file1)
34
+ assert !(file2 == file1)
35
+ assert !(file2 == file3)
36
+ assert !(file2 > file3)
37
+ assert !(file2 >= file3)
38
+
39
+ assert !(file2 < 2)
40
+ assert !(file2 < 2.0)
41
+ assert file2 <= 2
42
+ assert file2 <= 2.0
43
+ assert file2 == 2
44
+ assert file2 == 2.0
45
+ assert !(file2 > 2)
46
+ assert !(file2 > 2.0)
47
+ assert file2 >= 2
48
+ assert file2 >= 2.0
49
+
50
+ assert_raise(ArgumentError) { assert file2 > nil }
51
+ assert_raise(ArgumentError) { assert file2 > true } # wrong class
52
+ end
53
+
6
54
  def test_raise_on_unknown_options
7
55
  assert_raise(ArgumentError) { Net::FTP::List::Entry.new("foo", {:bar => "baz"}) }
8
56
  end
@@ -2,22 +2,13 @@ require 'test/unit'
2
2
  require 'net/ftp/list'
3
3
 
4
4
  class TestNetFTPListUnix < Test::Unit::TestCase
5
-
6
- ONE_YEAR = (60 * 60 * 24 * 365)
7
-
8
- FIRST_MINUTE_OF_YEAR = "Jan 1 00:00"
9
- LAST_MINUTE_OF_YEAR = "Dec 31 23:59"
10
- OTHER_RELATIVE_TIME = "Mar 11 07:57"
11
- SYMBOLIC_LINK_TIME = "Oct 30 15:26"
12
- ABSOLUTE_DATE = "Feb 15 2008"
13
-
14
5
  def setup
15
- @dir = Net::FTP::List.parse "drwxr-xr-x 4 user group 4096 #{FIRST_MINUTE_OF_YEAR} etc" rescue nil
16
- @file = Net::FTP::List.parse "-rw-r--r-- 1 root other 531 #{LAST_MINUTE_OF_YEAR} README" rescue nil
17
- @other_dir = Net::FTP::List.parse "drwxr-xr-x 8 1791 600 4096 #{OTHER_RELATIVE_TIME} forums" rescue nil
6
+ @dir = Net::FTP::List.parse "drwxr-xr-x 4 user group 4096 Jan 1 00:00 etc" rescue nil
7
+ @file = Net::FTP::List.parse "-rw-r--r-- 1 root other 531 Dec 31 23:59 README" rescue nil
8
+ @other_dir = Net::FTP::List.parse "drwxr-xr-x 8 1791 600 4096 Mar 11 07:57 forums" rescue nil
18
9
  @spaces = Net::FTP::List.parse 'drwxrwxr-x 2 danial danial 72 May 23 12:52 spaces suck' rescue nil
19
- @symlink = Net::FTP::List.parse "lrwxrwxrwx 1 danial danial 4 #{SYMBOLIC_LINK_TIME} bar -> /etc" rescue nil
20
- @older_date = Net::FTP::List.parse "-rwxrwxrwx 1 owner group 154112 #{ABSOLUTE_DATE} participando.xls" rescue nil
10
+ @symlink = Net::FTP::List.parse "lrwxrwxrwx 1 danial danial 4 Oct 30 15:26 bar -> /etc" rescue nil
11
+ @older_date = Net::FTP::List.parse "-rwxrwxrwx 1 owner group 154112 Feb 15 2008 participando.xls" rescue nil
21
12
  @block_dev = Net::FTP::List.parse 'brw-r----- 1 root disk 1, 0 Apr 13 2006 ram0' rescue nil
22
13
  @char_dev = Net::FTP::List.parse 'crw-rw-rw- 1 root root 1, 3 Apr 13 2006 null' rescue nil
23
14
  @socket_dev = Net::FTP::List.parse 'srw-rw-rw- 1 root root 0 Aug 20 14:15 log' rescue nil
@@ -36,15 +27,53 @@ class TestNetFTPListUnix < Test::Unit::TestCase
36
27
  assert_equal "Unix", @pipe_dev.server_type, 'LIST unix socket device'
37
28
  end
38
29
 
39
- def test_ruby_unix_like_date
40
- {
41
- FIRST_MINUTE_OF_YEAR => @dir,
42
- LAST_MINUTE_OF_YEAR => @file,
43
- OTHER_RELATIVE_TIME => @other_dir,
44
- SYMBOLIC_LINK_TIME => @symlink,
45
- ABSOLUTE_DATE => @older_date
46
- }.each do |date_and_time, parsed_entry|
47
- assert_equal parse_adjust_date_and_time(date_and_time), parsed_entry.send(:mtime)
30
+ class ::Time
31
+ class << self
32
+ def time_travel(time)
33
+ @traveled_to_time = time
34
+
35
+ begin
36
+ yield
37
+ ensure
38
+ @traveled_to_time = nil
39
+ end
40
+ end
41
+
42
+ alias_method :original_now, :now
43
+ def now
44
+ @traveled_to_time || original_now
45
+ end
46
+ end
47
+ end
48
+
49
+ # mtimes in the past, same year.
50
+ def test_ruby_unix_like_date_past_same_year
51
+ Time.time_travel(Time.new(2009, 1, 1)) do
52
+ assert_equal Time.new(2009, 1, 1), Net::FTP::List.parse(@dir.raw).mtime
53
+ end
54
+ Time.time_travel(Time.new(2008, 4, 1)) do
55
+ assert_equal Time.new(2008, 3, 11, 7, 57), Net::FTP::List.parse(@other_dir.raw).mtime
56
+ end
57
+ end
58
+
59
+ # mtimes in the past, previous year
60
+ def test_ruby_unix_like_date_past_previous_year
61
+ Time.time_travel(Time.new(2008, 2, 4)) do
62
+ assert_equal Time.new(2007, 10, 30, 15, 26), Net::FTP::List.parse(@symlink.raw).mtime
63
+ end
64
+ end
65
+
66
+ # mtime in the future.
67
+ def test_ruby_unix_like_date_future
68
+ Time.time_travel(Time.new(2006, 3, 1)) do
69
+ assert_equal Time.new(2006, 4, 13), Net::FTP::List.parse(@char_dev.raw).mtime
70
+ end
71
+ end
72
+
73
+ # Parsed during a leap year.
74
+ def test_ruby_unix_like_date_leap_year
75
+ Time.time_travel(Time.new(2012, 1, 2)) do
76
+ assert_equal Time.new(2011, 10, 30, 15, 26), Net::FTP::List.parse(@symlink.raw).mtime
48
77
  end
49
78
  end
50
79
 
@@ -105,12 +134,4 @@ class TestNetFTPListUnix < Test::Unit::TestCase
105
134
  assert_equal 'xconsole', @pipe_dev.basename
106
135
  assert @pipe_dev.device?
107
136
  end
108
-
109
-
110
- private
111
- def parse_adjust_date_and_time(date_and_time)
112
- parsed_time = Time.parse(date_and_time)
113
- parsed_time -= ONE_YEAR if parsed_time > Time.now
114
- parsed_time
115
- end
116
137
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 3
7
7
  - 2
8
- - 0
9
- version: 3.2.0
8
+ - 1
9
+ version: 3.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Stateless Systems
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-09 00:00:00 +11:00
17
+ date: 2012-08-01 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies: []
20
20