file-find 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGES +4 -0
  2. data/README +3 -1
  3. data/lib/file/find.rb +48 -12
  4. data/test/test_file_find.rb +113 -9
  5. metadata +3 -3
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.5 - 29-Dec-2008
2
+ * Added maxdepth and mindepth support.
3
+ * Added a 'clean' rake task to cleanup any test-unit results files.
4
+
1
5
  == 0.2.4 - 10-Dec-2008
2
6
  * Added support for symbolic permissions. Thanks go to Bill Kleb for the
3
7
  suggestion and to Hal Fulton for providing the solution.
data/README CHANGED
@@ -33,6 +33,8 @@ few options, hasn't been updated in over six years and isn't packaged properly.
33
33
  * ftype
34
34
  * inum (except Windows)
35
35
  * group (name or id)
36
+ * maxdepth
37
+ * mindepth
36
38
  * mtime
37
39
  * name (or 'pattern')
38
40
  * path
@@ -50,7 +52,7 @@ http://www.rubyforge.org/projects/shards.
50
52
 
51
53
  Some specific things I plan on adding:
52
54
 
53
- * symbolic permissions for the :perm option
55
+ * general FileTest operations
54
56
  * exec
55
57
  * links
56
58
  * support for :user and :group on MS Windows
data/lib/file/find.rb CHANGED
@@ -6,7 +6,7 @@ include Sys
6
6
 
7
7
  class File::Find
8
8
  # The version of this library
9
- VERSION = '0.2.4'
9
+ VERSION = '0.2.5'
10
10
 
11
11
  # :stopdoc:
12
12
  VALID_OPTIONS = %w/
@@ -16,6 +16,8 @@ class File::Find
16
16
  ftype
17
17
  inum
18
18
  group
19
+ maxdepth
20
+ mindepth
19
21
  mtime
20
22
  name
21
23
  pattern
@@ -72,6 +74,16 @@ class File::Find
72
74
  #
73
75
  attr_accessor :inum
74
76
 
77
+ # Limits search to a maximum depth into the tree relative to the starting
78
+ # search directory.
79
+ #
80
+ attr_accessor :maxdepth
81
+
82
+ # Limits search to a minimum depth into the tree relative to the starting
83
+ # search directory.
84
+ #
85
+ attr_accessor :mindepth
86
+
75
87
  # Limits searches by file modification time, where the value you supply is
76
88
  # the number of days back from the time that the File::Find#find method was
77
89
  # called.
@@ -147,6 +159,8 @@ class File::Find
147
159
  @user = nil
148
160
 
149
161
  @previous = nil
162
+ @maxdepth = nil
163
+ @mindepth = nil
150
164
 
151
165
  validate_and_set_options(options) unless options.empty?
152
166
 
@@ -207,23 +221,45 @@ class File::Find
207
221
 
208
222
  glob = File.join(File.dirname(file), @name)
209
223
 
210
- # Add directories back onto the list of paths to search unless
211
- # they've already been added.
212
- #
213
- # TODO: Add max_depth support here
214
- #
215
- if stat_info.directory?
216
- unless paths.include?(file)
217
- paths << file
218
- end
219
- end
220
-
221
224
  # Dir[] doesn't like backslashes
222
225
  if File::ALT_SEPARATOR
223
226
  file.tr!(File::ALT_SEPARATOR, File::SEPARATOR)
224
227
  glob.tr!(File::ALT_SEPARATOR, File::SEPARATOR)
225
228
  end
226
229
 
230
+ if @maxdepth || @mindepth
231
+ file_depth = file.split(File::SEPARATOR).length
232
+ path_depth = @path.split(File::SEPARATOR).length
233
+ depth = file_depth - path_depth
234
+
235
+ if @maxdepth && (depth > @maxdepth)
236
+ if File.directory?(file)
237
+ unless paths.include?(file) && depth > @maxdepth
238
+ paths << file
239
+ end
240
+ end
241
+
242
+ next
243
+ end
244
+
245
+ if @mindepth && (depth < @mindepth)
246
+ if File.directory?(file)
247
+ unless paths.include?(file) && depth < @mindepth
248
+ paths << file
249
+ end
250
+ end
251
+
252
+ next
253
+ end
254
+ end
255
+
256
+ # Add directories back onto the list of paths to search unless
257
+ # they've already been added
258
+ #
259
+ if stat_info.directory?
260
+ paths << file unless paths.include?(file)
261
+ end
262
+
227
263
  next unless Dir[glob].include?(file)
228
264
 
229
265
  if @atime
@@ -13,6 +13,7 @@ require 'file/find'
13
13
  require 'rbconfig'
14
14
  require 'sys/admin'
15
15
  include Config
16
+ include FileUtils
16
17
 
17
18
  class TC_File_Find < Test::Unit::TestCase
18
19
  def self.startup
@@ -51,7 +52,7 @@ class TC_File_Find < Test::Unit::TestCase
51
52
  end
52
53
 
53
54
  def test_version
54
- assert_equal('0.2.4', File::Find::VERSION)
55
+ assert_equal('0.2.5', File::Find::VERSION)
55
56
  end
56
57
 
57
58
  def test_path
@@ -94,6 +95,11 @@ class TC_File_Find < Test::Unit::TestCase
94
95
  assert_true(rule2.find.empty?)
95
96
  end
96
97
 
98
+ def test_find_basic
99
+ assert_respond_to(@rule1, :find)
100
+ assert_nothing_raised{ @rule1.find }
101
+ end
102
+
97
103
  def test_mtime
98
104
  rule1 = File::Find.new(:name => "*.rb", :mtime => 0)
99
105
  rule2 = File::Find.new(:name => "*.rb", :mtime => 1)
