test/test_file_find.rb in file-find-0.2.4 vs test/test_file_find.rb in file-find-0.2.5
- old
+ new
@@ -11,10 +11,11 @@
require 'fileutils'
require 'file/find'
require 'rbconfig'
require 'sys/admin'
include Config
+include FileUtils
class TC_File_Find < Test::Unit::TestCase
def self.startup
Dir.chdir('test') unless File.basename(Dir.pwd) == 'test'
@@windows = CONFIG['host_os'] =~ /windows|mswin/i
@@ -49,11 +50,11 @@
@rule1 = File::Find.new(:name => '*.txt')
@rule2 = File::Find.new
end
def test_version
- assert_equal('0.2.4', File::Find::VERSION)
+ assert_equal('0.2.5', File::Find::VERSION)
end
def test_path
assert_respond_to(@rule1, :path)
assert_respond_to(@rule1, :path=)
@@ -92,10 +93,15 @@
assert_false(rule1.find.empty?)
assert_true(rule2.find.empty?)
end
+ def test_find_basic
+ assert_respond_to(@rule1, :find)
+ assert_nothing_raised{ @rule1.find }
+ end
+
def test_mtime
rule1 = File::Find.new(:name => "*.rb", :mtime => 0)
rule2 = File::Find.new(:name => "*.rb", :mtime => 1)
assert_false(rule1.find.empty?)
@@ -158,10 +164,104 @@
assert_respond_to(@rule1, :follow)
assert_respond_to(@rule1, :follow=)
assert_true(@rule1.follow)
end
+ def test_maxdepth_basic
+ assert_respond_to(@rule1, :maxdepth)
+ assert_respond_to(@rule1, :maxdepth=)
+ assert_nil(@rule1.maxdepth)
+ end
+
+ def test_maxdepth_file
+ mkpath('a1/a2/a3')
+ touch('a1/a.foo')
+ touch('a1/a2/b.foo')
+ touch('a1/a2/c.foo')
+ touch('a1/a2/a3/d.foo')
+ touch('a1/a2/a3/e.foo')
+ touch('a1/a2/a3/f.foo')
+
+ @rule2.pattern = "*.foo"
+ @rule2.maxdepth = 1
+ assert_equal([], @rule2.find)
+
+ @rule2.maxdepth = 2
+ assert_equal(['a.foo'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.maxdepth = 3
+ assert_equal(['a.foo', 'b.foo', 'c.foo'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.maxdepth = nil
+ assert_equal(['a.foo', 'b.foo', 'c.foo', 'd.foo', 'e.foo', 'f.foo'], @rule2.find.map{ |e| File.basename(e) })
+ end
+
+ def test_maxdepth_directory
+ mkpath('a/b/c')
+ @rule2.pattern = "c"
+
+ @rule2.maxdepth = 1
+ assert_equal([], @rule2.find)
+
+ @rule2.maxdepth = 2
+ assert_equal([], @rule2.find)
+
+ @rule2.maxdepth = 3
+ assert_equal(['c'], @rule2.find.map{ |e| File.basename(e) })
+ end
+
+ def test_mindepth_basic
+ assert_respond_to(@rule1, :mindepth)
+ assert_respond_to(@rule1, :mindepth=)
+ assert_nil(@rule1.mindepth)
+ end
+
+ def test_mindepth_file
+ mkpath('a1/a2/a3')
+ touch('z.min')
+ touch('a1/a.min')
+ touch('a1/a2/b.min')
+ touch('a1/a2/c.min')
+ touch('a1/a2/a3/d.min')
+ touch('a1/a2/a3/e.min')
+ touch('a1/a2/a3/f.min')
+
+ @rule2.pattern = "*.min"
+
+ @rule2.mindepth = 0
+ assert_equal(['z.min', 'a.min', 'b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.mindepth = 1
+ assert_equal(['z.min', 'a.min', 'b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.mindepth = 2
+ assert_equal(['a.min', 'b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.mindepth = 3
+ assert_equal(['b.min', 'c.min', 'd.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.mindepth = 4
+ assert_equal(['d.min', 'e.min', 'f.min'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.mindepth = 5
+ assert_equal([], @rule2.find.map{ |e| File.basename(e) })
+ end
+
+ def test_mindepth_directory
+ mkpath('a/b/c')
+ @rule2.pattern = "a"
+
+ @rule2.mindepth = 1
+ assert_equal(['a'], @rule2.find.map{ |e| File.basename(e) })
+
+ @rule2.mindepth = 2
+ assert_equal([], @rule2.find)
+
+ @rule2.mindepth = 3
+ assert_equal([], @rule2.find)
+ end
+
def test_name_basic
assert_respond_to(@rule1, :name)
assert_respond_to(@rule1, :name=)
assert_equal('*.txt', @rule1.name)
end
@@ -249,20 +349,24 @@
def test_previous_basic
assert_respond_to(@rule1, :previous)
end
def test_expected_errors
- assert_raises(Errno::ENOENT){ File::Find.new(:path => '/bogus/dir').find }
+ assert_raise(Errno::ENOENT){ File::Find.new(:path => '/bogus/dir').find }
+ assert_raise(ArgumentError){ File::Find.new(:bogus => 1) }
end
def teardown
- FileUtils.rm_rf(@file1)
- FileUtils.rm_rf(@file2)
- FileUtils.rm_rf(@file3)
- FileUtils.rm_rf(@file4)
- FileUtils.rm_rf(@dir1)
- FileUtils.rm_rf(@dir2)
- FileUtils.rm_rf(@link1) unless @@windows
+ rm_rf(@file1)
+ rm_rf(@file2)
+ rm_rf(@file3)
+ rm_rf(@file4)
+ rm_rf(@dir1)
+ rm_rf(@dir2)
+ rm_rf(@link1) unless @@windows
+ rm_rf('a')
+ rm_rf('a1')
+ rm_rf('z.min') if File.exists?('z.min')
@rule1 = nil
@rule2 = nil
@file1 = nil
@file2 = nil