@@ -160,6 +166,100 @@ class TC_File_Find < Test::Unit::TestCase
160
166
  assert_true(@rule1.follow)
161
167
  end
162
168
 
169
+ def test_maxdepth_basic
170
+ assert_respond_to(@rule1, :maxdepth)
171
+ assert_respond_to(@rule1, :maxdepth=)
172
+ assert_nil(@rule1.maxdepth)
173
+ end
174
+
175
+ def test_maxdepth_file
176
+ mkpath('a1/a2/a3')
177
+ touch('a1/a.foo')
178
+ touch('a1/a2/b.foo')
179
+ touch('a1/a2/c.foo')
180
+ touch('a1/a2/a3/d.foo')
181
+ touch('a1/a2/a3/e.foo')
182
+ touch('a1/a2/a3/f.foo')
183
+
184
+ @rule2.pattern = "*.foo"
185
+ @rule2.maxdepth = 1
186
+ assert_equal([], @rule2.find)
187
+
188
+ @rule2.maxdepth = 2
189
+ assert_equal(['a.foo'], @rule2.find.map{ |e| File.basename(e) })
190
+
191
+ @rule2.maxdepth = 3
192
+ assert_equal(['a.foo', 'b.foo', 'c.foo'], @rule2.find.map{ |e| File.basename(e) })
193
+
194
+ @rule2.maxdepth = nil
195
+ assert_equal(['a.foo', 'b.foo', 'c.foo', 'd.foo', 'e.foo', 'f.foo'], @rule2.find.map{ |e| File.basename(e) })
196
+ end
197
+
198
+ def test_maxdepth_directory
199
+ mkpath('a/b/c')
200
+ @rule2.pattern = "c"
201
+
202
+ @rule2.maxdepth = 1
203
+ assert_equal([], @rule2.find)
204
+
205
+ @rule2.maxdepth = 2
206
+ assert_equal([], @rule2.find)
207
+
208
+ @rule2.maxdepth = 3
209
+ assert_equal(['c'], @rule2.find.map{ |e| File.basename(e) })
210
+ end
211
+
212
+ def test_mindepth_basic
213
+ assert_respond_to(@rule1, :mindepth)
214
+ assert_respond_to(@rule1, :mindepth=)
215
+ assert_nil(@rule1.mindepth)
216
+ end
217
+
218
+ def test_mindepth_file
219
+ mkpath('a1/a2/a3')
220
+ touch('z.min')
221
+ touch('a1/a.min')
222
+ touch('a1/a2/b.min')
223
+ touch('a1/a2/c.min')
224
+ touch('a1/a2/a3/d.min')
225
+ touch('a1/a2/a3/e.min')
226
+ touch('a1/a2/a3/f.min')
227
+
228
+ @rule2.pattern = "*.min"
229
+
230
+ @rule2.mindepth = 0
231
+ assert_equal(['z.min', 'a.min', 'b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
232
+
233
+ @rule2.mindepth = 1
234
+ assert_equal(['z.min', 'a.min', 'b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
235
+
236
+ @rule2.mindepth = 2
237
+ assert_equal(['a.min', 'b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
238
+
239
+ @rule2.mindepth = 3
240
+ assert_equal(['b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
241
+
242
+ @rule2.mindepth = 4
243
+ assert_equal(['d.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
244
+
245
+ @rule2.mindepth = 5
246
+ assert_equal([], @rule2.find.map{ |e| File.basename(e) })
247
+ end
248
+
249
+ def test_mindepth_directory
250
+ mkpath('a/b/c')
251
+ @rule2.pattern = "a"
252
+
253
+ @rule2.mindepth = 1
254
+ assert_equal(['a'], @rule2.find.map{ |e| File.basename(e) })
255
+
256
+ @rule2.mindepth = 2
257
+ assert_equal([], @rule2.find)
258
+
259
+ @rule2.mindepth = 3
260
+ assert_equal([], @rule2.find)
261
+ end
262
+
163
263
  def test_name_basic
164
264
  assert_respond_to(@rule1, :name)
165
265
  assert_respond_to(@rule1, :name=)
@@ -251,17 +351,21 @@ class TC_File_Find < Test::Unit::TestCase
251
351
  end
252
352
 
253
353
  def test_expected_errors
254
- assert_raises(Errno::ENOENT){ File::Find.new(:path => '/bogus/dir').find }
354
+ assert_raise(Errno::ENOENT){ File::Find.new(:path => '/bogus/dir').find }
355
+ assert_raise(ArgumentError){ File::Find.new(:bogus => 1) }
255
356
  end
256
357
 
257
358
  def teardown
258
- FileUtils.rm_rf(@file1)
259
- FileUtils.rm_rf(@file2)
260
- FileUtils.rm_rf(@file3)
261
- FileUtils.rm_rf(@file4)
262
- FileUtils.rm_rf(@dir1)
263
- FileUtils.rm_rf(@dir2)
264
- FileUtils.rm_rf(@link1) unless @@windows
359
+ rm_rf(@file1)
360
+ rm_rf(@file2)
361
+ rm_rf(@file3)
362
+ rm_rf(@file4)
363
+ rm_rf(@dir1)
364
+ rm_rf(@dir2)
365
+ rm_rf(@link1) unless @@windows
366
+ rm_rf('a')
367
+ rm_rf('a1')
368
+ rm_rf('z.min') if File.exists?('z.min')
265
369
 
266
370
  @rule1 = nil
267
371
  @rule2 = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file-find
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Berger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-10 00:00:00 -07:00
12
+ date: 2008-12-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.0.1
33
+ version: 2.0.2
34
34
  version:
35
35
  description: A better way to find files
36
36
  email: djberg96@gmail.com